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


CVS & SVN


vcscommand.vim is a plug-in useful for manipulating files controlled by CVS, SVN, SVK, git, bzr, and hg. To install under Ubuntu run the following (this requires the vim-scripts and vim-addon-manager packages to be installed first):

    vim-addons install vcscommand


Tag list


taglist.vim 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>.

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)
  • :tab split create a new tab using the current buffer

  • 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 0 move to the beginning of the current line
    • $ or % move to the end 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
    • '' move to the position before the last jump
    • '. move to the position where the last change was made
    • n% move to 'n' % of the file (n is in the range of 1 to 100)
    • z. center the window
    • 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 (d2w deletes two words)
    • 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
    • (for the following change commands, replace c with gu to make lower case, gU to make upper case or g~ to swap case)
    • cw change words from the current position onward (c2w changes two words)
    • cb change words from the left of the current cursor position backwards
    • cl change the character at the current cursor position
    • ch change the character to the left of the current cursor position
    • c0 change text from the current cursor position to the beginning of the line
    • c$ change 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)
    • :[range]s/[A-Z]/\l\0/g convert a range of lines to lower case (:[range]s/[A-Z]/\l&/g also works)
    • :so $VIMRUNTIME/plugins/MyPlugin.vim manually load a plugin
    • CTRL-w s split the window horizontally
    • CTRL-w v split the window vertically
    • CTRL-w n split the window horizontally creating a new file
    • :sp filename split the window horizontally opening file "filename" in the new window
    • :10 sp filename split the window horizontally opening file "filename" in the new window using only 10 lines
    • :sp +20 filename split the window horizontally opening file "filename" in the new window starting at line 20
    • :sp +/hello filename split the window horizontally opening file "filename" in the new window searching for "hello"
    • :vsp filename split the window vertically opening file "filename" in the new window
    • CTRL-w j move down one window
    • CTRL-w k move up one window
    • CTRL-w h move left one window
    • CTRL-w l move right one window
    • CTRL-w = make all windows equal size
    • CTRL-w - reduce current window height by one
    • CTRL-w 5 - reduce current window height by five
    • CTRL-w + increase current window height by one
    • CTRL-w 5 + increase current window height by five
    • CTRL-w < reduce current window width by one
    • CTRL-w 5 < reduce current window width by five
    • CTRL-w > increase current window width by one
    • CTRL-w 5 > increase current window width by five
    • CTRL-w w cycles through all windows
    • CTRL-w r rotates windows to the right
    • CTRL-w R rotates windows to the left
    • CTRL-w J move the current to the bottom
    • CTRL-w K move the current to the top
    • CTRL-w H move the current to the left
    • CTRL-w L move the current to the right
    • CTRL-w q close the current window