I have a Perl file, and I want to move all the comments on top.
Perl comments begin with a #.
I tried something like:
:g:^\s*#.*:m
You are missing the address to send the line to which is 0.
:g:^\s*#:m0
For more help see :h :m
Think i found the issue, I've ommited the line number
:g:^\s*#.*:m0
Related
I commented a part of a line using double quotes in my vimrc file, but that part of the line is not commented as can be seen at the bottom of the screenshot below:
Any idea why is this happening?
vim's docs say the following:
It is not possible to add a comment to a shell command ":!cmd" or to the
":map" command and a few others, because they see the '"'' as part of their
argument. This is mentioned where the command is explained.
Just put your comments on preceding lines.
vim's docs are here, for example.
Please, read :h :comment in the embedded help system. To quote the most relevant part:
It is not possible to add a comment to a shell command ":!cmd" or to the
":map" command and a few others (mainly commands that expect expressions)
that see the '"' as part of their argument:
...A long list follows...
So just don't.
I'm learning vim script here.
but, I have reached a deadlock.
:let save_ic = &ic
:set noic
:/The Start/,$delete
:let &ic = save_ic
I don't know what $delete means.
Is this an option?
Please tell me what it is.
And, please inform the document related to it.
The interesting line
:/The Start/,$delete
means issuing a command :delete to a range of lines, similar to what :1,10deletewould do. In this case, the first line in the range is the next line where pattern /The Start/ matches, and the last line in range is the last line in the file, $.
For clarity, you can write the command with a space between $ and the command :delete.
You can read through :h rangeto see other options. Here is another presentation of command ranges: The Vim Ranger.
I'd like to replace current string line with another (for example the another line is placed in 5 lines above current line). I can do it with a pair of commands
dd
:-5t-1
Is there the shorter way to obtain same goal?
dd
:-5t-1
is already pretty short if you ask me. But you can squeeze everything into a one-liner:
:d|-5t-1
and remove the 1 because it's implied by -:
:d|-5t-
Barring making a custom command or mapping I don't see how you could make it shorter.
:-5y<CR>Vp
is it shorter?
if you need do that really often, add this into your vimrc:
command! -range R d|<line1>,<line2>t-
then you can just do :-5R replace current line with -5 line
or 2,4R to cp line 2-4 (3 lines) to current line, and replace current line.
If you don't mind a plugin, my LineJuggler plugin offers a ]r command (and many more):
]r Fetch the line [count] visible lines above the current line and replace the current line with it.
With it, your example would be the short and easy 5]r
In addition, the companion LineJugglerCommands plugin now offers a similar :Replace Ex command. Again, your example would be
:Replace -5
Anyone know of any tricks in vim to easily swap two lines of code? I know that swapping adjacent lines is trivial, but let's say I wanted to swap line 23 with line 52. Is that achievable?
yes!
:23m52|51m22
then press Enter
it is also easy to write a function to do it.
My LineJuggler plugin has normal and visual mode mappings ]E and [E (and others, similar to the popular unimpaired plugin) to swap the current line / selection with [count] lines below / above.
I think a :[range]Swap {range} command would also be helpful; I'm working on that.
Edit: Here it is: LineJugglerCommands plugin.
If you have a visual selection when you paste, it will put the selection into the default buffer after pasting. So you would do 23Gdd51GVp23GP
user move command to move lines. e.g., swap line 23 and line 52:
:23m52 | 52m23
type :help move to get help usage of move, it may move a range of lines :-)
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;