Chapter 22. Introduction to vi

Table of Contents

command mode and insert mode
start typing (a A i I o O)
replace and delete a character (r x X)
undo and repeat (u .)
cut, copy and paste a line (dd yy p P)
cut, copy and paste lines (3dd 2yy)
start and end of a line (0 or ^ and $)
join two lines (J) and more
words (w b)
save (or not) and exit (:w :q :q! )
Searching (/ ?)
replace all ( :1,$ s/foo/bar/g )
reading files (:r :r !cmd)
text buffers
multiple files
abbreviations
key mappings
setting options
practice: vi(m)
solution: vi(m)

The vi editor is installed on almost every Unix. Linux will very often install vim (vi improved) which is similar. Every system administrator should know vi(m), because it is an easy tool to solve problems.

The vi editor is not intuitive, but once you get to know it, vi becomes a very powerful application. Most Linux distributions will include the vimtutor which is a 45 minute lesson in vi(m).

command mode and insert mode

The vi editor starts in command mode. In command mode, you can type commands. Some commands will bring you into insert mode. In insert mode, you can type text. The escape key will return you to command mode.

Table 22.1. getting to command mode

keyaction
Escset vi(m) in command mode.

start typing (a A i I o O)

The difference between a A i I o and O is the location where you can start typing. a will append after the current character and A will append at the end of the line. i will insert before the current character and I will insert at the beginning of the line. o will put you in a new line after the current line and O will put you in a new line before the current line.

Table 22.2. switch to insert mode

commandaction
astart typing after the current character
Astart typing at the end of the current line
istart typing before the current character
Istart typing at the start of the current line
ostart typing on a new line after the current line
Ostart typing on a new line before the current line

replace and delete a character (r x X)

When in command mode (it doesn't hurt to hit the escape key more than once) you can use the x key to delete the current character. The big X key (or shift x) will delete the character left of the cursor. Also when in command mode, you can use the r key to replace one single character. The r key will bring you in insert mode for just one key press, and will return you immediately to command mode.

Table 22.3. replace and delete

commandaction
xdelete the character below the cursor
Xdelete the character before the cursor
rreplace the character below the cursor
ppaste after the cursor (here the last deleted character)
xpswitch two characters

undo and repeat (u .)

When in command mode, you can undo your mistakes with u. You can do your mistakes twice with . (in other words, the . will repeat your last command).

Table 22.4. undo and repeat

commandaction
uundo the last action
.repeat the last action

cut, copy and paste a line (dd yy p P)

When in command mode, dd will cut the current line. yy will copy the current line. You can paste the last copied or cut line after (p) or before (P) the current line.

Table 22.5. cut, copy and paste a line

commandaction
ddcut the current line
yy(yank yank) copy the current line
ppaste after the current line
Ppaste before the current line

cut, copy and paste lines (3dd 2yy)

When in command mode, before typing dd or yy, you can type a number to repeat the command a number of times. Thus, 5dd will cut 5 lines and 4yy will copy (yank) 4 lines. That last one will be noted by vi in the bottom left corner as "4 line yanked".

Table 22.6. cut, copy and paste lines

commandaction
3ddcut three lines
4yycopy four lines

start and end of a line (0 or ^ and $)

When in command mode, the 0 and the caret ^ will bring you to the start of the current line, whereas the $ will put the cursor at the end of the current line. You can add 0 and $ to the d command, d0 will delete every character between the current character and the start of the line. Likewise d$ will delete everything from the current character till the end of the line. Similarly y0 and y$ will yank till start and end of the current line.

Table 22.7. start and end of line

commandaction
0jump to start of current line
^jump to start of current line
$jump to end of current line
d0delete until start of line
d$delete until end of line

join two lines (J) and more

When in command mode, pressing J will append the next line to the current line. With yyp you duplicate a line and with ddp you switch two lines.

Table 22.8. join two lines

commandaction
Jjoin two lines
yypduplicate a line
ddpswitch two lines

words (w b)

When in command mode, w will jump to the next word and b will move to the previous word. w and b can also be combined with d and y to copy and cut words (dw db yw yb).

Table 22.9. words

commandaction
wforward one word
bback one word
3wforward three words
dwdelete one word
ywyank (copy) one word
5ybyank five words back
7dwdelete seven words

save (or not) and exit (:w :q :q! )

Pressing the colon : will allow you to give instructions to vi (technically speaking, typing the colon will open the ex editor). :w will write (save) the file, :q will quit an unchanged file without saving, and :q! will quit vi discarding any changes. :wq will save and quit and is the same as typing ZZ in command mode.

Table 22.10. save and exit vi

commandaction
:wsave (write)
:w fnamesave as fname
:qquit
:wqsave and quit
ZZsave and quit
:q!quit (discarding your changes)
:w!save (and write to non-writable file!)

The last one is a bit special. With :w! vi will try to chmod the file to get write permission (this works when you are the owner) and will chmod it back when the write succeeds. This should always work when you are root (and the file system is writable).

Searching (/ ?)

When in command mode typing / will allow you to search in vi for strings (can be a regular expression). Typing /foo will do a forward search for the string foo and typing ?bar will do a backward search for bar.

Table 22.11. searching

commandaction
/stringforward search for string
?stringbackward search for string
ngo to next occurrence of search string
/^stringforward search string at beginning of line
/string$forward search string at end of line
/br[aeio]lsearch for bral brel bril and brol
/\<he\>search for the word he (and not for here or the)

replace all ( :1,$ s/foo/bar/g )

To replace all occurrences of the string foo with bar, first switch to ex mode with : . Then tell vi which lines to use, for example 1,$ will do the replace all from the first to the last line. You can write 1,5 to only process the first five lines. The s/foo/bar/g will replace all occurrences of foo with bar.

Table 22.12. replace

commandaction
:4,8 s/foo/bar/greplace foo with bar on lines 4 to 8
:1,$ s/foo/bar/greplace foo with bar on all lines

reading files (:r :r !cmd)

When in command mode, :r foo will read the file named foo, :r !foo will execute the command foo. The result will be put at the current location. Thus :r !ls will put a listing of the current directory in your text file.

Table 22.13. read files and input

commandaction
:r fname(read) file fname and paste contents
:r !cmdexecute cmd and paste its output

text buffers

There are 36 buffers in vi to store text. You can use them with the " character.

Table 22.14. text buffers

commandaction
"adddelete current line and put text in buffer a
"g7yycopy seven lines into buffer g
"appaste from buffer a

multiple files

You can edit multiple files with vi. Here are some tips.

Table 22.15. multiple files

commandaction
vi file1 file2 file3start editing three files
:argslists files and marks active file
:nstart editing the next file
:etoggle with last edited file
:rewrewind file pointer to first file

abbreviations

With :ab you can put abbreviations in vi. Use :una to undo the abbreviation.

Table 22.16. abbreviations

commandaction
:ab str long stringabbreviate str to be 'long string'
:una strun-abbreviate str

key mappings

Similarly to their abbreviations, you can use mappings with :map for command mode and :map! for insert mode.

This example shows how to set the F6 function key to toggle between set number and set nonumber. The <bar> separates the two commands, set number! toggles the state and set number? reports the current state.

:map <F6> :set number!<bar>set number?<CR>

setting options

Some options that you can set in vim.

:set number  ( also try :se nu )
:set nonumber
:syntax on
:syntax off
:set all  (list all options)
:set tabstop=8
:set tx   (CR/LF style endings)
:set notx
	

You can set these options (and much more) in ~/.vimrc for vim or in ~/.exrc for standard vi.

paul@barry:~$ cat ~/.vimrc
set number
set tabstop=8
set textwidth=78
map <F6> :set number!<bar>set number?<CR>
paul@barry:~$
	

practice: vi(m)

1. Start the vimtutor and do some or all of the exercises. You might need to run aptitude install vim on xubuntu.

2. What 3 key sequence in command mode will duplicate the current line.

3. What 3 key sequence in command mode will switch two lines' place (line five becomes line six and line six becomes line five).

4. What 2 key sequence in command mode will switch a character's place with the next one.

5. vi can understand macro's. A macro can be recorded with q followed by the name of the macro. So qa will record the macro named a. Pressing q again will end the recording. You can recall the macro with @ followed by the name of the macro. Try this example: i 1 'Escape Key' qa yyp 'Ctrl a' q 5@a (Ctrl a will increase the number with one).

6. Copy /etc/passwd to your ~/passwd. Open the last one in vi and press Ctrl v. Use the arrow keys to select a Visual Block, you can copy this with y or delete it with d. Try pasting it.

7. What does dwwP do when you are at the beginning of a word in a sentence ?

solution: vi(m)

1. Start the vimtutor and do some or all of the exercises. You might need to run aptitude install vim on xubuntu.

vimtutor

2. What 3 key sequence in command mode will duplicate the current line.

yyp

3. What 3 key sequence in command mode will switch two lines' place (line five becomes line six and line six becomes line five).

ddp

4. What 2 key sequence in command mode will switch a character's place with the next one.

xp

5. vi can understand macro's. A macro can be recorded with q followed by the name of the macro. So qa will record the macro named a. Pressing q again will end the recording. You can recall the macro with @ followed by the name of the macro. Try this example: i 1 'Escape Key' qa yyp 'Ctrl a' q 5@a (Ctrl a will increase the number with one).

6. Copy /etc/passwd to your ~/passwd. Open the last one in vi and press Ctrl v. Use the arrow keys to select a Visual Block, you can copy this with y or delete it with d. Try pasting it.

cp /etc/passwd ~
vi passwd
(press Ctrl-V)

7. What does dwwP do when you are at the beginning of a word in a sentence ?

dwwP can switch the current word with the next word.