Vim cheatsheet | navigation, editing, insert mode, visual mode
Vim is an efficient text editor used in terminal (CLI). Vim is mostly used by programmers.
There is a new VIM fork called NeoVIM or nvim. It is great and modern with better capabilities. All things in this cheatsheet works on vim and nvim.
Exiting Vim ๐
:q
: close file (if it is the only file in vim, vim will be exited too). (a.k.a quit vim):qa
: close all files.:w
: save. (a.k.a write file to the disk/storage):wq
or:x
: save and close file.ZZ
: save and quit.ZQ
: Quit without checking changes.:q!
: Quit without saving any changes.
Navigating Vim ๐
Arrow keys ๐
normal arrow keys works fine, but VIM has its own arrow keys which are in near places to your fingers while you are typing.
h
: left arrow. move one character to the left.j
: down arrow. move one line downward (to the bottom).k
: up arrow. move one line upward (to the top).l
: right arrow. move one character to the right.<C-U>
(CTRL + u): move half-page up.<C-D>
(CTRL + d): move half-page down.<C-B>
(CTRL + b): page up. move one screen height up in the current file.<C-F>
(CTRL + f): page down. move one screen height down in the current file.
Navigating words ๐
b
: previous word.w
: next word.ge
: previous end of word.e
: next end of word.
Navigating lines ๐
0
(zero): start of line.^
(carret): start of line (after whitespace).$
: end of line.
Navigating characters ๐
fc
: go forward to characterc
. For example:ff
means go forward to the char f.Fc
(capital F small c): go backward to characterc
. For example:Fd
means go backward to the char d.
Navigating a document / file ๐
gg
: First line.G
: Last line.:{number}
: Go to line {number}. For example::221
means go to line number 221 in the current document / file.{number}G
: Go to line {number}.{number}j
: Go down {number} lines.{number}k
: Go up {number} lines.
Navigating Window ๐
zz
: Center this line.zt
: Top this line.zb
: Bottom this line.H
: Move to top of screen.M
: Move to middle of screen.L
: Move to bottom of screen.
Navigating the search results ๐
n
: Next matching search pattern.N
: Previous match.*
: Next whole word under cursor.#
: Previous whole word under cursor.
Navigating tab pages ๐
:tabedit [file]
: Edit file in a new tab.:tabfind [file]
: Open file if exists in new tab.:tabclose
: Close current tab.:tabs
: List all tabs.:tabfirst
: Go to first tab.:tablast
: Go to last tab.:tabn
: Go to next tab.:tabp
: Go to previous tab.
Editing in Vim ๐
a
: Append.A
: Append from end of line.i
: Insert.o
: Next line.O
: Previous line.s
: Delete char and insert.S
: Delete line and insert.C
: Delete until end of line and insert.r
: Replace one character.R
: Enter Replace mode.u
: Undo changes.<C-R>
: Redo changes.
Visual mode in Vim ๐
v
: Enter visual mode.V
: Enter visual line mode.<C-V>
(CTRL+v): Enter visual block mode.In
: visual mode.d
orx
: Delete selection.s
: Replace selection.y
: Yank selection (Copy).
See Operators โ for other things you can do.
Operators ๐
Usage ๐
Operators let you operate in a range of text (defined by motion). These are performed in normal mode.
d
: do the motion ofw
.
Operators list ๐
d
: Delete.y
: Yank (copy).c
: Change (delete then insert).>
: Indent right.<
: Indent left.=
: Autoindent.g~
: Swap case.gU
: Uppercase.gu
: Lowercase.!
: Filter through external program.
See :help operator
.
Examples of operators usage ๐
Combine operators with motions to use them.
dd
(repeat the letter): Delete current line.dw
: Delete to next word.db
: Delete to beginning of word.2dd
: Delete 2 lines.dip
: Delete a text object (inside paragraph).- (in visual mode)
d
: Delete selection.
See: :help motion.txt
.
Text objects ๐
Usage : how to use ๐
Text objects let you operate (with an operator) in or around text blocks (objects).
Operator | [i]nside or [a]round | Text object |
---|---|---|
v | i | p |
all text objects ๐
p
: Paragraph.w
: Word.s
: Sentence.[
: A [] block.(
: A () block.{
: A {} block.<
: A<>
block.'
: A quoted string."
: A quoted string.- ```: A quoted string.
b
: A block[(
.B
: A block in[{
.t
: A XML tag block.
Text objects by examples ๐
vip
: Select paragraph.vipipipip
: Select more paragraphs.yip
: Yank inner paragraph. Yank means copy.yap
: Yank paragraph (including newline). Yank means copy.dip
: Delete inner paragraph.cip
: Change inner paragraph.
See Operators โคด for other things you can do.
Folds ๐
zo
orzO
: Open.zc
orzC
: Close.za
orzA
: Toggle.zv
: Open folds for this line.zM
: Close all.zR
: Open all.zm
: Fold more (foldlevel += 1).zr
: Fold less (foldlevel -= 1).zx
: Update folds.
Uppercase ones are recursive (eg, zO is open recursively).
more navigation ๐
%
: Nearest/matching{[()]}
. Navigate to the end of function.[(
: Previous(
.[{
: Previous{
.[<
: Previous<
.])
: Next)
.[m
: Previous method start.[M
: Previous method end.
Jumping in Vim ๐
<C-O>
(CTRL+o): Go back to previous location.<C-I>
(CTRL+i): Go forward.gf
: Go to file in cursor.
Counters ๐
<C-A>
: Increment number.<C-X>
: Decrement number.
Windows ๐
z{height}<Cr>
: Resize pane to {height} lines tall.
Tags ๐
:tag Classname
: Jump to first definition of Classname.<C-]>
: Jump to definition.g]
: See all definitions.<C-T>
: Go back to last tag.<C-O> <C-I>
: Back/forward.:tselect Classname
: Find definitions of Classname.:tjump Classname
: Find definitions of Classname (auto-select 1st).
Case ๐
You can do these in visual or normal mode.
~
: Toggle case (Case => cASE).gU
: Uppercase.gu
: Lowercase.gUU
: Uppercase current line (alsogUgU
).guu
: Lowercase current line (alsogugu
).
Misc (more) ๐
.
: Repeat last command.]p
: Paste under the current indentation level.:set ff=unix
: Convert Windows line endings to Unix line endings.
Command line ๐
<C-R><C-W>
: Insert current word into the command line.<C-R>"
: Paste from " register.<C-X><C-F>
: Auto-completion of path in insert mode.
Text alignment ๐
:center [width]
:right [width]
:left
See :help formatting
.
Calculator ๐
Do this in insert mode.
<C-R>=128/2
: Shows the result of the division : โ64โ.
Exiting with an error ๐
:cq
:cquit
Works like :qa
, but throws an error. Great for aborting Git commands.
Marks ๐
- ``^`: Last position of cursor in insert mode.
- ``.`: Last change in current buffer.
- ``"`: Last exited current buffer.
- ``0`: In last file edited.
''
: Back to line in current buffer where jumped from.``
: Back to position in current buffer where jumped from.- ``[`: To beginning of previously changed or yanked text.
- ``]`: To end of previously changed or yanked text.
- ``<`: To beginning of last visual selection.
- ``>`: To end of last visual selection.
ma
: Mark this cursor position as a.- ``a`: Jump to the cursor position a.
'a
: Jump to the beginning of the line with position a.d'a
: Delete from current line to line of mark a.d
a`: Delete from current position to position of mark a.c'a
: Change text from current line to line of a.y
a`: Yank text from current position to position of a.:marks
: List all current marks.:delm a
: Delete mark a.:delm a-d
: Delete marks a, b, c, d.:delm abc
: Delete marks a, b, c.
Spell checking ๐
:set spell spelllang=en_us
: Turn on US English spell checking.]s
: Move to next misspelled word after the cursor.[s
: Move to previous misspelled word before the cursor.z=
: Suggest spellings for the word under/after the cursor.zg
: Add word to spell list.zw
: Mark word as bad/mispelling.zu
orC-X
(Insert Mode): Suggest words for bad word under cursor from spellfile.
See :help spell
.
Common & Useful Examples ๐
$_
: Delete the current line.vi(
: highlight/select the text within ().vi"
: hightlight/select the text between “”.ce
orcw
: change the word.
More resources & learning sources ๐
I hope this post helps you. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .