![]() |
:q!vi has possibly one of the most unintuitive interfaces, and for the user who's used to graphical editors, the first thing they want to do is to quit and return to safety. :-) I heard stories of users who quit vi by rebooting their system! Of course, that doesn't help get your work done. The rest of this primer will, I hope, give you a slightly bigger picture. The sections below feature commands you will want to type.
iMost users want to, of course, be able to add stuff to the file they're editing (that being one of the main uses of an editor). You can't just type straight away--when you start vi, you're in what may be called `command mode' where you enter commands. Type the letter i (for insert), in lowercase. This tells the editor that you want to insert text into your file (more precisely the `editing buffer' since changes are not written back to the file unless explicitly told to; see the section on dealing with files). Just start typing from here on.
<Esc>When you're in insert mode, hitting the `escape' key will get you back into command mode. Think of it as an escape from insert mode.
aYou'll probably realise that the direction keys will not bring the cursor beyond the last character in the line. Typing the letter a (for append) will put you in insert mode just to the right of the cursor. If you don't want to have to move to the end of the line to add something to the end of the line, type A in capitals. This will get you to the end of the current line, no matter where in the line you were.
w
bTyping w in command mode moves you to the start of the next word, and b does the reverse.
$Typing gets you to the beginning of the line, and $ to the end. If you are familiar with regular expressions (outside the scope of this document) you'll find them easy to remember.
<Ctrl>U
<Ctrl>DControl-U and Control-D move about half a screen up or down, respectively.
:1
:$:1 gets you to the first line in the buffer. (It should be obvious by now that you can replace 1 with any line number you care to choose.) :$ gets you to the last line in the buffer.
I
AIf you're inserting text at the beginning or end of lines, the command I is similar to i (insert at beginning of line), and A is similar to $a (append at end of line).
x
XIn insert mode, you can delete text that you've typed in the current line, in the current insertion. That is, if you hit the escape key and re-enter insert mode, you can't delete what you've typed previously by hitting the backspace key. One way to delete text, a character at a time, is to get into command mode and type either x or X the lower case version deletes the character at the cursor (and behaves similarly to the `delete' key in graphical editors) and the upper case one deletes the character just before the cursor (and behaves similarly to the `backspace' key in graphical editors).
d<motion>Typing the letter d, followed by a motion command (like h or l, or more usefully w) deletes text from the cursor to the target of the motion command. For example, dw deletes from the cursor to the start of the next word.
dd
:dBoth of these commands delete the whole line. To delete a whole bunch of text I just hold down the d key until I'm satisfied.
:jThe :j command joins the current line with the next. A single space is added to the end of the first line, unless the first line ended with a full-stop, in which case two spaces are added. You split a line the conventional way go into insert mode, and hit the return key.
/<pattern>This will search from the current cursor position, through to the end of the buffer, for <pattern> which is a regular expression (again, in most cases you can just type in plain text). ?<pattern> Searches backwards from the cursor position to the beginning of the buffer for the pattern.
The colon commands (or more precisely called `ex commands', because they are used in the ex(1) editor) work with the notion of `ranges', which come just after the colon. Ranges are best described by examples. The :1 and :$ commands mentioned earlier are not really commands, but line specifications, which stand for the first line and the last line respectively.
In addition, the dot . stands for the current line. You can offset this by a certain number of lines by using + or - followed by a number. For example, $-3 refers to three lines up from the end, and .+5 refers to the fifth line down from the current one.
A range is specified by two line specifications separated by a comma. For example, 2,5 means between lines 2 and 5, including lines 2 and 5.
The percent symbol % is a shortcut for 1,$.
And now, some real-life examples.
.The dot . command repeats the last command you did that actually changed the buffer. Movement commands do not count.
same as doing nothing at all (it just undid your undo). The Berkeley vi implementation, nvi, allows multiple level undo by typing dots (it repeats your undo). Most cabbage vi implementations, though, repeat your last command (before your undo) if you type dots.