Page cover image

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.

  1. Ensure that we are in normal mode.

  2. Move the cursor to the first line you want to comment out and press Ctrl + v to put the editor into visual mode.

  3. Select the lines you want to comment out with VIM's key j or arrow down key.

  4. 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.

  5. Then press Esc to insert # character on all other selected lines.

Here is how to remove comment from multi-lines.

  1. Ensure that you are in normal mode.

  2. Move the cursor to the first line you want to comment out and press Ctrl + v to put the editor into visual mode.

  3. Select the lines you want to comment out with VIM's key j or arrow down key.

  4. 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.

  1. Ensure that you are in normal mode.

  2. Press q + w to register w as macro.

  3. Perform the commands/actions while it shows recording @w.

  4. If it is in the insert mode, press Esc to get out of it.

  5. 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

Was this helpful?