Skip to content
Seir-Seal
GitHub

Vim

What is Vim?

Vim is a text editor for Unix that comes with Linux, BSD, and macOS. It is known to be fast and powerful, partly because it is a small program that can run in a terminal (although it has a graphical interface). It is mainly because it can be managed entirely without menus or a mouse with a keyboard.

Why Vim?

In all POSIX systems, Vim is the default fallback editor. Vim is sure to be open, whether you have just installed the operating system, or you have booted into a minimal system repair environment, or you are unable to access any other editor. While you can switch out other tiny editors on your systems, such as GNU Nano or Jove, it’s Vim that’s all but guaranteed to be on every other system in the world.

Global

These are some of the common global Vim movements and commands used for file management and navigation. You can use these commands in normal mode by pressing : to enter command mode and then typing the desired command.

Key CombinationDescription
:qQuit (close) the current file
:q!Quit (close) the current file without saving changes
:wSave the current file
:wqSave the current file and quit (close)
:xSave changes and quit (close) the current file
:e [file]Open a file for editing (optional: specify a file name)
:sp [file]Split window and open a file for editing (optional: specify a file name)
:vsp [file]Split window vertically and open a file for editing (optional: specify a file name)
:b [buffer]Switch to a different buffer (optional: specify a buffer number or name)
:bnSwitch to the next buffer
:bpSwitch to the previous buffer
:bd [buffer]Close (delete) a buffer (optional: specify a buffer number or name)
:lsList all open buffers and their statuses
:tabnewOpen a new tab
:tabnext or :tabnSwitch to the next tab
:tabprevious or :tabpSwitch to the previous tab
:tabclose or :tabcClose the current tab
:tabonly or :taboClose all other tabs and keep only the current tab
:help [command]Open Vim’s help documentation (optional: specify a command for specific help)

Visual Commands

CommandDescription
vStart visual mode character-wise selection.
VStart visual mode line-wise selection.
Ctrl + vStart visual mode block-wise selection.
gvReselect the last visual selection.
:Execute a command on the selected text.
!Filter the selected text through an external command.
xCut (delete) selected text.
yCopy (yank) selected text.
>Indent selected text.
<Unindent selected text.

Marking Text (Visual Mode)

CommandDescription
oMove to the other end of the marked area.
OMove to the other corner of the block.
awMark a word.
abA block with () parentheses.
aBA block with {} curly braces.
atA block with <> angle brackets tags.
ibInner block with () parentheses.
iBInner block with {} curly braces.
itInner block with <> angle brackets tags.
Esc or Ctrl + cExit visual mode.

Cursor

To use these commands, you typically press the key combination in Normal mode. For example, to move the cursor down, you would press the j key, and to jump to the end of a word, you would press the e key.

You can also use a count before some commands to repeat them. For example, 3j would move the cursor down three lines.

Remember that Vim has different modes (Normal, Insert, Visual, etc.), and cursor movement commands are primarily used in Normal mode. If you’re in a different mode (e.g., Insert), you may need to press Esc to return to Normal mode before using cursor movement commands.

CommandDescription
hMove cursor left.
jMove cursor down.
kMove cursor up.
lMove cursor right.
gjMove cursor down (multi-line text).
gkMove cursor up (multi-line text).
HMove to top of screen.
MMove to middle of screen.
LMove to bottom of screen.
wJump forwards to the start of a word.
WJump forwards to the start of a word (words can contain punctuation).
eJump forwards to the end of a word.
EJump forwards to the end of a word (words can contain punctuation).
bJump backwards to the start of a word.
BJump backwards to the start of a word (words can contain punctuation).
geJump backwards to the end of a word.
gEJump backwards to the end of a word (words can contain punctuation).
%Move cursor to matching character (default supported pairs: ’()’, ’{}’, ’[]’ - use :h matchpairs in Vim for more info).
0Jump to the start of the line.
^Jump to the first non-blank character of the line.
$Jump to the end of the line.
g_Jump to the last non-blank character of the line.
ggGo to the first line of the document.
GGo to the last line of the document.
5gg or 5GGo to line 5.
gdMove to local declaration.
gDMove to global declaration.
fxJump to next occurrence of character x.
txJump to before next occurrence of character x.
FxJump to the previous occurrence of character x.
TxJump to after previous occurrence of character x.
;Repeat previous f, t, F, or T movement.
,Repeat previous f, t, F, or T movement, backwards.
}Jump to next paragraph (or function/block, when editing code).
{Jump to previous paragraph (or function/block, when editing code).
zzCenter cursor on screen.
ztPosition cursor on top of the screen.
zbPosition cursor on bottom of the screen.
Ctrl + eMove screen down one line (without moving cursor).
Ctrl + yMove screen up one line (without moving cursor).
Ctrl + bMove screen up one page (cursor to last line).
Ctrl + fMove screen down one page (cursor to first line).
Ctrl + dMove cursor and screen down 1/2 page.
Ctrl + uMove cursor and screen up 1/2 page.

Basic Editing

CommandDescription
iInsert before cursor.
IInsert at the beginning of the line.
aInsert after cursor.
AInsert at the end of the line.
oOpen a new line below and insert.
OOpen a new line above and insert.
xDelete character under cursor.
ddDelete current line.
DDelete from cursor to end of line.
uUndo the last change.
Ctrl + rRedo the last undone action.

Copy and Paste

CommandDescription
yyCopy (yank) current line.
yCopy (yank) selected text.
pPaste after cursor.
PPaste before cursor.
ddCut (delete) current line.
dwCut (delete) from cursor to the end of the word.
DCut (delete) from cursor to the end of the line.
xCut (delete) character under cursor.

File Navigation

CommandDescription
:e <file>Edit a specific file.
:sp <file>Open a new horizontal split with the specified file.
:vsp <file>Open a new vertical split with the specified file.
:tabe <file>Open a file in a new tab.
:tabnGo to the next tab.
:tabpGo to the previous tab.
:tabfirstGo to the first tab.
:tablastGo to the last tab.
:lsList all open buffers.
:bpGo to the previous buffer.
:bnGo to the next buffer.
:bdClose the current buffer.

Search and Replace

CommandDescription
:/patternSearch for pattern in the file.
:s/old/new/gReplace old with new globally on the current line.
:s/old/new/gcReplace old with new globally on the current line with confirmation.
:%s/old/new/gReplace old with new globally in the entire file.
:%s/old/new/gcReplace old with new globally in the entire file with confirmation.

Search in Multiple Files

CommandDescription
:grep pattern filesSearch for pattern in files and open matching lines in quickfix list.
:lgrep pattern filesSame as :grep, but open results in location list.
:copenOpen quickfix list.
:lopenOpen location list.

Indent Text

CommandDescription
>>Indent current line.
<<Unindent current line.
>%Indent a block with () or {} (cursor on brace)
<%De-indent a block with () or {} (cursor on brace)
>ibIndent inner block with ()
>atIndent a block with <> tags
>i{Indent text within curly braces.
=i(Indent text within parentheses.
3==Re-indent 3 lines.
=%Re-indent a block with () or {} (cursor on brace).
]pPaste and adjust indent to current line.

More

  • Vim Adventures, a fun game you can play to learn Vim.
  • Looking to upgrade your vim experience, then try out NeoVim the super customizable text editor.