Skip to content

Vim Cheat Sheet

Posted on:August 19, 2023 at 05:07 PM

Table of Contents

Open Table of Contents

Introduction

Vim is a text editor that is included in most Linux distributions. It is a very powerful tool that can be used to edit text files, edit code, and even manage files on your server. Vim is a modal editor, which means that it has different modes for writing, navigating, and selecting text. Vim is a very powerful tool that can be used to edit text files, edit code, and even manage files on your server.

Modes

Vim has different modes for writing, navigating, and selecting text. The most common modes are:

Commands

Opening and Saving Files

CommandDescription
vim fileOpen a file in Vim
:wSave a file
:wqSave a file and exit Vim
:qExit Vim
:q!Exit Vim without saving changes
CommandDescription
hMove cursor left
jMove cursor down
kMove cursor up
lMove cursor right
wMove cursor to the beginning of the next word
bMove cursor to the beginning of the previous word
eMove cursor to the end of the next word
0Move cursor to the beginning of the line
$Move cursor to the end of the line
ggMove cursor to the beginning of the file
GMove cursor to the end of the file
:nMove cursor to line number n
Ctrl + fMove cursor forward one page
Ctrl + bMove cursor backward one page
Ctrl + dMove cursor forward half a page
Ctrl + uMove cursor backward half a page

Editing Files

CommandDescription
iEnter insert mode
aEnter insert mode after the cursor
oEnter insert mode on a new line below the cursor
OEnter insert mode on a new line above the cursor
rReplace character under cursor
REnter replace mode
xDelete character under cursor
ddDelete line
yyCopy line
pPaste line below cursor
PPaste line above cursor
uUndo last change
Ctrl + rRedo last change
.Repeat last change
JJoin line below to the current one

Searching Files

CommandDescription
/patternSearch forward for pattern
?patternSearch backward for pattern
nRepeat search in same direction
NRepeat search in opposite direction

Selecting Text

CommandDescription
vEnter visual mode
VEnter visual line mode
Ctrl + vEnter visual block mode
awSelect a word
abSelect a block with parentheses
aBSelect a block with braces
ibSelect inner block with parentheses
iBSelect inner block with braces
EscExit visual mode

Editing Text

CommandDescription
~Switch case
>Indent line
<Unindent line
=Auto indent
gqFormat text
JJoin line below to the current one
Ctrl + aIncrement number
Ctrl + xDecrement number

Copying and Pasting Text

CommandDescription
yyCopy line
pPaste line below cursor
PPaste line above cursor
ywCopy word
yiwCopy inner word
y$Copy to end of line
y0Copy to beginning of line
y^Copy to first non-blank character of line
yggCopy to first line of file
yGCopy to last line of file
y/patternCopy to next occurrence of pattern
y?patternCopy to previous occurrence of pattern
y/pattern/Copy to next occurrence of pattern on the same line
y?pattern?Copy to previous occurrence of pattern on the same line

Deleting Text

CommandDescription
xDelete character under cursor
dwDelete word
ddDelete line
d$Delete to end of line
d0Delete to beginning of line
d^Delete to first non-blank character of line
dggDelete to first line of file
dGDelete to last line of file
d/patternDelete to next occurrence of pattern
d?patternDelete to previous occurrence of pattern
d/pattern/Delete to next occurrence of pattern on the same line
d?pattern?Delete to previous occurrence of pattern on the same line

Exiting Vim (no joking)

CommandDescription
:wSave a file
:wqSave a file and exit Vim
:qExit Vim
:q!Exit Vim without saving changes

References

Happy learning!