Last modified: May 15, 2018

Twelve Useful "vi" Commands

Introduction

The editor of choice for system administration tasks, as well as for many users, on Unix and Unix-like systems is "vi". In fact, I insist on its use in SENS when editing system files, because it's small, fast, and reliable. While it seems a bit hard to learn, there is incredible power buried in it.

This page shows a subset of vi commands that I use regularly. There are many more goodies buried in it, and I recommend reading one of the available online manuals for more information (see the "References" section at the end of this page for several examples). It's also important to note that vi has its foundations in the line-based "ex" editor, and knowledge of ex is also useful.

Oh, and I'd like to state right away that it's pronounced "vee-eye," not "vie," and definitely not "six".

The main thing to remember is that it has two different modes of operation, unlike most other editors where you just start typing. The first mode is "command mode," where every key on the keyboard performs a command; this is often called "beep mode," because people can't figure out why everything they type causes the editor to beep. The other main mode is "insert mode," where keys actually input the characters printed on them. Minor modes, such as "overwrite mode," are manifestations of insert mode, and will be treated as such in this document.

It can be useful to think of vi commands as sequences consisting in many cases of a verb and a noun. For example, to change a word one uses the command sequence "cw", where the "c" is the "change" operator and the "w" is an object representing a word, so read it as "change word."

So, with that introduction out of the way, let's get started. You've just typed "vi document" on the command line, pressed return, and now you either have a screen showing the first few lines of an existing document, or if you're starting a new document you have a blank screen with a bunch of tilde ("~") characters on it (those stand for non-existent lines, by the way). Now what?

1. h, j, k, and l — Moving Around

Like most full-screen editors, vi allows you to move the cursor to any spot in your text. On most newer Unix systems (and workalikes), the arrow keys will move the cursor, but I strongly urge the use of the traditional keys:

These keys are a legacy of the Lear-Siegler ADM-3 terminals used on early Unix systems. However, because they are on the "home row", they provide a fast way to move around, especially if you are a touch typist. I think that if you give them a chance, you'll agree that they are faster than repeatedly moving your right hand off of the keyboard to access the arrow keys.

2. Esc - Escape

This deserves to be by itself. Pressing the Esc (Escape) key is how you get out of text input modes and back into command mode. It's also how you quit out of a multi-key command sequence you've started to type, but decided not to complete.

3. i, I, a, and A — Insert and Append Text

Pressing the i key puts the editor into insert mode. Now, any character you type will be entered into the document, at the point before the current cursor position. When you are done, press the Esc key to return to command mode.

Upper-case I does the same thing, but moves the cursor to the start of the current line first. You'll find that many vi commands have upper-case commands that are extended versions of the corresponding lower-case command.

The a key appends characters to the current text. It's a variation of i, putting the editor into insert mode after the character under the cursor, instead of before it like insert does.

Upper-case A does the same intuitive thing that I does, moving the cursor to the end of the current line and putting the editor into append mode.

4. o and O — Open a new line

The o key will open a new line below the line that the cursor currently resides on. All lines below it are bumped down, and the editor is put into insert mode. Again, to return to command mode, press the escape key.

Upper-case O does the same thing, except it opens a new line above the current line.

5. y, Y, p and P — Yank and Pop

"Yank" and "pop" are vi's equivalent of "copy" and "paste". This is where we'll introduce a couple of new concepts: Numeric arguments to specify a range, and objects to specify on what "thing" the command is to act.

The y key expects a second keystroke to specify what it is supposed to yank: l yanks characters (read it as "letters"), w yanks words, and a second y yanks the entire line (in general, with an object-based command, typing the same letter twice acts on the entire line). Upper-case Y is a synonym for yy, by the way, and yanks the current line or lines.

Here are some examples:

3yy or 3Y
Yanks three lines (the current line and the next two).
y2w or 2yw
Yanks two words; the examples are equivalent, but I find the first a bit more intuitive (think of it as "yank two words").
yl
Yanks the current character (letter). This is useful if you need to copy a character that is not easily entered on the keyboard, such as a Unicode character.

By contrast, "put" is a lot simpler: If the last thing you did was a line-based yank, p puts the contents of the yank buffer after the current line, and P puts the contents before the current line. If the last yank operation was based on words or characters, then p puts the contents of the yank buffer after the current cursor position, and P puts the contents before the current cursor position.

6. d, D, x and X — Delete Stuff

Just like the p operator above, the d (delete) operator is a verb that takes an noun as a second keystroke. So, dw deletes a word and dl deletes a letter (character).

A capital D deletes from the current cursor location to the end of the line.

To delete the entire current line, use dd.

Deleting characters is such a common activity that a shortcut (or macro, if you prefer) for dl, x, was created for this purpose. A complementory shortcut, X (capital "X"), deletes the character before the cursor, not after.

7. c, C, s, and S — Changing and Substituting Text

Same idea as the above, as far as verbs and nouns go. The c (change) operator is a verb that takes an noun as a second keystroke. So, cw changes a word and cl changes a letter (character).

A capital C changes from the current cursor location to the end of the line.

To change the entire current line, use cc.

The shortcut s does the same thing as cl, and changes the character under the cursor (think "substitute" for "s"). Another shortcut, S (capital "S"), changes the entire line, just like cc.

8. r and R — Replace Text

The r command replaces a single character under the cursor with another single character. It does not put the editor into open mode like change or substitute.

The R command replaces multiple characters starting at the current cursor position and continuing as long as you type.

9. u, U and . — Undo and Repeat

u is "undo", which is probably one of the best commands in vi. Very simply, it undoes the last thing you did. Note that in classic "vi" it only undoes one prior change, and using again will undo the undo, putting the text back the way it was before undo was used the first time. In some newer versions of vi, such as "nvi" and "vim", there are multiple levels of undo and repeatedly pressing u will go back through them. For those versions, control-R will "redo" an undo.

U will undo all changes made to the current line, as long as no changes have yet been made to other lines.

. is the "repeat" operator. It will repeat the previous action, except for cursor motion commands. This is extremely handy.

It's important to note that it repeats actions, not actual text changes. For example, if I use ~ to change a lowercase "a" to an uppercase "A", then move to a "t" and hit ., it will replace the "t" with a "T", not an "A", because we're repeating the command, not the result.

10. More Moving Around

Note that w and b use punctuation to delimit word boundaries. To use whitespace only as word boundaries, use W and B, respectively.

11. /, ?, n, and N — Find Text

The / operator searches forward for matching text, and ? searches backward.

The n operator repeats the last search in the same direction, and N repeats the last search but goes in the opposite direction.

12. ZZ, :w, and :q — Saving and Quitting

The ZZ command saves the file and exits the editor.

The ex command :w writes (saves) the file but leaves the editor open.

The ex command :q quits (exits) the editor.

To force exiting or saving, use an exclamation point ("!") after the command, eg. :q! to force quitting, even if the file has not been saved.

Conclusion

The vi editor is extremely powerful. There are other features that were not discussed here, such as named buffers ("a through "z), the underlying "ex" editor that can be used to perform global search-and-replace functions (:%s/foo/bar/g), and many other cool things.

References

Made with CSS   Valid XHTML 1.1!   Valid CSS!