LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

VIM Text Editor

Modes

Unlike most editors, VIM does not let you type text right away. It starts in Normal mode, where key presses are commands instead of text input. You switch between modes depending on what you want to do.
$ vim textfile.txt
copy
KeyDescription
iEnter Insert mode: type text freely
vEnter Visual mode: select characters
VEnter Visual Line mode: select whole lines
Ctrl+vEnter Visual Block mode: select a rectangle
:Enter Command-line mode: run commands
REnter Replace mode: overwrite existing text
EscReturn to Normal mode
The best way to learn is the built-in interactive tutorial: run vimtutor in your terminal, it takes about 30 minutes.

File Management

All commands starting with : are typed in Command-line mode. Press Enter to execute them.
KeyDescription
:eReload current file from disk
:e {file}Open a file for editing
:wSave current file
:w {file}Save to a different file
:qQuit (fails if there are unsaved changes)
:q!Quit and discard unsaved changes
:wqSave and quit
:xSave and quit (only writes if modified)

Navigation

These work in Normal mode. VIM uses h j k l instead of arrow keys, but arrow keys also work.
KeyDescription
hMove left
jMove down
kMove up
lMove right
wJump to start of next word
WJump to start of next WORD (whitespace-separated)
eJump to end of word
bJump back to start of previous word
0Jump to start of line
^Jump to first non-blank character of line
$Jump to end of line
{ / }Jump to previous / next paragraph
%Jump to the matching bracket
Ctrl+dScroll down half a page
Ctrl+uScroll up half a page
ggJump to first line of file
GJump to last line of file
{number}GJump to a specific line number
gk / gjMove up / down one display line (wrapped lines)

Insertion

Each of these enters Insert mode from Normal mode, but places the cursor differently. Press Esc when done to return to Normal mode.
KeyDescription
iInsert before cursor
IInsert at beginning of line
aAppend after cursor
AAppend at end of line
oOpen a new line below and start inserting
OOpen a new line above and start inserting
RReplace mode: type over existing characters
:r {file}Insert contents of a file below the cursor

Editing

VIM combines operators like d (delete), y (yank/copy), and c (change) with motions like w (word) or $ (end of line): dw deletes a word, c$ rewrites the rest of the line.
KeyDescription
uUndo last change
Ctrl+rRedo last undone change
yyYank (copy) the current line
y{motion}Yank text covered by a motion (e.g. yw for a word)
pPaste after cursor
PPaste before cursor
xDelete character under cursor
r{char}Replace the character under the cursor
ddDelete current line
d{motion}Delete text covered by a motion (e.g. dw for a word)
c{motion}Delete text and enter Insert mode
JJoin the next line onto the current one
>> / <<Indent / unindent the current line
.Repeat the last change
Deleted and yanked text both land in the same register, so dd then p moves a line.

Counts & Text Objects

Prefix any command with a number to repeat it: 3dd deletes three lines, 2w jumps two words. Text objects describe what is *around* the cursor: i means inside, a includes the delimiters.
KeyDescription
ciwChange the word under the cursor
diwDelete the word under the cursor
di"Delete everything inside the quotes
ci(Change everything inside the parentheses
da(Delete the parentheses and their content
yipYank the current paragraph

Search and Replace

Press / or ? in Normal mode to start a search. Replace commands use the : command line.
KeyDescription
/patternSearch forward for pattern
?patternSearch backward for pattern
nJump to next match
NJump to previous match
*Search forward for the word under the cursor
#Search backward for the word under the cursor
:s/foo/bar/Replace first occurrence of foo with bar on current line
:s/foo/bar/gReplace all occurrences of foo with bar on current line
:%s/foo/bar/gReplace all occurrences of foo with bar in the entire file
:%s/foo/bar/gcReplace all in file, asking for confirmation each time
:nohClear search highlighting

Multiple Windows

VIM can split the screen to show multiple files at once. Window commands start with Ctrl+w.
KeyDescription
:split {file}Split horizontally and open file
:vsplit {file}Split vertically and open file
:sview {file}Split horizontally and open file as read-only
Ctrl+w wCycle to the next window
Ctrl+w h j k lMove to the window in that direction
Ctrl+w _Maximize current window height
Ctrl+w =Make all windows equal size
:hideClose the current window
:onlyClose all windows except the current one
Every open file lives in a buffer, whether it is visible or not.
KeyDescription
:lsList all open buffers
:b {number}Switch to a buffer by its number
:bn / :bpSwitch to the next / previous buffer
:bdClose (delete) the current buffer
Copied to clipboard
Kai