Vim Tips

21 Aug 2013 in Tech

I've been using vim for a while now, but with Vim being Vim, there's always new things to learn. Thanks to Jacek, I've learned a few more things recently.

S to change a line

Whenever I wanted to change an entire line before, I used to use 0c$ (go to the beginning of the line and change everything to the end). Since I learned about S which does the same thing, I've been trying to use it. Muscle memory's difficult to break though, so I'm still stuck using 0c$, but I'm trying to use S more.

| to go to a column

When working Google Bigquery, if the data provided did not fit the schema it returned an error with the line and column that it found an error on. I used to use <num>gg to jump to the correct line, and then a combination of w and h/l to get the correct column. As it turns out, you can use <num>| to jump directly to a column. So, to jump to line 29, column 33 we'd use 29gg33|.

Ctrl+f to move down a page

Whilst most of the navigating I do whilst inside a file in vim is done via searching (e.g. /searchterm), sometimes it's useful to scan through a file to get a feeling of how it's structured. Previously, I just kept my finger on j to scroll through the file. ctrl+f is a much more efficient way to page through the file, one screen at a time.

:%! to run an external program

To run the contents of your buffer through an external program, you can use :%!<program>. For example, to reformat the current buffer to wrap at 80 characters, we can use the fmt command line utility. To use it through vim, we type :%!fmt -80. The % is a normal vim selector, so you could use :.!fmt -80 fo reformat just the current line, or :.,+5!fmt -80 to reformat the next five lines (including the current one).