Vim Tips and Tricks

This article does not serve as a general Vim tutorial. There are already great resources that cover this: vimtutor, countless YouTube videos and cheat sheets.

What I will focus on here is sharing various Vim operations that I have found useful, and you might as well.

System clipboard

You can use the special + register to manipulate the OS clipboard.

To yank into this register, use the "+y command. To paste from it, use "+p.

This shortcut can admittedly get quite awkward, Neovim solves this by default by linking the yank register to your system clipboard automatically.

Commenting and uncommenting lines (Original source)

For this task, you can use block selection.

Commenting out code

Go to the first line you want to comment out. Move your cursor to be placed on the first non-whitespace character. Then, press Ctrl+V to activate visual block mode.

Keep moving your cursor down until you reach the last line you want to comment out. Then, press Shift+I to enter insert mode and insert whatever pattern is used for comments (such as // in C family languages).

After you're done, press Esc. It may take a few seconds for the operation to complete.

Uncommenting code

Go to the first line you want to activate and put your cursor on the first character of the comment pattern. After that, activate visual block mode.

Now, select the entire pattern you want to remove, then press the X key.