Editing text files

Although there are many editors and word processors available for Linux, the vi (visual) editor is the standard editor that is available on every Unix and Linux system. You may find vi difficult to learn and use at first, but you will come to appreciate its many powerful features.

The version of vi often found on Linux systems (including ours) is a derivative named vim (vi improved)invoked by typing either vi or vim at the prompt.

The key to using vi is to remember that it was developed before cursor controls like the arrow keys, Page Up/Down keys, and other editing keys were commonly available on keyboards. To work around this, all forms of vi define three different operating modes in addition to any extensions that may have been added:

Therefore each key on the keyboard can have three entirely different meanings, and some have more with various shift combinations. Try both p and P for pasting, for example, in command mode.

When you first start vi, you are in command mode. Press i to enter text input mode and type your material. Return to command mode by using the ESC key to end insert mode. Make changes or corrections from input mode as needed. When you are finished, go to edit mode to save your file and exit. Use :w to write your file to disk, or :w <filename> if it needs a new name, and :q to exit. You may combine :w and :q as :wq.

The diagram below illustrates methods of moving between each mode, and some of the keys available in each mode.






Some basic commands for vi

These basic commands are enough to get you started and to complete most simple editing tasks.

Adding new text (from command mode into insert mode)

i insert new text in front of cursor

o open a new line after (below) cursor

a insert new text after cursor

O open a new line before (above) cursor

A insert new text at the end of the line


Moving around without the arrow keys (within command mode)

k move backwards (up) one line

h move backwards one character

l (lower case L) move forward one char

j move down (forward) one line

^ move to the start of a line

$ move to the end of a line

Editing text (from command mode)

x delete one character (under cursor)

dd cut (delete) current line

r replace character (under cursor)

yy copy (yank) current line

dw delete one word (containing cursor)

p paste most recently cut/copied text

cw change word (from cursor)

u undo last editing command

Search and Replace (from ed mode)

/text search forward for next "text"

?text search back for prior "text"

:s/old/new/ replace old with new once on the current line

:s/old/new/g replace all occurrences of old with new on current line only

:3,$s/old/new/g replace all old with new on all lines from 3 to $ (end of line)

Save and Exit (from ed mode; there are more ways)

:w filename write as filename

:q quit, exit, leave vi/vim

:w write using existing filename

:q! quit, discarding changes

You will often find vi commands in other places as well, either derived from vim itself or often from a common ancestor program. For example, the / and ? search commands work in less and man, and a close relative of :s works in sed.

The use of h, j, k, l is guaranteed to move the cursor on all compatible editors derived from vi, but almost everyone uses the arrow keys.

Try the tutorial (look at :help) in vim for more useful techniques.