Vim is a text editor standard on nearly all *nix distributions. The only two distributions I know of that don't have Vim standard are gentoo and linux from scratch. Vim stands for 'vi improved' as Vim is based on the original visual (as opposed to line oriented) editor for unix, called 'vi'. Vi, and thus Vim, represent a fundamentally different way of editing files, as compared to the now more common methods employed by editors such as EMACS, Notepad, Kate, and pretty much every other editor out there. The difference lies in the fact that Vim operates in various 'modes', meaning different key strokes do different things at different times. This allows us to use only the standard alphanumeric and punctuation keys to perform all our tasks, because when we are not in insert mode, they can be used for something else. This is important, historically because keyboards used not be standardised beyond the alphanumeric and punctuation keys, and because even today, when remotely connected to another computer via telnet or ssh, there are only limited character sets (primarily employing only alphanumeric and punctuation characters) available on the connection protocol.
It's important to realise that Vim is incredibly powerful, and really huge (in terms of learning quantity). I've been using it for eight years as my primary editor, and still find things I don't know about, and that I actually would start using daily. Never mind all the stuff in the help system I know I don't know about! This document serves only to introduce the very basics of Vim. If you want to know more, in general or about a specific command, and trust me, there's more to know, use Vim's help system.
We start Vim in a *nix environment simply by typing vim
at the command prompt.
sirlark@hephaestus ~ $ vim
Alternatively, we can choose a file to edit immediately, by specifying it as an argument on the command line
sirlark@hephaestus ~ $ vim myfile.txt
We can in fact specify any number of filenames, and Vim will open them all, although only the first will be visible upon starting Vim. Files that already exist are loaded from disk into a buffer in Vim for editing. Buffers may not be visible, but are still in memory. Changes made to a buffer (changes you make to a file are in fact only made in memory) are only saved to disk when Vim is instructed to write the buffer. Filenames that do not exist create empty buffers, and the file itself is only created upon buffer write.
Vim starts in 'Command' (aka 'Normal' mode). In this mode keystrokes are not inserted into files, but rather they are used to issue large (or small) scale editing commands, such as cut, paste, delete line, jump to beginning of file, etc... Additionally, entering the character ':' allows us to type more complex commands on a Vim command line. A number of Vim commands use the concept of a motion. Simply put, a motion is a movement of the cursor from one point in the file to another. The motion is considered to be the contents of the file between the cursor's starting point and it's end point. The really important keystrokes you will need are
w
moves to the beginning of the next word, where a
word is considered an alphnumeric sequence of characters, i.e.
punctuation breaks up words.W
moves to the beginning of the next word, where a
word is considered an sequence of characters not broken by
whitespace. Synonyms: CTRL-Right
e
moves to the next end of word
(alphanumeric).E
moves to the next end of word (whitespace).r<character>
replaces the character under
the cursor with the character entered.R
enters Insert Mode in overwrite or replace
sub-mode.CTRL-R
redoes the last action undone by an undo
comamnd.y<motion>
yanks (or copies) the motion for
later pasting. If the motion is across multiple lines, all lines
involved are copied in their entirety, linewise.yy
copies the entire contents of the current line,
linewise.u
undoes the last action.i
enters Insert mode at the point in the file
where the cursor is postioned. Synonyms: INS
o
enters Insert Mode by inserting a new blank line
beneath the line the cursor is currently on. Upon entering insert
mode, the cursor is positioned at the beginning of the newly
inserted blank line.O
enters Insert Mode by inserting a new blank line
above the line the cursor is currently on. Upon entering insert
mode, the cursor is positioned at the beginning of the newly
inserted blank line.p
pastes the most recently deleted or copied text
at the position after the cursor. Linewise cuts and copies will
always paste entire lines, splitting the current line to the right
of cursor, and pasting complete lines between the two
sections.p
pastes the most recently deleted or copied text
before the character under the cursor. Linewise cuts and copies
will always paste entire lines, splitting the current line at the
character under the cursor, and pasting complete lines between the
two sections.a
enters Insert mode just after the character
under the cursor.A
enters Insert mode at the end of the current
line.d<motion>
deletes from the cursor to the end
of the motion specified. If the motion crosses multiple lines, all
lines involved are deleted in their entirety, linewise. Deleted
text is stored for later pasting.D
deletes from the cursor to the end of the
current line. Deleted text is stored for later pasting.dd
deletes the current line linewise.gg
moves the cursor to the very beginning of the
file.gq<motion>
formats the motion specified (use
'ap' as a motion to indicate the entire paragraph, being a section
of text from the last blank line to the next blank line) to fit the
current maximum column width (usually 80, set with :set
tw=80
) obeying initial indentation according to the first
line of the paragraph.G
moves the cursor to the first column on the last
line of the file.h
moves the cursor one character to the left.
Synonyms:Left
j
moves the cursor one line up.
Synonyms:Up
J
joins the line after that which the cursor is on
to the end of the current line, stripping any of the precedeing
white space on the lower line.k
moves the cursor one line down.
Synonyms:Down
K
displays the man page for the word under the
cursor.l
moves the cursor one character to the right.
Synonyms:Right
x
deletes the character under the cursor, storing
it for later pasting. Synonyms: DEL
X
deletes the character to the left of the cursor,
storing it for later pasting. Synonyms: BACKSPACE
c<motion>
deletes the motion, and goes into
Insert Mode, thus allowing the 'changing' or replacement of a
section of text.C
deletes from the cursor to the end of the
current line, and goes into Insert Mode.v
starts visual mode.V
starts line oreiented visual mode.CTRL-v
start rectangular or block visual
mode.b
moves the cursor to the beginning of the current
word (alphanumeric) or the beginning of the previous word if
already at the beginning of the current word.B
moves the cursor to the beginning of the current
word (whitespace) or the beginning of the previous word if already
at the beginning of the current word.n
searches for the next (in the direction of the
current search) occurence of pattern.{
moves the cursor to the beginning of the current
paragraph. When editing code, depending on the language, often
jumps to the beginning of the code block.}
moves the cursor to the end of the current
paragraph. When editing code, depending on the language, often
jumps to the end of the code block./
prompts for a pattern for which to search, and
searches forwards for the next occurence of it in the file.?
prompts for a pattern for which to search, and
searches backwards for the next occurence of it in the file..
performs the last action again.One of the cooler things about Vim is the ability to specify counts.
We can for example specify that one of the above actions indicated by
keystroke be performed multiple times by prepending the keystroke with
a number. For example if we wanted to insert exactly 72 '*' characters,
instead of going into Insert Mode and counting, we simply type
72i*<ESC>
. Similarly we can replace the next 7
characters with 'z's (7rz
), etc...
Also, in Command Mode, we have the ability to enter more complex commands by using the ':' key. The ':' key present us with a command prompt at the bottom left of the screen (':'), where we can type more complex commands, some of which are
:q
quits Vim if there are no modified
buffers.:w [<filename>]
writes the contents of the
current buffer to the filename given, if one is given, otherwise
back to the file from which the buffer was loaded.:e <filename>
opens a new buffer and loads
the file specified into it for editing. If a buffer already exists
for that file, then jumps to that buffer. If the current buffer has
not been saved, an error message is produced, and the command does
nothing.:read <filename>
reads the contents of the
file specified and inserts them into the buffer after the current
line, linewise.:help [<command>]
starts the help system,
either at the table of contents if no command is specified, or at
the page for the command given.:buffers
lists all the buffers open in Vim.:bn
switches to the next buffer if the current
buffer hasn't been modified since the last save.:bp
switches to the previous buffer if the current
buffer hasn't been modified since the last save.:bd
removes the current buffer, i.e. stops editing
the current file, if the buffer hasn't been modified since the
save.:s/<search pattern>/<replace
pattern>/<options>
searches for the first
occurence of pattern on every line within the given range (or
the current line only, if no range is given) and replaces it
with the replacement pattern. Options can be
g
replace every pattern on a line, not
just the firstc
prompt for confirmation before repacing
each pattern.:source [<filename>]
loads and runs a script
of vim commands from the file specified. This is how vim plugins
are loaded.:!<shell command>
runs the shell command
specified. If a range is given, the lines speicifed in the range
will be piped to the shell command, and it's output will replace
those lines.You will have noticed the concept of range in some some of the
commands above. In general, we can specify that a particular command
should be applied only to a certain range of lines, by entering that
range as a pair of line numbers, comma separated as in <from
line>,<to line>
. '%' reflects the entire file as a
range. Type ':help range' for more details, and there are lots more
details.
Insert Mode acts just as any other editor. What you type gets put into your file. The arrow keys work as normal, delete and backspace work as normal. The only two keys you need to worry about are Escape and Insert. Escape takes you back to Command Mode. Insert will toggle between Insert proper and Replace or Overwrite Mode. When in Insert Mode you will always see the message "-- INSERT --" or "-- REPLACE --" displayed at the bottom left of the screen, depending on whether you are inserting or overwriting.
A useful shortcut when in Insert Mode is CTRL-X,CTRL-f
attempts to complete the word under the cursor as if it were a
filename. The first available completion is used. You can cycle though
available completions by holding down CTRL and pressing 'f' repeatedly.
In versions 7 and higher of Vim, a little drop down menu is actually
displayed when this happens.
From Command mode one can highlight or select text to operate on using the visual ro selection modes, of which there are three. All oF them operate by highlighting from the cursor position when the mode is entered to the current cursor position, and show the message '-- VISUAL --' in the bottom left corner of the screen when we are in them. Exiting the mode by pressing Escape aborts the selection. Essentially the visual modes allow us to specify a range for ':' commands, or keystroke commands from command mode to work on. The three visual modes are
To exit any visual mode without doing anything we simply press Escape. However, we can also issue any of the commands from command mode when in visual mode. The command specified will be executed and then we will be returned to command mode. If that command operates only on entire lines, then all the lines selected, even if only partially selected, will be operated on. Otherwise the interactions of commands and selections are to lengthly to describe, although they tend to be resonably intuitive. All I can say is experiment.
The first place to get help for Vim is in Vim itself, using the
:help
command. Go though the tutorial at least once, and
learn how to navigate the help system. Alternatively, an internet
online copy is maintained at http://vimdoc.sf.net. Other places
include your local Vim guru, who may charge you in pizza or drinks
(caffeinated or alcoholic). Finally, there's a whole collection of Vim
gurus who sit on the #vim IRC channel on freenode.org. If you don't
know how to use IRC, learn!
I have written a plugin for Vim (shameless punting here, I know!) that makes Vim act like an IDE for the purposes of debugging python code. It's still under development, but is pretty stable, and should work fine. I use it regularly, without any issues. You can download it from here. Read the quick start file and go from there.