Tag Archives: vi

Vim tips

While writing on Adam Olsen’s blog, I included some tips for Vim users on each post. None of the posts themselves are relevant today, but the Vim tips are still useful, so they’re reproduced here. In the future I may decide to edit this post and add more tips.

Note that I’m not actively using Vim for my day-to-day editing, although it’s still my text/code editor of choice while working on a terminal.

Indentation

Vim has a few settings you can use to configure the indentation of your code. expandtab (et) is a boolean setting that does what it says, it specifies whether to expand tabs into spaces; set noexpandtab (noet) to disable it. tabstop (ts) specifies how wide tab characters are displayed. softtabstop (sts) specifies the ‘virtual tabstop’, the tabstop that you feel when you press Tab or Backspace. shiftwidth (sw) specifies the indentation applied when using autoindent (e.g. the line after an if statement will get indented by this amount).

For example, while working with Exaile, you have to :set et sts=4 sw=4 because Exaile always uses four spaces for indentation (thus ts is not applicable). However, while editing a makefile you would normally set noet and use the same number for ts, sts, and sw.

Modelines

In Vim, you can set per-file options using modelines. (This has nothing to do with the term “mode line” in Emacs. Emacs has a similar feature, but I don’t know what it’s called there.) Basically they are special strings in the first or last few lines of a file that Vim interprets into options. They look like, for example, // vim: expandtab. The exact syntax is explained in detail in the modeline section of Vim’s help.

Colour schemes

Tired of looking at the same colours in Vim all the time? Use the :colorscheme (:colo) command. For example, try :colorscheme evening (if you want to revert, the default scheme is named default). The effects of colour schemes are more noticeable when you’re using GVim.

There are a few colour schemes included with Vim, normally located in /usr/share/vim/vimXX/colors/ (XX is 70 if you use Vim 7.0).

Wrapping text

Sometimes it’s useful to wrap text at a specified column. You can use Vim’s gq command and textwidth (tw) option to do this. Now, Vim has tons of options for wrapping lines (it can even autowrap text while you’re typing) and gq itself can be used in many different ways, but I normally use gq to wrap a block of comments after I finished writing it. Simply select the lines you want to format (in Visual mode) and hit gq. The wrapping column is determined by textwidth, e.g. :set tw=80 to wrap at column 80.

Advertisements