ViM

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Introduction

ViM is the greatest editor ever invented. That's my opinion and I am sticking with it.

Why?

For starters, it is modal. When you write a document with pen and paper, you are actually doing a number of different tasks. Sometimes, you are writing things down. Other times, you are editing. These are different things.

Second, ViM keeps you on the keyboard, and only rarely do you need to use a control key combination.

Mastering ViM

How do you get really good at ViM? The same way you get good at anything else. Study, practice, adapt, and keep learning.

There is some really great stuff out there on ViM, including the help pages. Take the time to get familiar with them. If you plan on using ViM all the time, read the manual about one every six months or year so you can keep improving.

What follows below are the basic concepts and commands I use everyday.

Normal Mode

ViM starts in Normal Mode. This is the mode where what you type is NOT reflected on the screen. Here are a series of useful commands:

Go into Edit Mode

All of these commands will transition to Edit Mode. You can prepend with a number that means 'do this n times'. '4i' will insert the text 4 times instead of just once.

I use '78i-<ESC>' to insert 78 '-', for instance.

i Insert text before (to the left) of the current cursor position.
I Insert at the beginning of the first text of the line.
a Append (insert right after--to the right) at the current cursor position.
A Append at the end of the end of the line.
o Start a new line right below this one.
O Start a new line right above this one.
c... Followed by a motion, this allows you to change text--delete it and the enter edit mode.
cc Change the line.
R Enter edit mode, but don't insert text--overwrite it. (This is actually Replace Mode...)

Manipulating Registers (Copy, Cut and Paste)

You can delete text (cut) or just yank it (copy). Remember you can prepend counts to stuff.

d(motion) Delete that much text.
dd Delete the line.
D Delete to the end of the line.
y(motion) Yank (copy) that much text.
yy Yank the line.
Y Same as yy. (Yeah, it sucks. :map Y y$ if you care.)

By default, the stuff is put into the default register. To see what's in all the registers, ":reg".

You can specify which register to put stuff in by prepending the yank or delete (or even the 'c'-change) commands with "<register>, where register is a single letter a-z or A-Z.

There is more you can do with registers. I just don't use those features.

To paste, you can use the following commands:

p Paste after the cursor or below this line.
P Paste before the cursor or above this line.
]p. [p. ]P. [P Paste with the current indentation.

Motions

A lot of the commands in Normal Mode allow a motion to be appended. These motions can also be used to move the cursor. Thus, 'cj' means 'delete this line and the next, and go to insert mode' while 'j' means 'go down one line'.

You can prepend numbers to duplicate the motion that many times. '4j' means go down 4 lines.

h, j, k, l left, down, up, right.
^ To the first char on the line.
$ To the end of the line.
gg To the beginning of the file.
G To the end of the file. If there is a number, go to that line. '43G' will take you to line 43.
¦ To the nth column
f(char) "Find" (move on top of) the nth occurrence of the character char.
t(char) "'Til" (move right before) the nth occurrence of the character char. I use 't"' if I want to go to right before the end of the quote.
F(char) Goes left instead of right, but otherwise like f(char)
T(char) Goes left instead of right, but otherwise like t(char). I use 'T"' if I want to go to right after the opening quote of a quote I am in.
 ; Repeat last t, f, F, T
, Repeat last t, f, F, T in the opposite direction.
w, W Word/WORD forward. (Word is a word minus punctuation. WORD is a word plus punctuation connected to it.)
e, E End of word/WORD forward.
b, B Beginning of word/WORD backward.
(, ) Sentences forward or backward.
{, } Paragraphs forward or backward. (C/C++, Perl, and Java use { } to group code.)
 % Go to the matching paren, bracket, etc...

The following are only good to specify ranges. Note that they start with 'a' and 'i' which are normal mode commands of their own.

iw, iW The entire word/WORD you are on. (Good for selection--not motion.)
a[, a] A [...]
i[, i] Inside [...] (don't include [ ])
a(, a), ab A (...) (a block)
i(, i), ib inside (...) (a block)
a<, a>, i<, i> Likewise for <...>
a{, a}, i{, i}, aB, iB Likewise for {...} (a BLOCK)
a", a', a`, i", i', i` Quoted strings: "...", '...', `...`
at, it Likewise for HTML/XML tags.

Tags

Tags are useful for finding identifiers or navigating the help documentation.

CTRL-] Jump to the tag/id under the cursor.
CTRL-T or CTRL-O Jump back.

Increment/Decrement a Number

Use CTRL-A to increment, and CTRL-Z to decrement. This recognizes octal and hex by default as well as decimal. Use nrformats (nf) option to add alpha -- which will increment a to b, etc...

Cmdline Mode

Get Help

:h[elp] {subject}     " Get help on a subject
:h[elp] {section}     " Go to section section
:helpg[rep] {pattern} " Grep for words in help files.

The help contents are organized with tags. See tags above.

Change a lot of files at once

:args <file-pattern>
:argdo <cmd> | update

See section 26.3

Edit Mode

Edit mode is the mode where you are entering text.

<ESC> Leave Edit mode.
<CTRL-V> + any key Insert that key literally. For instance, to get '^M', do <CTRL-V> <ENTER>. To get a literal tab, <CTRL-V> <TAB>.

Settings

linebreak, lbr Boolean. Whether to break the lines (when wrap is off) in the middle of words or only at specific places.
breakat List of chars. What characters will allow linebreaks when linebreak is on. For shell scripts, you probably don't want to break at '-', for instance.
wrap Whether to wrap long lines.

Pattern Matching in ViM

Use '/' (search forward) or '?' (search backwards) if you are looking for something.

Search and Replace

Use ":(range)s/(pattern)/(replacement)/(flags)" to search and replace. By default, it only works on the current line, and only replaces the first match.

You can use Visual Highlighting to mark a section you want to do search and replace in. When you type ":s" from a visual selected area, it will put ":'<,'>" in for you. You can reuse that to mean the last visually selected block.

You can also do it on the entire file with ":%s".

Flags

If you want to confirm each substitution, put "c" as the flag. The text is highlighted with the "IncSearch" group if you want to change it.

If you want to see how many you would have replaced, use 'n'.

If you want to replace all matches, put 'g'.

If you want to ignore case, 'i'. (If you don't, and you are by default ignoring case, then 'I'.)

Search for lines and do something

You can do some really cool things like "delete all lines that match this" with the ":(range)g/(pattern)/(command)" command.

The command I use is almost always 'd', although you can get creative if you really want to. Don't forget the range.

Highlighting the Match

If you want to see the match highlighted, ":se hls". If you'd rather not, ":se nohls".

If you don't like the way it highlights, you can play with the "Search" highlight group coloring.

Incremental Search

If you'd like to match as you type in the regex, ":se is". If that drives you batty like it does me, ":se nois".

If you don't like the way it highlights, you can play with the "IncSearch" highlight group coloring.

Ignoring Case

If you'd like to ignore case, ":se ic". If you'd rather not (I never do, except in text) ":se noic".

There is a smartcase option. I avoid it at all costs.

Syntax Highlighting

ViM has great syntax highlighting. To turn it on:

:syn on

To set the syntax highlighting language:

:setf python

To have it recognize files by their extension:

" In ~/.vimrc
autocmd BufNewFile,BufRead *.myt setf myghty

Or you can create a file, such as myt.vim,. in the syntax directory. (See $VIMRUNTIME/syntax/synload.vim for how it does this.)

To make sure it syncs from the start:

:syn sync fromstart

See syntax in ViM for more useful information.

See Also