Table of Content

Weight: 3

Description: Candidates should be able to edit text files using vi. This objective includes vi navigation, basic vi modes, inserting, editing, deleting, copying and finding text.

Key Knowledge Areas:

  • Navigate a document using vi
  • Use basic vi modes
  • Insert, edit, delete, copy and find text

Terms and Utilities:

vi
/, ?
h,j,k,l
i, o, a
c, d, p, y, dd, yy
ZZ, :w!, :q!, :e!

move

h,j,k,l   left, down, up, right
0 - move to 1st char of line 
$ - move to end of line
1G - move to begin of 1st line of file
nG - move to line n 
G - move to last line of file

screen

^f  move forward one screen  
^b  move backward one screen  
^d  move down (forward) one half screen  
^u  move up (back) one half screen  
^l  redraws the screen  
^r  redraws the screen, removing deleted lines  

undo

u only undo last change

yank

nyy or yny     copy n lines
ndd or dnd     cut n lines

p  # paste

replace

:%s/old/new/g     replace all string old to string new in file
:n1,n2s/old/new/g    replace all string old to string new in range of lines from n1 to n2 

change

cw change current word 
cNw change n words
C change(replace) char in current line
cc change current line 
d dw delete one word
x delete one char

r replace one char
R replace anything you type in until esc

exit

:e    edit  without leaving vi 
:e!                 forget changes since last write
:wq write and quit  ZZ same

run shell inside vi

:!pwd
:!ls

Quiz questions

1. What are the vi commands for up, down, left and right?
2. How do you search for the string “The” at the beginning of a line?
3. What is the key to exit insert mode?
4. What is the meaning of ! in command mode?
5. How do you delete an entire line?
6. How does vi’s cut and paste work?
7. Explain 3 different ways of getting into insert mode.
8. What is the quick equivalent for “:wq”?
Answers to quiz questions
1. k j h l
2. / ^The
3. Escape
4. Don’t save
5. dd
6. d to delete (e.g. dd for a line, dw for delete word, 6dw for delete 6 words), p for paste (or P for paste before).
7. i (insert), o (add line), O (add line before), a (append). Perhaps even cw (change word).
8. ZZ