home index sites engineering software hardware email
 

How-To: Vim



Documentation & resources




vimrc


Take a look at my personal vimrc for some useful vim configuration settings and tweaks.


Plug-ins


Tag list


Taglist is a source code browser that provides an overview of the structure of a file making it easy to browse through source. To install under Ubuntu run the following (this requires the vim-scripts and vim-addon-manager packages to be installed first):

    vim-addons install taglist


snipMate


snipMate aims to be an unobtrusive, concise vim script that implements some of TextMate's snippets features in Vim. A snippet is a piece of often-typed text that you can insert into your document using a trigger word followed by a <tab>.

CVScommand


CVScommand is a plug-in for dealing with CVS repositories.

SVNcommand


SVNcommand is a plug-in for dealing with SVN repositories.

More plug-ins!


See the Vim.org website for a complete list of plug-ins (or run vim-addons under Ubuntu).


Spell check


Vim (as of version 7) has a built-in spell checker. To enable the spell checker add the following to your vimrc:

	se spell
	setlocal spell spelllang=en_us
	set spellfile=~/.vim.spell.add

To make it even easier add the following, which enables spell checking with F7 and disables it with shift-F7, to your vimrc :

	map <F7> <Esc>:setlocal spell spelllang=en_us<CR>
	map <S-F7> <Esc>:setlocal nospell<CR>

You can find the above in my personal vimrc.

Here are some of the more useful spell checking commands:

  • zg to mark a word as correctly spelled
  • zw to mark a word as misspelled
  • zug undo zg
  • zuw undo zw
  • z= show a list of alternate spellings
  • :spellr repeat the last z= replacement for all matches in the current window
  • ]s skip to next misspelled word
  • [s skip to previous misspelled word

  • For more information run :help spell inside a Vim session!


    Tabs


    Vim (as of version 7) has tab support. To make it a little easier to move back and forth between tabs, add the following, which maps shift-left (arrow) to move to the previous tab and shift-right to the next tab, to your vimrc:

    	map <S-left> :tabp<CR>
    	map <S-right> :tabn<CR>

    Of course, you can find the above in my personal vimrc.

    Here are some of the more useful tab commands:

  • :tabnew create a new tab
  • :tabnew filename open filename in a new tab
  • :tabm n moves the current tab to position n (Vim starts tab numbering from 0)

  • For more information run :help tab-page-intro inside a Vim session!

    Useful Commands


    • } move cursor to the end of the current paragraph
    • { move cursor to the beginning of the current paragraph
    • ) move cursor to the end of the current sentence
    • ( move cursor to the beginning of the current sentence
    • ^ move to the first non-blank character in the current line
    • $ or , move to the end of the current line
    • 0 move to the beginning of the current line
    • w or W move to the beginning of the next word
    • b or B move to the beginning of the previous word
    • fX move to the next 'X' character on the current line
    • FX move to the previous 'X' character on the current line
    • e move to the end of the next word
    • H move to the first line of the screen
    • M move to the middle line of the screen
    • L move to the last line of the screen
    • G move to the end of the file
    • gg move to the beginning of the file
    • gq<CR> format the line (Q also worked in earlier versions, restore with this: :nnoremap Q gq)
    • v start highlighting characters
    • V start highlighting entire lines
    • dw delete words from the current position onward
    • db delete words from the left of the current cursor position backwards
    • dl or x delete the character at the current cursor position
    • dh or X delete the character to the left of the current cursor position
    • d0 delete text from the current cursor position to the beginning of the line
    • d$ or D delete the entire line starting at the current cursor position
    • . redo last command
    • :bufdo cmd executes cmd in each buffer in the buffer list
    • [[ go to the start of a section, backward
    • ]] go to the start of a section, forward
    • [] go to the end of a section, backward
    • ][ go to the end of a section, forward
    • :w >> /tmp/file.txt append to a file
    • :set list shows unprintable characters
    • :set noai disable auto indenting
    • :set paste enter paste mode (disables auto indenting amongst other things)
    • while in insert mode press ^P (previous) and ^N (next) to scroll through word completion matches
    • [count]["x]yy or [count]["x]Y yank [count] lines (default of 1) [into register x]
    • {Visual}["x]y yank the highlighted text [into register x]
    • :[range]y [x] {count} yank {count} lines, starting with last line number in [range] [into register x]
    • ["x]p put the text [from register x] after the cursor [count] times
    • ["x]P put the text [from register x] before the cursor [count] times
    • ["x]gp put the text [from register x] after the cursor [count] times, leaving the cursor just after the new text
    • ["x]gP put the text [from register x] before the cursor [count] times, leaving the cursor just after the new text
    • :[line]pu [x] put the text [from register x] after [line] (default current line)
    • :[line]pu! [x] put the text [from register x] before [line] (default current line)