Vi Editor Cheat Sheet

Vi editor is a powerful and versatile text editor that has been around for over four decades. It is a command-line based editor that is widely used by programmers, system administrators, and other professionals who work with text files on Unix and Linux systems.

Vi editor is known for its speed, efficiency, and flexibility. It allows users to edit text files of any size, navigate through them quickly, and perform complex editing tasks with ease. Vi editor is also highly customizable, with a wide range of options and settings that can be tailored to suit individual preferences and workflows.

Basic Navigation

CommandDescription
hMove cursor left
jMove cursor down
kMove cursor up
lMove cursor right
0Move cursor to beginning of line
$Move cursor to end of line
ggMove cursor to beginning of file
GMove cursor to end of file
:nMove cursor to line number n

Inserting and Editing Text

CommandDescription
iInsert text before cursor
aAppend text after cursor
oOpen new line below cursor
OOpen new line above cursor
rReplace character under cursor
xDelete character under cursor
ddDelete current line
yyYank (copy) current line
pPaste yanked text after cursor
uUndo last change
Ctrl + rRedo last change

Searching and Replacing

CommandDescription
/patternSearch for pattern
nMove to next search result
NMove to previous search result
:%s/old/new/gReplace all occurrences of old with new
:s/old/new/gReplace first occurrence of old with new on current line
:s/old/new/gcReplace first occurrence of old with new on current line, with confirmation

Saving and Quitting

CommandDescription
:wSave changes
:wq or :xSave changes and quit
:qQuit (only if no changes have been made)
:q!Quit without saving changes

Advanced Navigation

CommandDescription
f<char>Move cursor to next occurrence of <char> on current line
F<char>Move cursor to previous occurrence of <char> on current line
t<char>Move cursor to just before next occurrence of <char> on current line
T<char>Move cursor to just after previous occurrence of <char> on current line
;Repeat last f, F, t, or T command
,Repeat last f, F, t, or T command in opposite direction
%Move cursor to matching bracket ((), {}, [])

Visual Mode

CommandDescription
vEnter visual mode
VEnter visual line mode
Ctrl + vEnter visual block mode
dDelete selected text
yYank (copy) selected text
pPaste yanked text after cursor

Macros

CommandDescription
q<char>Start recording macro into register <char>
qStop recording macro
@<char>Execute macro in register <char>
@@Execute last executed macro

Multiple Windows

CommandDescription
:splitSplit window horizontally
:vsplitSplit window vertically
Ctrl + w hMove cursor to window on the left
Ctrl + w jMove cursor to window below
Ctrl + w kMove cursor to window above
Ctrl + w lMove cursor to window on the right
Ctrl + w Ctrl + wCycle through windows
Ctrl + w +Increase window size
Ctrl + w -Decrease window size

Miscellaneous

CommandDescription
.Repeat last command
:set numberShow line numbers
:set nonumberHide line numbers
:set hlsearchHighlight search results
:set nohlsearchDisable search result highlighting
:set ignorecaseIgnore case when searching
:set noignorecaseDo not ignore case when searching
:set tabstop=nSet tab width to n spaces
:set expandtabUse spaces instead of tabs
:set noexpandtabUse tabs instead of spaces
:set mouse=aEnable mouse support
:set nomouseDisable mouse support

Reference:

https://docs.oracle.com/cd/E19253-01/806-7612/editorvi-43/index.html