Unleashing VIM
This page is the how-to note on VIM as new tricks learned.
Multi-lines actions
In VIM, it is quite useful to utilise the visual mode and multi-line actions when you are commenting out multiple lines for troubleshooting or documentation.
Here is how to comment out multi-lines.
Ensure that we are in normal mode.
Move the cursor to the first line you want to comment out and press
Ctrl + v
to put the editor into visual mode.Select the lines you want to comment out with VIM's key
j
or arrow down key.After that, press
Shift + i
to put the editor into insert mode inside visual mode and then press#
which will add a hash to the first line.Then press
Esc
to insert#
character on all other selected lines.
Here is how to remove comment from multi-lines.
Ensure that you are in normal mode.
Move the cursor to the first line you want to comment out and press
Ctrl + v
to put the editor into visual mode.Select the lines you want to comment out with VIM's key
j
or arrow down key.Then press
x
to delete#
character on all lines.
The same workflow can be used to perform any other multi-lines actions in VIM as well.
Macros
It can be quite powerful to use macros in VIM for the same repetitive task in your editor. For instance, a specific set of motions and actions you are about to perform at multiple places can be recorded as a macro.
Here is how to record a macro in VIM.
Ensure that you are in normal mode.
Press
q + w
to registerw
as macro.Perform the commands/actions while it shows
recording @w
.If it is in the insert mode, press
Esc
to get out of it.Then press
q
to end the recording which stores the commands/actions in@w
.
To reuse the recorded macro, move the cursor to where you want to perform the same commands/actions in VIM, and press @w
to recall the macro.
Last updated