Introduction to VI, the Unix Text Editor

A 'How To' Guide for the Nanopublisher

Overview

Vi has two modes, command mode and insert mode. It starts in command mode. In this mode everything typed is treated as a command. You enter insert mode using i, a, o, O, or s commands. To enter command mode from insert mode, hit “esc”.

Motion commands

h move one character to the left
j move one line down
k move one line up
l move one character to the right (the keys h, j, k, and l were chosen for the basic motions for efficiency rather than because the letters have some numonic value (they are on the home row of the keyboard, under your right hand)
/text search down for text
?text search up for text
n repeat previous search
N repeat previous search in the reverse direction
fc find character c (this places the cursor on first occurrence of c to the right of the original position of the cursor)
tc move to character c (this places the cursor just before first occurrence of c)
^ move to start of current line
$ move to end of line
enter move to the start of the next line
- move to the start of the previous line
w move to next word
e move to the end of the current word
G go to the last line of the file
1G go to the first line of the file
nG go to line n
% go to matching bracket
H go to top of screen
M go to middle of screen
L go to bottom of screen
'c go to the line that contains the mark associated with the letter c (c can be any letter from a-z or A-Z)
`c go to the mark associated with the letter c (c can be any letter from a-z or A-Z)
'' (hit the single quote twice) go to the line of the previous cursor location
`` (hit the single back quote twice) go to the previous cursor location

Action commands

Action commands are combined with motion commands to operate on a region of text or they are entered twice to act on a line. Examples include cw=change word, dw=delete word, y$=yank to end of line, c/foo<CR>=change to foo, dtc=delete to character c. Action commands also have their 'whole line' form, where the basic command is typed twice (such as dd, cc, or yy), and their 'to end of line' form, where you use the capital letter version of the basic command. These last two forms cannnot be combined with a motion command.

d delete
c change
y yank (copy into buffer for later pasting)

Other comands

i insert text before current position of cursor
a append text after current position of cursor
o open up a new line and insert text below the current line
O open up a new line and insert text above the current line
dd delete line
D delete from cursor to end of line
cc change line
C change from cursor to end of line
yy yank line
Y yank line (yes this is inconsistent, either accept it or remap it using ":map Y y$")
p paste to right of cursor
P paste to left of cursor
x delete character
xp swap characters
. repeat last change
mc set the location of the mark associated with the letter c (c can be any letter from a-z or A-Z)
ctrl g display current line number
ctrl f scroll forward one screen
ctrl b scroll backward one screen
ctrl d scroll down half a screen
ctrl u scroll up half a screen
u undo last change
U undo all changes on current line
crtl r redo the last change to be undone
rc replace current character with c
stext substitutes current character with text. Terminate with esc.
:%s/find/replace/g find every occurrence of find and replace it with replace
:w write out file
:w file write out to file
:q quit
:q! discard changes and quit
:e! reload file discarding any change

Indenting

:se ai enable autoindenting (use noai to disable)
:se sw=4 set shiftwidth to 4 spaces
ctrl t indent one shiftwidth
ctrl d unindent one shiftwidth

Additional Resources

More information can be found at here. You can learn about Vi and Vim (an improved version of Vi) with the following books. Clicking on the images on the right takes you to Amazon, where you can learn more about them.

Any questions or comments on these notes can be directed to theNurds@nurdletech.com.

Analog Verification