Tips from Bram, be sure to read them.

Interaction with X

Vim works wonderful together with X. Besides the graphical version (gvim), it's also possible to start vim in a terminal like xterm, konsole, gnome-terminal or on Mac OS X. With the right options, it's still aware of the X clipboard, the mouse, et cetera.

First turn on the mouse with:

  :set mouse=a

vim has one default clipboard, but has an extra one under any a-z character. The two X clipboards are available too, but under a special character. To paste the X clipboards, paste either the + or * clipboard:

The star buffer contains the so-called X 'primary' for which you just select something with the mouse:


The plus buffer contains the so-called X 'clipboard' for which you selected with the mouse, then right-clicked and selected 'copy', or pressed a shortcut like CTRL-C or similar:


If this doesn't work, check the following things:

  $ ssh -v -X remotemachine
  debug1: Requesting X11 forwarding with authentication spoofing.
  debug1: Remote: No xauth program; cannot forward with spoofing.

If you are using Apple Mac OS X:

Vim on OS X

If you're running Mountain Lion (i.e. OS X 10.8), first install an X server: XQuartz

To get support for the X11 clipboard, install the MacPorts or the Homebrew version. If you don't have any of those installed yet (WHAT?!), follow the instructions on their website. When that's done:

Vim via MacPorts

Install vim:

 $ sudo port install vim

Then edit the file /opt/local/var/macports/sources/ and search for the line --without-x, and make sure it reads --with-x. Recompile with the following command:

 $ sudo port upgrade -n --force vim

Make sure to put /opt/local/bin in your path before anything else. Check the results by typing vim --version, which should say +xterm_clipboard somewhere below.

Vim via Homebrew



Put this in your .vimrc to run editor contents through an external program:

  map   <f9>   :w<CR>:!perl -c %<CR> 

Substituting line feeds

To replace line feeds (UNIX line feeds) in a file, use \n. To remove double empty lines in a file, do something like this:



Tags make it possible for vim to jump to function definitions under your cursor. First, you need to have exuberant ctags installed. It comes with most Linux distributions. For debian, install as follows:

  $ apt-get install exuberant-ctags

You have to create a tags file in your project its root directory:

  $ ctags -R

Then, add the following line to your .vimrc:

  set tags=tags;/

To jump to a function definition, put the cursor on the function call and press CTRL-]. To go back, press CTRL-t.

See also Tip 94 - Questions & Answers about using tags with Vim.

For code completion, type a few characters and press CTRL-n. See this blog entry: vim word and code completion.

Some keystrokes

*Search for the word under the cursor
%Go to matching opening/closing parenthesis
gd Go to local declaration
qaRecord macro a, stop recording by hitting q again
@aPlay macro a

Some commands

:mak Run make
:tsel List the tags

Compiling vim

If you want it done right, compile it yourself. The broken versions so casually strewn around your favorite Linux system are not to be taken seriously. Serious hackers compile their own editors. That said, here is my favorite stanza to compile vim on Debian Linux (Squeeze):

 $ ./configure --prefix=$HOME/opt/vim73 --with-x \
    --with-features=normal --enable-pythoninterp \
    --with-python-config-dir=/usr/lib/python2.6/config \
    --enable-gui && make

Cooperation with Python

You definitely want code completion in Python. To enable this, first make sure vim is compiled with Python support (type vim --version and check to see if there's a plus in front of the python option). Then add the following line to your .vimrc:

 autocmd FileType python set omnifunc=pythoncomplete#Complete

Auto-completion works via the CTRL-X CTRL-O command, but it's much easier to use tab for completion. Add the following lines to your .vimrc:

 function! SuperCleverTab()
     if strpart(getline('.'), 0, col('.') - 1) =~ '^\s*$'
         return "\<Tab>"
         if &omnifunc != ''
             return "\<C-X>\<C-O>"
         elseif &dictionary != ''
             return "\<C-K>"
             return "\<C-N>"
 inoremap <Tab> <C-R>=SuperCleverTab()<cr>

(From: Hacking Vim by Kim Shulz)

You'll also need exhuberant ctags installed; if you're running Debian, type:

 $ sudo apt-get install exuberant-ctags

Then go to your Python source code directory and type:

 $ ctags -R

To maintain the tags file, you could of course run it manually, but there's a vim plugin for that: [AutoTag]. Download the .vim file and place it in your $HOME/.vim/plugin directory. Anytime you save a file, it'll re-run the ctags command.