box.matto.nl

home/

Some vim-tips

Last edited
vim is a great editor. It started out as another vi-clone but has become the most enhanced version of vi on the planet.

Here are some tips:

Splitting windows

Open more then one file at the same time and edit them both. First, split the screen and open a second file:

:split second_file_name

Now you have a screen splitted into two areas, one holding the first file and one holding the second file.

Moving from one window to the next

Ctrl-w Ctrl-w: rotate through the windows
Ctrl-w space: rotate through the windows
Ctrl-w j: go to window downwards
Ctrl-w k: go to window upwards
Ctrl-w _: maximize current window
Ctrl-w =: make all windows same size

Pasting indented text

When vim is autoindenting the results of pasting some text can be quite horrible. There is a simpel solution for this:

:set paste
< some pasting here >
:set nopaste

That is all!
now you cat paste text in vim too :)

Selecting blocks

A fast way to select bloks is with visual-a.

To select a block between ( and ) do:

vab

and to select a block between { and } do:

vaB

Showing all your bookmarks

Using marks in vim

You can easy set bookmarks in your text with the command: m<label> where label is a character. Example:

ma
This sets the mark "a".
From anywhere in your file you can jump to this mark with single-quote-<label>. In the example above the label is "a", so you can jump to this mark with:
'a

Doing stuff until the next mark

Marks are not only great to find stuff in your file, you can also use them as a boundary when doing stuff over multiple lines. Example:

.,'as/^/> /
This stands for:
Start from here (location of your cursor)
Stop at mark "a" (do until this mark)
Replace the beginning of the line (^)
With > followed by a space

Working on a marked area

You can mark an area by marking the beginning of the area and marking the end of the area.

Say you mark the beginning of the area with a and the end with b. Now we can do stuff like:

  • delete area
    :'a,'b d
  • write area to a new file
    :'a,'b w <filename>
  • substitute only in this area
    :'a,'b s/this/that/g

Showing all your bookmarks

Setting marks is a very nice feature that you will learn to love. Once you have the hang of it, you will start using multiple marks in a single file.

You can ask vim to show a list of all your marks (and some marks it made on its own) with the command:

:marks

If you are only intested in what is under mark f, you can ask vim also:

:marks f

Sort an part of your file in vim

Go to the beginning of the area you want to sort.

Hit "v" (without the quotes) to start marking the area you want to sort.

Go to the end of the area you want to sort.

Hit

:sort

Et voila: the area is sorted alphabetical (well, ASCII-betical).

Jump to the last insert position

Hit gi in normal mode will jump to the last position where you were in insert mode.

Open a filebrowser

Open a filebrowser with

:E

Edit your last shell command

In your bash-shell, hit

fc

This opens the last command-line into your default EDITOR, which you can than edit. When you close your EDITOR, the (edited) command gets executed.

Show word-count

Some information of your current buffer is shown when entering the command

g Ctrl-g

This includes the word-count

Mapping for opening companion include file

When editing a file named foo.c this mapping will open a new window with file foo.h:

map <C-h> :new %:p:r.h

Put this line in your .vimrc. Then, open a file foo.c and hit Ctrl-h.

(more to follow)

Other sites

Last updated: $Date: 2010-01-29 10:55:32 $