Remove all arbitary spaces before a line in Vim - vim

I'v written a plugin where it comes to parsing a XML tag. The content inside the tag is indented and when i copy the parsed string into the file it's gettting like:
Example line
This is part of the parsed line
Thats goes one
End of line
What I want is to remove all spaces in front of these lines, the final text should be
Example line
This is part of the parsed line
Thats goes one
End of line
I've tried to use = but it doesn't work the way I want. How can I do that with minimal key strokes ?

To format a line to the left I use :left. Use this format an entire file:
:%le

A simple search/replace s/^\s*// should do the trick, but it's probably not the minimal version.

Personally I would visually select the lines with V, then use 99< to push the text as far left as it could go.

Just type d followed by w followed by j at the beginning of each line.

How about this:
:%s/^ *//
Or are you looking for a vim-script solution?

To remove initial spaces and tabs at specified line numbers (E.g. from lines 5 to 10),
:5,10s/^\s*//

Yet another way to achieve this is using the the normal command :h :normal-range
:%norm d^
This goes to column 0 in each line (%) and deletes (d) to the first non-white character(^).
This is slightly more to type as the accepted answer, but allows for easy extension if you have a more complex scenario in mind, such as additional un-commenting or so:
:%norm d^I#
Resulting in:
#Example line
#This is part of the parsed line
#Thats goes one
#End of line

The search/replace suggested by Lukáš Lalinský or the %le approach in the wikia page is probably the way I'd do it, but as another alternative you could also do:
:%< 99
As a quick way to shift the whole file (%) 99 times to the left.

Remove all consecutive spaces: :%s/ */ /g
It was useful to me to go from:
$screen-xs-min: 480px;
$screen-sm-min: 768px;
$screen-md-min: 992px;
$screen-lg-min: 1200px;
To:
$screen-xs-min: 480px;
$screen-sm-min: 768px;
$screen-md-min: 992px;
$screen-lg-min: 1200px;

Related

Write output from command line into file in vim

I would like to insert at the end of each line the number of alphabetic characters on that line. To do this on one line is easy. I search using :s/\a//gn and get the occurrence of alphabetic characters in the command line and then A and space and enter the number.
My problem arises when I have so many lines that it becomes extremely tedious to do this. I am trying to create a macro but am having difficulty getting command line output into it. Is there a way to do this without resorting to *nix commands? I have access to a *nix box but not all the time.
So if my file had the following content:
abc2d4s
jd4a5ag
jdf7fjf
abdd5ff
I would like the output to look like this:
abc2d4s 5
jd4a5ag 5
jdf7fjf 6
abdd5ff 6
I was thinking if there was a way to get the replace output piped into the register somehow but cannot figure out how to do it, but maybe there is a better way.
You can capture the output of the :s///gn command with :redir, but in this case, I would rather implement the counting via substitute() and :help sub-replace-expression:
:%s/.*/\=submatch(0) . ' ' . len(substitute(submatch(0), '\A', '', 'g'))/
This matches the entire line (.*), then removes all non-alphabetic characters (\A), and appends the length of the result. Note: Works only for ASCII characters (but \a covers only those, anyway)!
this cmd should give you that output:
%s/.*/\=submatch(0).' '.(len(submatch(0))-len(substitute(submatch(0),'\a','','g')))
One way to do that would be to use a simple macro:
:%norm A <C-v><C-r>=col('.')-2<C-v><CR>
which should look like:
:%norm A ^R=col('.')-2^M
where we enter insert mode at the end of each line and insert a space followed by the column number of the last character.
A variant:
:%norm A^R=" ".len(getline('.'))^M

Delete all characters after "." in each line

I have a text file with about 2,000 lines of text and I want to remove a large portion of each line of text.
The text is in this format:
Important Text that I care about. Stuff I want to delete
I am unsure as to how to delete all of the text after the . in each line.
Can someone give me a quick command that would do this?
With substitutions:
:%s/\..*/./
With :normal command:
:%norm f.lD
Various additional :normal solutions:
:%norm )Dx
:%norm $T.D
:%norm f.C.
:%norm 0/\. /e<C-v><CR>D
Use the Substitution Ex Command to Trim All Lines
This is very similar to both answers, yet I think there is value in presenting it.
Like the other answers, I just used the ex substitution command:
:%s/[^.]*$//
Explanation of substitution:
% indicates a range for all lines.
[^.] is a character class of all non-period characters
* is a quantifier indicating 0 or more matches.
$ is an anchor which communicates to VIM that we want this pattern to match at the end of the line.
Addendum
The solution assumes each line will have a period, otherwise the command will not work as expected as #Qeole has indicated. Qeole's solution addresses non-periods lines appropriately.
Use search and replace "vim feature" combined with regex:
:%s/\..*$//g
with the cursor at the first character of first line.
fS<Ctrl-V>G$d

Vim: Yank distant line without moving

Suppose I have the following (* = cursor):
...
*
Kittens
Puppies
Humans
...
How do I yank the "Humans" (cursor relative 3rd line) while leaving the cursor in place?
Preferably in one motion or one (generic) command.
Try this:
:+3y
It uses the range +3 , that it is the point where it will begin to yank. and by default it does one line.
UPDATE: If you wanted to copy both the second and third line without moving cursor, you would use same command but with a range of two points, like:
:+2,+3y
It would copy both Puppies and Humans.
Now, for those who are wondering, based on Birei's answer, you can also do :ny, where n is the line number you want to yank. This is actually what I was looking for when I came to this page, and is helpful if you've got set number on and don't want to count relative lines.
Now, I wonder, how to perform this yank to a particular register! :D
My LineJuggler plugin combines yanks and puts into various short mappings; e.g. with 3[f you can fetch a line 3 lines below the current line and put it below the current line.

Vim: Replace all dots but the last one in a line

I'm looking for a way to replace all dots in a line except the last one.
Example:
Blah - whatever - foo.bar.baz.avi should become
Blah - whatever - foo bar bar.avi
Since I'll have more than one line in the file and the amount of dots varies in each line I'm looking for a generic solution and not something like "Replace the first X matches" with X being a constant.
This seems to do the trick:
%s/\.\(.*\.\)\#=/ /g
\#= is a lookahead. It only matches a full stop if there are any following full stops.
See :help zero-width.
As a variation on the first answer you may prefer this syntax:
%s/\.\ze.*\./ /g
It let's you put the assertion that there be a following full stop after the search operator.
Another way that might be useful to learn (replace inside visual selection) would be:
:g/^/normal $F.hv0:s/\%V\./ /g^M
where ^M is entered with CTRL-V, Enter.
This means: for each line (g/^/) type $F.(go to last dot), visually select from character to the left till beginning of line (hv0), and then replace dots (:s/\./ /g^M) only inside visual selection (\%V).

vi search copy paste search copy

I was just wondering if anyone could help out with how to do the following using vi.
I have a text file and it might contain something like
start of text file:
--something1.something2--
--anotherThing1.something2--
end of text file:
If I want to take this line and convert it by way of searching for anything matching the first occurrence of [A-Za-z0-9] copy that to the buffer then append it on the same line to before the first occurrent of --
start of text file:
--something1.something2.something1--
--anotherThing1.something2.anotherThing1--
end of text file:
Is there a single VI command to do this?
Cheers
Ben
:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/gc
or without asking confirmation for every replace:
:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/g
will produce:
--something1.something2.something1--
--anotherThing1.something2.anotherThing1--
from:
--something1.something2--
--anotherThing1.something2--
This is if you want to copy the first word after '--' up to first '.' and append '.' and word found before the last '--'.
Using vim.
RE COMMENTS:
Someone mentioned that it will not work when there are multiple words and so on.
I tested it on the following:
start of text file:
--something1.something2.something3.something4.something5.something6--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5--
end of text file:
after replace with the above expression:
start of text file:
--something1.something2.something3.something4.something5.something6.something1--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5.anotherThing1--
end of text file:
Try this:
%s:\([A-Za-z0-9]\+\)\(\..\+\)--:\1\2.\1--:g
Holy crap, in the time it took me to login to post that answer, you posted it and already got a comment!
%s/--\(\w*\)\.\(.*\)--/--\1.\2.\1--/g
--ab1.cd2..yz99-- -> --ab1.cd2..yz99.ab1--
Building on stefanB's solution but using negated character classes and the "very magic" setting, I arrive at the following substitution command:
:%s/\v--([^.]+)\.(.*)--/--\1.\2.\1--/
Depending on the exact requirements (what are allowed characters for "something1" and "anotherThing1") this might or might not be more correct.
One more thing to consider: all solutions posted so far assume that there is only one occurance of the "--text.someOtherText-- pattern per line. If this is not the case, the (.*) part of the pattern would have to be adjusted and the /g modifier is required.

Resources