vi Editor

Cursor Movement
Text creation and Manipulation
Changing and deleting Text
Scrolling screen
Replacing words globally
Managing files
Other vi tips

$HOME/.exrc - vi control file


Cursor Movement

 j,k  move cursor down or up
 h,l  move cursor left or right
 +    first character of next line	
 -    first character of previous line
 e    end of word	
 w    Forward by word	
 b    Backward by word	
 $    end of line	
 0    beginning of line

 H    top line on screen
 L    last line on screen
 M    middle line on screen

 nG   Go to given line n
 G    Go to end of file


Text creation and Manipulation

 i - insert text at current position
 a - append text at current position
 r - replace a singe character with new character
 o - Open blank line below cursor for text
 s - Delete character at cursor and substitute text
 x - Delete current cursor position 

 A - Append text to end of current line
 I - Insert text at beginning of line
 O - Open blank line above cursor for text
 S - Delete line and substitute text
 R - Replace existing characters with new characters
 X - Delete back one character 

 . - repeat last action
 u - undo last action
 U - Restore line to original state
 J - Join current and next line
 ~ - Toggle case


Changing and deleting Text

 cw  -  change word
 cc  - change line
 C   - change text from current position to end of line
 dd  - delete line
 ndd - delete n lines
 D   - delete remainder of line
 dw  - delete word
 yy  - copy current line
 p   - Insert last deleted or yanked text after cursor
 P   - Insert last deleted or yanked text before cursor


Scrolling the Screen

 ^F	Scroll forward one screen
 ^B	Scroll backward one screen
 ^D	Scroll forward half screen (down)
 ^U	Scroll backward half screen (up)


Searching a pattern or text

 /patern   Search forward for pattern
 ?pattern  Search backward for pattern
 n         Repeat last search
 N         Repeat last search in opposite direction

 fx        Move to next occurrence of x in current line
 Fx        Move to previous occurrence of x in current line
 tx        Move to just before next occurrence of x in current line
 Tx        Move to just after previous occurrence of x in current line


Replacing words Globally

To change xxxx to yyyy globally in a file

 :%s/xxxx/yyyy/g
         or
 :g/xxx/s//yyy/


Managing files

 :sh   Temporarily goes to command prompt
 :w    write file
 :q!   quit file without saving
 :wq!  Write and quite file
 :x    save and quit
 :e!   return to version of current file at time of last write

 :n    Edit next file
 :n!   forces next file

 :w file1   Save copy to file1
 :e file2   Edit file2 without leaving vi
 :r file1   Read in contents of file1 after cursor
 :r !command	Read in output from command after current line


Other Tips

  • press ^G to find out the current line number anytime
  • :set number to set line number in front of each line in vi
  • :set nonum to not to display numbers
  • :set all to list all available options