Global Constant
Steve Nay's ramblings

New vim tricks

I’ve been using Vim since 2006, and it’s still my favorite text editor. My current job requires me to live in Visual Studio, so I’m using VsVim, a mostly-compatible Vim mode layered on top of the Visual Studio editor. My .vimrc file, plugins, and syntax files are on my GitHub account.

Since Vim is such an important part of my development toolset, I’m always interested to learn new tricks and techniques to make it more efficient. A coworker gave a presentation on Vim today. It was intended as an introduction, but he managed to teach me quite a few useful new things in the process. Here are a few of them:

  • e Move to the last letter of the current word (useful with w and b)
  • % Move to the parenthesis/curly brace/square bracket corresponding to the one under the cursor
  • f<character> Find the next (or previous, with F) occurrence of the given character on this line
  • :g!//d Remove all lines not matching a search previously done with / (not in VsVim)
  • CTRL+R " Paste the unnamed register while in insert mode
  • diw With the cursor in the middle of a word, delete the word
  • c$ Delete to the end of the line (or any other motion) and switch to insert mode
  • :55 Move to line 55, opening any code folds if necessary (easier and more robust than 55gg)
  • Move to the line at the top (H), bottom (L), or center of the screen (M), respectively
  • zz Center the current line in the screen, or move it to the top (zt) or bottom (zb)

Here are a few favorites that I’ve learned over the past few months:

  • * Find the next occurrence of the current word
  • gqq Wrap the current line (or gq for a visual block) to 80 characters, modifying line breaks if necessary. Useful for plain text documents.
  • = Normalize the code indentation for the selected block

Later article
DIY standing desk