Getting Started with Vi

Even if you use a different editor for most of your work, it helps to know the basics of the ubiquitous vi.

Most people who are getting started with Linux or UNIX today already know how to use graphical WYSIWYG (what you see is what you get) editors, with heavy use of the mouse, icons and pull-down menus. When they first encounter the traditional UNIX/Linux editor, vi, it may seem awkward and not very powerful, but the opposite is true.

As with graphical editors, you can select a range of text and execute a command to make changes, move the cursor by indicating where you want it to be and insert text by typing at the keyboard. The difference is that vi doesn't use the mouse; you use the keyboard to specify changes or move the cursor. In order for the entire keyboard to be available for commands, vi has separate command and insert modes.

Modes can be confusing at first. With most editors, whatever you type always goes into the document, and editing is done using a mouse or perhaps keyboard shortcuts. With vi, command mode uses the whole keyboard to specify editing commands. Only when you issue an insert command does the keyboard add text to the document.

Most new vi users learn only a few basic commands: the arrow keys, i for insert, x to delete a character, dd to delete a line, :wq to save and exit and perhaps a few others. They quickly grow tired of leaning on the same keys over and over but are afraid of learning a huge list of commands.

Instead of merely providing a list to memorize, here we explore the structure behind them, which makes the commands easier to remember and allows you to become a vi power user quickly. There are several basic categories of commands, including inserting text, moving the cursor, block edit commands, colon (ex) commands, options and miscellaneous commands.

Inserting Text

The first command to learns in vi is i. Typing i puts the editor into insert mode, where everything you type is inserted into the document until you press the Esc key. But i is not the only way to insert text.

To add text after the cursor, such as at the end of the line, use a. Or jump to the end of the line and append text with A. Similarly, I inserts at the beginning of the line. With these and other insert commands, always press Esc to return to command mode.

Use o to open a new line after the current one and insert text there or O to open before the current line.

Replace the current character with s, or replace many characters using R, similar to overwrite mode in a graphical editor.

Nearly all vi commands allow a numeric prefix, which usually repeats the command a specified number of times. For example, type 3iHooray then press Enter and Esc. It inserts three lines of “Hooray” into your buffer. The s command is different; here, the number indicates how many characters are to be replaced: 5s replaces five characters with whatever you type.

A few control keys have special meaning in insert mode. You can use the arrow keys to move around, but don't forget you're still in insert mode. Also, Ctrl-T and Ctrl-D, at the start of a line, can be used to indent. Ctrl-T moves to the next level of indentation, and Ctrl-D moves back. Use these with the autoindent and shiftwidth options (described in the Options section below) for best results.

Moving the Cursor

Graphical editors move around mainly by clicking the mouse. Some vi clones offer mouse support, but vi and all its clones share movement commands that can move you around quickly. The simplest are the arrow keys or the equivalent letters: h (left), j (down), k (up) and l (right). If these are all you use, however, you are missing out on the full power of vi.

You can move forward and back on a line by holding down the right or left arrow key (or l or h), but there are easier ways. Go to the beginning of the line with ^ or the end with $. To jump one word at a time, use w or W to go forward or b or B to go back. You also can use e or E to jump to the end of the current word. For lowercase w, b and e, a word is a sequence of alphanumeric characters; the uppercase versions use spaces to separate words.

Another way to move around on the current line is to jump to a particular character. For example, to move the cursor to the next s on a line, type fs. Use F for the previous one: Fs. Or use ts to go right before the s and Ts to go the other way. Change s to search for any character. Type , (comma) to repeat any of these searches.

To move the cursor to the top, middle or bottom of the screen, use H, M or L (uppercase). For H or L, a numeric prefix indicates how many lines from the top or bottom of the screen you want to go; it has no effect on M.

To jump back and forth by sentences, type ( or ); for paragraphs, use { and }. If you're editing source code with a lot of brackets, try the % command: put the cursor on any of the following characters: (, ), [, ], { or }, and press %; you'll be taken to the matching bracket.

Often the best way to move around is by searching with the / command. For example, type /hello and press Enter to jump to the next “hello” in the document. Instead of “hello”, enter any regular expression. The n command repeats the search; N goes the other way. Or use ? to search backward in the file. Search is case-sensitive; I discuss the ignorecase option later in this article.

With most movement commands, a numeric prefix can be used to repeat. For example, 5w moves you forward five words, 2n jumps not to the next search match, but to the one after that, and 5fs jumps to the fifth s to the right of the cursor.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I hate vi

Anonymous's picture

Why on earth would anyone ever use this now? Basic text editors much more intuitive than vi have been available for over a decade. Why hasn't this abortion of a text editor been put to rest? There is no reason to even learn it except for some reason, a lot of Linux certification tests have vi questions in them.

Why in God's name does this text editor have it's own command line? It's a TEXT EDITOR. jed and kate are more powerful for crying out loud. Why should I feel like I should to learn this cumbersome piece of crap?

"Even if you use a different editor for most of your work, it helps to know the basics of the ubiquitous vi." <==WHY?