VI Cheat Sheet
File Management Commands
:w name- Write edit buffer to file name:wq- Write to file and quit:q!- Quit without saving changesZZ- Same as:wq:sh- Execute shell commands (<ctrl>d)
Window Motions
<ctrl>d- Scroll down (half a screen)<ctrl>u- Scroll up (half a screen)<ctrl>f- Page forward<ctrl>b- Page backward/string- Search forward?string- Search backward<ctrl>l- Redraw screen<ctrl>g- Display current line number and file informationn- Repeat searchN- Repeat search reverseG- Go to last linenG- Go to line n:n- Go to line nz<CR>- Reposition window: cursor at topz.- Reposition window: cursor in middlez-- Reposition window: cursor at bottom
Cursor Motions
H- Upper left corner (home)M- Middle lineL- Lower left cornerh- Back a characterj- Down a linek- Up a line^- Beginning of line$- End of linel- Forward a characterw- One word forwardb- Back one wordfc- Find c;- Repeat find (find next c)
Command Mode vs Input Mode
Vi starts in command mode. The positioning commands operate only while vi is in command mode. You switch vi to input mode by entering any one of several vi input commands. Once in input mode, any character you type is taken to be text and is added to the file. You cannot execute any commands until you exit input mode.
To exit input mode, press the escape (Esc) key.
Input Commands (end with Esc)
a- Append after cursori- Insert before cursoro- Open line belowO- Open line above:r file- Insert file after current line
Change Commands (Input Mode)
cw- Change word (Esc)cc- Change line (Esc) - blanks linec$- Change to end of linerc- Replace character with cR- Replace (Esc) - typeovers- Substitute (Esc) - 1 char with stringS- Substitute (Esc) - Rest of line with text.- Repeat last change
Changes During Insert Mode
<ctrl>h- Back one character<ctrl>w- Back one word<ctrl>u- Back to beginning of insert
Deletion Commands
ddorndd- Delete n lines to general bufferdw- Delete word to general bufferdnw- Delete n wordsd)- Delete to end of sentencedb- Delete previous wordD- Delete to end of linex- Delete character
Recovering Deletions
p- Put general buffer after cursorP- Put general buffer before cursor
Undo Commands
u- Undo last changeU- Undo all changes on line
Rearrangement Commands
yyorY- Yank (copy) line to general buffer"z6yy- Yank 6 lines to buffer zyw- Yank word to general buffer"a9dd- Delete 9 lines to buffer a"A9dd- Delete 9 lines; Append to buffer a"ap- Put text from buffer a after cursorp- Put general buffer after cursorP- Put general buffer before cursorJ- Join lines
Parameters
:set list- Show invisible characters:set nolist- Don't show invisible characters:set number- Show line numbers:set nonumber- Don't show line numbers:set autoindent- Indent after carriage return:set noautoindent- Turn off autoindent:set showmatch- Show matching sets of parentheses as they are typed:set noshowmatch- Turn off showmatch:set showmode- Display mode on last line of screen:set noshowmode- Turn off showmode:set all- Show values of all possible parameters
Moving Text Between Files
To move text from file old to file new:
vi old
"a10yy # yank 10 lines to buffer a
:w # write work buffer
:e new # edit new file
"ap # put text from a after cursor
:30,60w new # Write lines 30 to 60 in file new
Regular Expressions (Search Strings)
^- Matches beginning of line$- Matches end of line.- Matches any single character*- Matches any previous character.*- Matches any character
Search and Replace Commands
Syntax: :[address]s/old_text/new_text/
Address Components:
.- Current linen- Line number n.+ m- Current line plus m lines$- Last line/string/- A line that contains "string"%- Entire file[addr1],[addr2]- Specifies a range
Examples:
- Replace first occurrence of Banana with Kumquat in 11 lines starting with current line:
:.,.+10s/Banana/Kumquat - Replace every occurrence of apple with pear:
:%s/apple/pear/g - Remove last character from every line in file:
:%s/.$//