vi Cheat Sheet

Vi is a powerful text editor that is widely used in the Unix and Linux environments. It is known for its efficiency, versatility, and extensibility. Vi was originally developed in the 1970s as part of the Unix operating system, and has since become a standard tool for editing text files in a variety of contexts.

Vi can be intimidating for beginners, but with practice, it can become a powerful tool for editing text. To help you get started, we’ve put together a cheat sheet that covers the most commonly used commands in vi. The cheat sheet is organized into different themes, such as navigation, editing, searching, and advanced features. Each command is listed along with a brief description of what it does.

Here are a few tips to get you started with vi:

Vi has two modes: command mode and insert mode. Command mode is used for navigating and editing text, while insert mode is used for typing text. To switch between modes, press the ‘esc’ key to enter command mode, and press ‘i’ to enter insert mode.

Vi uses keyboard shortcuts to perform commands. For example, to move the cursor to the right, press the ‘l’ key. To delete the character under the cursor, press the ‘x’ key.

Vi has a range of advanced features, such as the ability to search for and replace text, and the ability to customize the editor using configuration files. These features can be accessed using commands that begin with a colon (‘:’).

Cheat Sheet

Getting Started

CommandDescription
viOpen a new file in vi
vi <filename>Open an existing file in vi
iEnter Insert mode
escReturn to Command mode
:qQuit vi
:wWrite changes to the file
:wqWrite changes to the file and quit

Navigation

CommandDescription
hMove left one character
jMove down one line
kMove up one line
lMove right one character
wMove forward one word
bMove back one word
0Move to the beginning of the line
$Move to the end of the line
ggMove to the beginning of the file
GMove to the end of the file

Editing

CommandDescription
xDelete the character under the cursor
ddDelete the current line
yyYank (copy) the current line
pPaste the contents of the clipboard
uUndo the last change
.Repeat the last change
rReplace the current character
cwChange the current word
CChange the rest of the line
JJoin the current line with the next line
:s/old/new/gReplace all occurrences of ‘old’ with ‘new’ in the current line
:s/old/new/gcReplace all occurrences of ‘old’ with ‘new’ in the current line with confirmation

Searching

CommandDescription
/patternSearch forward for ‘pattern’
?patternSearch backward for ‘pattern’
nMove to the next match
NMove to the previous match

Advanced

CommandDescription
:set nuDisplay line numbers
:set nonuHide line numbers
:set tabstop=4Set the tab size to 4 spaces
:set shiftwidth=4Set the indent size to 4 spaces
:set expandtabUse spaces instead of tabs for indentation
:set noexpandtabUse tabs instead of spaces for indentation
:set hlsearchHighlight search results
:set nohlsearchDisable highlighting of search results

Exiting

CommandDescription
:wSave changes
:wq or 😡Save changes and exit
:q!Discard changes and exit

Visual Mode

CommandDescription
vEnter visual mode
VEnter visual line mode
Ctrl+vEnter visual block mode
yYank the selected text
dDelete the selected text
~Change the case of the selected text
>Indent the selected text
<Unindent the selected text

Reference:

https://cs.stanford.edu/people/miles/vi.html