Vim Tips 2013-12-23

Here are some tips for a couple of my favorite Vim commands:

Open File Under Cursor

How? Place the cursor over a filename then type gf. Vim attempts to opens the file relative to the current working directory. You can change your current directory using :cd.

Why? I use this command when I am reading error logs to open files mentioned in the log.

More info on this command.

Changing Blocks of Text

How?

  • cc change the line under the cursor
  • ciw change the word under the cursor
  • ci" change the contents of the next quote under or after the cursor
  • ca" change the entire quote (includes the quote characters)
  • ci( change text inside parentheses under the cursor
  • ca( includes the enclosing left- and right parentheses characters
  • c + motion = change text covered by a motion

Why? I use this family of commands constantly when coding to change string literals or the contents of parenthesis. The list above is incomplete, but you can combine the c-command with any motion you want. For example c3Fx will change text to the third previous occurrence of x.

Using Folds

How?

  • zf fold over selected lines
  • zo open fold under cursor
  • zR open all folds

Why? I mainly use folds together with the latex-suite plugin. Other plugins will also fold text for you. Folds are useful because they reduce the time to manually scan through large blocks of text after a relevant section. If you have large LaTeX documents folds make a big difference by folding all figures etc that would otherwise get in the way of the text!

More info on folds.

Increment or Decrement Numbers

How? Pressing <C-a> will make Vim increment the next number on the current line. <C-x> decrements the number.

Why? Very handy for example when you have a list of indices/offsets and you want to increment all by some amount. I also find that it is often quicker to hit <C-a> or <C-x> a couple times rather than entering insert mode to adjust a number.

More info on these commands.

Categories: Programming

Leave a Reply

Your email address will not be published. Required fields are marked *