vankuik.nl RecentChanges 2010-03-10 XML TXT

Vim

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 Terminal.app 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:

  <esc>"*p

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:

  <esc>"+p

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.

Macros

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:

  :%s/\n\n/\n/

Tags

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