Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Vim Cheatsheet

한국어

Table of Contents

  1. Cursor Movement
  2. Insert Mode
  3. Editing
  4. Cut & Paste
  5. Visual Mode & Selection
  6. Indentation
  7. Search & Replace
  8. Registers & Marks
  9. Macros
  10. Files, Buffers, Windows & Tabs
  11. Exiting
  12. Diff
  13. Practical Notes

1. Cursor Movement

Key Action
h move cursor left
j move cursor down
k move cursor up
l move cursor right
gj move cursor down (multi-line text)
gk move cursor up (multi-line text)
H move to top of screen
M move to middle of screen
L move to bottom of screen
Key Action
w jump forward to start of word
W jump forward to start of WORD (whitespace-delimited)
e jump forward to end of word
E jump forward to end of WORD
b jump backward to start of word
B jump backward to start of WORD
ge jump backward to end of word
gE jump backward to end of WORD
Key Action
0 jump to beginning of line
^ jump to first non-blank character of line
$ jump to end of line
g_ jump to last non-blank character of line
gg go to first line of document
G go to last line of document
5G go to line 5
gd move to local declaration
gD move to global declaration
Key Action
f{c} jump to next occurrence of character c on line
F{c} jump to previous occurrence of character c on line
t{c} jump to just before next occurrence of c
T{c} jump to just after previous occurrence of c
; repeat last f, F, t, or T forward
, repeat last f, F, t, or T backward
Key Action
} jump to next paragraph (or block)
{ jump to previous paragraph (or block)
Ctrl+f scroll one page forward
Ctrl+b scroll one page backward
Ctrl+d scroll half page forward
Ctrl+u scroll half page backward
Ctrl+e scroll screen down one line
Ctrl+y scroll screen up one line
% jump to matching bracket (), [], or {}

Tip

Prefix any motion with a number to repeat it: 4j moves down 4 lines, 3w jumps 3 words forward, 2Ctrl+d scrolls down a full page.


2. Insert Mode

Key Action
i insert before cursor
I insert at beginning of line
a insert (append) after cursor
A insert (append) at end of line
o open new line below current line
O open new line above current line
ea insert at end of current word
Key Action
Ctrl+h delete character before cursor
Ctrl+w delete word before cursor
Ctrl+t indent line one level
Ctrl+d de-indent line one level
Ctrl+n insert next autocomplete match
Ctrl+p insert previous autocomplete match
Ctrl+r{x} insert contents of register x
Ctrl+o{cmd} execute one normal mode command then return
Esc / Ctrl+c exit insert mode

3. Editing

Key Action
r{c} replace character under cursor with c
R enter replace mode (overwrite until Esc)
J join line below to current, with space
gJ join line below to current, without space
gwip reformat paragraph to textwidth
g~{motion} toggle case of text covered by motion
gu{motion} lowercase text covered by motion
gU{motion} uppercase text covered by motion
Key Action
cc change (replace) entire line
C / c$ change to end of line
ciw change inner word
cw change to end of word
s delete character and enter insert mode
S delete line and enter insert mode
xp transpose character under cursor and next
Key Action
u undo
Ctrl+r redo
. repeat last change

Note

Operator + Motion: c (change), d (delete), and y (yank) combine with any motion. cw = change to end of word, d$ = delete to end of line, y3j = yank 3 lines down, gUiw = uppercase entire word. This composability is Vim's core grammar.


4. Cut & Paste

Key Action
yy yank (copy) a line
2yy yank 2 lines
yw yank to end of word
yiw yank inner word
yaw yank word with surrounding whitespace
y$ yank to end of line
y^ yank to first non-blank of line
Key Action
p put (paste) clipboard after cursor / current line
P put (paste) clipboard before cursor / current line
gp put after cursor and leave cursor after pasted text
gP put before cursor and leave cursor after pasted text
Key Action
dd delete (cut) a line
2dd delete 2 lines
dw delete (cut) to end of word
diw delete inner word
daw delete word with surrounding whitespace
D / d$ delete to end of line
d^ delete to first non-blank of line
d0 delete to beginning of line
x delete (cut) character under cursor
X delete character before cursor

5. Visual Mode & Selection

Entering Visual Mode

Key Action
v start character-wise visual mode
V start line-wise visual mode
Ctrl+v start block-wise (column) visual mode
gv reselect last visual selection
o move to the other end of the selection
O move to the other corner of block selection
Esc / Ctrl+c exit visual mode

Text Objects (usable in visual mode and with operators)

Key Action
aw select a word (with surrounding whitespace)
iw select inner word
ab / a( select a block including ()
ib / i( select inner block ()
aB / a{ select a block including {}
iB / i{ select inner block {}
at select a tag block including <>
it select inner tag block <>
a" select a double-quoted string
i" select inner double-quoted string
a' select a single-quoted string
i' select inner single-quoted string

Commands in Visual Mode

Key Action
> shift selection right
< shift selection left
y yank (copy) selected text
d delete selected text
c change (replace) selected text
~ toggle case of selected text
u convert selected text to lowercase
U convert selected text to uppercase

6. Indentation

Key Action
>> indent current line one level
<< de-indent current line one level
>% indent a block matched by (), {}, or []
<% de-indent a matched block
>ib indent inner block ()
>at indent a tag block <>
3== re-indent 3 lines
=% re-indent a matched block
=G re-indent to end of file
gg=G re-indent entire file

Tip

In visual mode, > and < indent or de-indent the selection. Press . immediately after to repeat.


7. Search & Replace

Search

Key Action
/{pattern} search forward for pattern
?{pattern} search backward for pattern
n repeat search in same direction
N repeat search in opposite direction
* search forward for whole word under cursor
# search backward for whole word under cursor
g* search forward for partial word under cursor
g# search backward for partial word under cursor

Substitute

Command Action
:s/old/new/ replace first match on current line
:s/old/new/g replace all matches on current line
:%s/old/new/g replace all matches in file
:%s/old/new/gc replace all matches with confirmation
:%s/old/new/gi replace all matches, case-insensitive
:5,12s/old/new/g replace in lines 5 through 12

Search Across Files

Command Action
:vimgrep /{pattern}/ **/* search pattern in all files recursively
:cn jump to next quickfix result
:cp jump to previous quickfix result
:copen open quickfix list
:cclose close quickfix list

Tip

Add \c anywhere in a pattern for case-insensitive search: /\cword. Use :set hlsearch to highlight matches; :noh clears highlighting without changing the setting.


8. Registers & Marks

Registers

Key Action
:reg show contents of all registers
"xy yank into register x
"xp paste from register x
"+y yank into system clipboard
"+p paste from system clipboard
"*y yank into primary selection (X11)
"*p paste from primary selection (X11)

Note

Special registers: "" unnamed (last yank or delete), "0 last explicit yank, "- last small delete (< 1 line), ". last inserted text, "% current filename, ": last command-line command, "/ last search pattern.

Marks

Key Action
m{a-z} set local mark (lowercase = file-local)
m{A-Z} set global mark (uppercase = cross-file)
`{a} jump to exact position of mark a
'{a} jump to first non-blank char of mark a line
`0 jump to position when last exited Vim
`` jump back to position before last jump
'' jump to first non-blank of line before last jump
:marks list all marks
:delmarks a delete mark a

9. Macros

Key Action
q{a} start recording macro into register a
q stop recording
@{a} execute macro in register a
@@ re-execute the last run macro
5@{a} execute macro a 5 times
:reg {a} view raw contents of macro a

Tip

Macros are plain text in registers. To edit one: paste with "ap, modify the text, yank the line back with "ayy, then delete the line. Use 100@a with a motion that fails at EOF to run until it can't continue.


10. Files, Buffers, Windows & Tabs

Buffers

Command Action
:e {file} open file in a new buffer
:bn go to next buffer
:bp go to previous buffer
:b# switch to alternate (last used) buffer
:b {file} switch to buffer by name
:bd close (delete) current buffer
:ls list all open buffers

Windows (Splits)

Key Action
:sp {file} open file in horizontal split
:vsp {file} open file in vertical split
Ctrl+w s split horizontally
Ctrl+w v split vertically
Ctrl+w w cycle through windows
Ctrl+w h/j/k/l move focus to window left/down/up/right
Ctrl+w H/J/K/L move window to far left/bottom/top/right
Ctrl+w = equalize all window sizes
Ctrl+w + increase window height
Ctrl+w - decrease window height
Ctrl+w > increase window width
Ctrl+w < decrease window width
Ctrl+w q close current window
Ctrl+w o close all other windows

Tabs

Key Action
:tabnew open a new empty tab
:tabnew {file} open file in a new tab
gt / :tabn move to next tab
gT / :tabp move to previous tab
{n}gt go to tab number n
:tabclose close current tab
:tabonly close all other tabs
:tabmove {n} move tab to position n

11. Exiting

Command Action
:w write (save) file
:w {file} write to a specific file
:w !sudo tee % write with sudo (when opened without privileges)
:wq / :x / ZZ write and quit
:q quit (fails if there are unsaved changes)
:q! / ZQ quit without saving
:wqa write and quit all windows/tabs
:qa! quit all without saving

12. Diff

Command / Key Action
vimdiff {f1} {f2} open two files in diff mode (from shell)
:diffthis mark current window as a diff window
:diffupdate recompute diff highlighting
:diffsplit {file} open file in a horizontal diff split
:vert diffsplit {file} open file in a vertical diff split
]c jump to next change
[c jump to previous change
do diff obtain — pull change from other window
dp diff put — push change to other window

13. Practical Notes

Tip

Operator + Motion — Vim's grammar is composable. Operators (d delete, c change, y yank, g~/gu/gU case) combine with any motion or text object: dw = delete to end of word, ci( = change inside parentheses, y$ = yank to end of line, gUiw = uppercase entire word. You learn a vocabulary, not a list of shortcuts.

Tip

Number Prefix — Most commands accept a count: 3dd deletes 3 lines, 5j moves down 5 lines, 2dw deletes 2 words, 4>> indents 4 lines.

Tip

Repeat & Undo. repeats the last change at the cursor position. u undoes, Ctrl+r redoes. The . + small targeted change pattern is one of Vim's most efficient workflows.

Note

Text Objectsi (inner) and a (around/a) prefix motion objects: iw/aw (word), i(/a( (parentheses), i{/a{ (braces), i"/a" (double quotes), i'/a' (single quotes), it/at (XML/HTML tags). Example: ci" changes the content inside quotes without touching the quotes themselves.


*Sources: rtorr/vim-cheat-sheet (MIT)

About

My quick-reference cheatsheet for Vim

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors