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.
Block Edit Commands

In graphical editors, to change a block of text, click and drag the mouse to highlight the text, then click an icon or menu option or type a keyboard shortcut. Although vi does not utilize the mouse, the process is surprisingly similar.

As we have just seen, vi has a rich set of movement commands. Any of these will work as an argument to a block editing command (for example, dw to delete a word). In vi, instead of first highlighting with a mouse and choosing an action, type the block edit command (d) first, and then a movement command (w) to specify the block of text. As a shortcut, you can type the block edit command twice to affect only the current line, for example, dd deletes the current line.

As we've seen, d is for delete. Whenever you delete text, that text is remembered, much like the cut command in a graphical editor. To paste the text (“put” in vi parlance), type p to put it after the cursor's position (or uppercase P for before).

That takes care of cut and paste, but what about copy? In vi, it's called yank and is done with the y commands. These are exactly like the d commands, except they don't delete anything: yy yanks the current line, yw yanks a word and so on.

To replace a chunk of text, you could delete it first and use i to insert a replacement, or use c (change) instead, and you are placed into insert mode to type the replacement text; press Esc when done. Use with a movement command (such as, cw) or change the whole line with cc.

Programmers will love the > and < commands. These shift a range of text to the right or left. To shift a block of code, put the cursor on the { or } character at the beginning or end of the block and type >% to shift it to the right. Any other movement command can be used in place of %, or use >> to shift one line. Use < to shift left. See also Ctrl-T and Ctrl-D (while in insert mode) and the shiftwidth option below.

Lastly, use ! to filter text through an external program. Type the program to run, and the selected part of the file is passed as standard input; the output of the program is inserted into the document in its place. For example, !! filters the current line; !} filters a paragraph. One command that is particularly useful with this is the UNIX/Linux filter fmt, which reformats paragraphs. To run an external program without modifying the file, see the :! command in the “Colon (ex) Commands” section.

Any numeric prefix given is passed along to the movement portion of the command, whether it is given first or between the edit and movement command. For example, either 3dw or d3w deletes three words; 3yy or y3y yanks (copies) three lines.

Colon (ex) Commands

Vi inherits as class of commands from its predecessor ex, accessible through what is sometimes called colon mode. Common examples are :wq (write and quit) and :s/X/Y/ (substitute Y for X). Type : followed by an ex command, and press Enter. After the command is done, vi returns to command mode.

There are several variations on the :w and :q commands. To save the file and continue editing, use :w. You can quit vi with :q, but only if your work is saved; to quit no matter what, use :q! (dangerous). Try :w! to override read-only mode if :q gives an error. Many people use :wq! to write and exit. In vim or nvi, this is like typing :w! and then :q if there was no error while saving. But some other versions of vi treat it like :w and :q!, which means if there was any error saving the file, the work would be lost. Because of this, it is a better habit to use :wq.

The substitution command is extremely powerful. In its simplest form, :s/X/Y/ replaces one occurrence of X with Y on the current line. To replace all occurrences on a line, add g (global) to the end, for example, :s/foo/bar/g. Add c to the end to confirm each change, for example, :s/foo/bar/gc. To change a range of lines, put two line numbers separated by a comma in front, for example, :10,20s/foo/bar/g replaces all occurrences of “foo” with “bar” on lines 10 through 20. Use 1 for the first line of the file and $ for the last line; to replace on all lines, use :1,$s/foo/bar/g (instead of 1,$, you can use %, as in :%s/foo/bar/g).

Another useful command is :g/X/. It allows you to run any ex command for all lines matching the pattern X. For example, to print all lines that contain “hello” on the screen, use :g/hello/p (:p is an ex command for printing lines to the screen). You even can combine it with the :s/// command. To change “foo” to “bar” on all lines that contain “hello”, use :g/hello/s/foo/bar/g.

In both :s/X/Y/ and :g/X/, the expression X is a regular expression, not only a static string. Set the nomagic option to disable this (see below). To learn about regular expressions, I recommend the book Mastering Regular Expressions by Jeoffrey Friedl (O'Reilly & Associates, 2nd ed., 2002).

Use :! to run an external program. :!ls prints the current directory listing. As a convenience, the percent character (%) in the command is replaced with the name of the file you are currently editing, so use :!chmod +x % to make the file executable. Typing :!! repeats the previous :! command. Unlike the command-mode ! commands, this does not modify the text; the output of the program, if any, is simply displayed.



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?