logo

Cheatsheets - Vim

Last Updated: 2023-08-20

Movement

Movement Commands
←, ↓, ↑, → h, j, k, l
To first character of next line +
To first character of previous line -
To end of word e or E
Forward by word w or W
Backward by word b or B
To end of line $
To beginning of line 0
Movement Command
Scroll forward one screen ^F
Scroll backward one screen ^B
Scroll forward half screen ^D
Scroll backward half screen ^U
Scroll forward one line ^E
Scroll backward one line ^Y
Move current line to top of screen and scroll z ENTER
Move current line to center of screen and scroll z.
Move current line to bottom of screen and scroll z-
Redraw the screen ^L
Move to home—the top line of screen H
Move to middle line of screen M
Move to bottom line of screen L
Move to first character of next line ENTER
Move to first character of next line +
Move to first character of previous line -
Move to first nonblank character of current line ^
Move to end of word e
Move to end of word (ignore punctuation) E
Move to beginning of current sentence (
Move to beginning of next sentence )
Move to beginning of current paragraph {
Move to beginning of next paragraph }
Move to beginning of current section [[
Move to beginning of next section ]]
Search forward for pattern / pattern
Search backward for pattern ? pattern
Repeat last search n
Repeat last search in opposite direction N
Repeat last search forward /
Repeat last search backward ?
Move to next occurrence of x in current line f x
Move to previous occurrence of x in current line F x
Move to just before next occurrence of x in current line t x
Move to just after previous occurrence of x in current line T x
Repeat previous find command in same direction ;
Repeat previous find command in opposite direction ,
Go to given line n n G
Go to end of file G
Return to previous mark or context ``
Return to beginning of line containing previous mark ''
Show current line (not a movement command) ^G

Details:

  • 0(zero): Move to beginning of line.
  • $: Move to end of line.
  • H: Move to the top line(home) on screen.
  • M: Move to middle line on screen.
  • L: Move to last line on screen.
  • n H: Move to n lines below top line.
  • n L: Move to n lines above last line.
  • <ENTER>: Move to first character of next line.
  • +: Move to first character of next line.
  • -: Move to first character of previous line.
  • ^: Move to first nonblank character of current line.
  • n |: Move to column n of current line.
  • G: end of file
  • 1G: beginning of file
  • {num}G: move to a specific line, e.g. move to line 5: 5G
  • <Ctrl-f>: Scroll forward one screen.
  • <Ctrl-b>: Scroll backward one screen.
  • <Ctrl-d>: Scroll forward half screen (down).
  • <Ctrl-u>: Scroll backward half screen (up).
  • <Ctrl-e>: scroll the screen up one line, cursor does not move.
  • <Ctrl-y>: scroll the screen down one line, cursor does not move.
  • z<ENTER>: Move current line to top of screen and scroll.
  • z.: Move current line to center of screen and scroll.
  • z-: Move current line to bottom of screen and scroll.
  • 200z: ENTER moves line 200 to the top of the screen.
  • { beginning of paragraph
  • } end of paragraph
  • ( beginning of sentence
  • ) end of sentence
  • w beginning of next word
  • W not counting symbols and punctuation
  • b beginning of prev word
  • B backward
  • e end of word
  • E Move to end of word (ignore punctuation).
  • g, next change
  • g; prev change
  • gd goto definition, search from start of the section
  • gD goto global definition, search from line 1
  • % goto next block
  • [[ prev section, start
  • [] prev section, end
  • ][ next section, end
  • ]] next section, start
  • [{ start of the block
  • ]} end of the block
  • [/ start of the comment
  • ]/ end of the comment

Editing

Editing action Command
Insert text at current position i
Insert text at beginning of line I
Append text at current position a
Append text at beginning of line A
Open new line below cursor for new text o
Open new line above cursor for new text O
Delete line and substitute text S
Overstrike existing characters with new text R
Join current and next line J
Toggle case ~
Repeat last action .
Undo last change u
Restore line to original state U

Change, Delete, Yank

Text object Change Delete Copy
One word cw dw yw
Two words, excl. punctuation 2cW or c2W 2dW or d2W 2yW or y2W
Three words back 3cb or c3b 3db or d3b 3yb or y3b
One line cc dd yy or Y
To end of line c$ or C d$ or D y$
To beginning of line c0 d0 y0
Single character r x or X yl or yh
Five characters 5s 5x 5yl

c: Change

Change means delete some characters and enter insert mode so you can type some thing else

  • cw: To the end of a word
  • c2b: Back two words
  • c$, C: To the end of line
  • c0: To the beginning of line
  • caw: change the whole word, symbols count as separate word, a,b,c=> ,b,c
  • caW: change the whole word, symbols do not count as separate word a,b,c => ``
  • cc: change the whole line
  • s: remove one character and enter INSERT mode
  • {num}s: remove specified number of characters and enter INSERT mode
  • S: remove the whole line, same to cc
  • r: replace a single character
  • R: enter REPLACE mode, replace characters until ESC is pressed.
  • {num}r

Example: abcdef

  • 3sz<ESC> => zdef: remove 3 characters and enter insert mode, enter z, press ESC to exit INSERT mode
  • 3rz => zzzdef: replace 3 consecutive characters to z

~ switch between uppercase and lowercase

d: Delete

  • dw: delete word and space after the word
  • de: delete word but preserve space
  • db: delete backward
  • d0: delete to the beginning of the line
  • d$, D: delete to the end of the line
  • dG: delete to the end of the file
  • d?{pattern}: Delete from before the cursor up to the pattern, the pattern is deleted.
  • d/{pattern}: delete from cursor to the pattern, the pattern is preserved.
  • df{char}: deletes up to and including the named character x.
  • x: delete a single character
  • X: delete the character before the cursor.
  • xp (delete character and put after cursor) to transpose two letters.
  • "3p copy from 3rd register

This works only for a deleted line. Words, or a portion of a line, are not saved in a buffer. If you want to restore a deleted word or line fragment, and u won’t work, use the p command by itself.

  • <C-h>: Delete back one character (backspace)
  • <C-w>: Delete back one word
  • <C-u>: Delete back to start of line

y: Yank

Y = yy: copy a whole line

  • p: paste after this line
  • P: paste above this line

Yanking uses the same buffer as deleting.

Search and Replace

Search

  • /{pattern} search forward
  • ?{pattern} search backward
  • n: Repeat search in same direction.
  • N: Repeat search in opposite direction.
  • /<ENTER> Repeat search forward.
  • ?<ENTER> Repeat search backward.

inline search

  • f{char}: Find (move cursor to) next occurrence of x in the line, where x stands for any character.
  • F{char}: Find (move cursor to) previous occurrence of x in the line.
  • t{char}: Find (move cursor to) character before next occurrence of x in the line.
  • T{char}: Find (move cursor to) character after previous occurrence of x in the line.
  • ;: Repeat previous find command in same direction.
  • ,: Repeat previous find command in opposite direction.

Replace

:s vs :%s:

  • :s: Search current line, :s/Search/Replace/FLAG
  • :%s: Search whole text, :%s/Search/Replace/FLAG

FLAGs:

  • g: global, replace all occurrences
  • c: confirm before replace
  • i: ignore cases

Matching

:match Group /pattern/

Group: color group pattern: regex

see the complete color group

:so $VIMRUNTIME/syntax/hitest.vim

e.g.

:match ErrorMsg /^Error/

remove highlight

:match NONE

Mark color characters after a certain column: every character after the 73rd character will be marked as an error

:match ErrorMsg /\%>73v.\+/

Mark tabs not used for indentation in code

:match ErrorMsg /[^\t]\zs\t\+/

Compound Command

  • C: c$
  • s: cl
  • S: ^C
  • I: ^i
  • A: $a
  • o: A<CR>
  • O: ko

Repeat and Reverse

  • u: undo
  • .: repeat

Vim records our keystrokes until we leave Insert mode

Intent Act Repeat Reverse
Make a change {edit} . u
Scan line for next character f{char}/t{char} ; ,
Scan line for previous character F{char}/T{char} ; ,
Scan document for next match /pattern n N
Scan document for previous match ?pattern n N
Perform substitution :s/target/replacement & u
Execute a sequence of changes qx{changes}q @x u

Indentation

  • >>: Indent line by shiftwidth spaces
  • <<: De-indent line by shiftwidth spaces
  • 5>>: Indent 5 lines
  • 5==: Re-indent 5 lines
  • ``>%`: Increase indent of a braced or bracketed block (place cursor on brace first)
  • =%: Reindent a braced or bracketed block (cursor on brace)
  • <%: Decrease indent of a braced or bracketed block (cursor on brace)
  • ]p: Paste text, aligning indentation with surroundings
  • =i{: Re-indent the 'inner block', i.e. the contents of the block
  • =a{: Re-indent 'a block', i.e. block and containing braces
  • =2a{: Re-indent '2 blocks', i.e. this block and containing block
  • >i{: Increase inner block indent
  • <i{: Decrease inner block indent
  • Vjj>: Visually mark and then indent 3 lines
  • gg=G: Re-indent entire buffer

Indent range: Go to the first line of the range and enter ma to place marker A. Then go to the last line and enter >'a to indent from here to marker A.

Modes

  • Normal Mode
  • Insert Mode
  • Replace Mode
  • Visual Mode

Switch modes:

  • <Esc>: Switch to Normal mode
  • <C-[>: Switch to Normal mode
  • <C-o>: Switch to Insert Normal mode

Enter Insert Mode

  • a
  • A: Append text to end of current line.
  • i
  • I: Insert text at beginning of line.
  • o: Open blank line below cursor for text.
  • O: Open blank line above cursor for text.
  • s: Delete character at cursor and substitute text.
  • S: Delete line and substitute text.

Enter Replace Mode

  • R

Command-line mode commands

  • :: Use Command-Line mode to execute an Ex command
  • /: Use Command-Line mode to perform a forward search
  • ?: Use Command-Line mode to perform a backward search
  • =: Use Command-Line mode to evaluate a Vim script expression

Multi Windows

switch between windows:

  • <Ctrl-w> <Ctrl-w>
  • <Ctrl-w> j: down (Move the current window to the bottom of the screen, using the full width of the screen.)
  • <Ctrl-w> k: up (Move the current window to the top of the screen, using the full width of the screen.)
  • <Ctrl-w> h: left (Move the current window to the left of the screen, using the full height of the screen.)
  • <Ctrl-w> l: right (Move the current window to the right of the screen, using the full height of the screen.)
  • <Ctrl-w> t: top
  • <Ctrl-w> b: bottom

rotate windows:

  • <Ctrl-w> r
  • <Ctrl-w> R
  • <Ctrl-w> x
  • <Ctrl-w> X

ctrl-z + fg

  • inside vim
  • ctrl-z, back to terminal, do something else
  • fg back to vim

Operator Commands

  • c: Change
  • d: Delete
  • y: Yank into register
  • g~: Swap case
  • gu: Make lowercase
  • gU: Make uppercase
  • >: Shift right
  • <: Shift left
  • =: Autoindent

Usage:

  • (command)(text object)
  • (command){num}(text object)
  • {num}(command)(text object)

where text object is a movement command

When an operator command is invoked in duplicate, it acts upon the current line. So dd deletes the current line, while >> indents it. The gU command is a special case. We can make it act upon the current line by running either gUgU or the shorthand gUU.

Refresh/Reload File

To refresh/reload a file without close and reopen, use

:e

to force reload with changes:

:e!

To reload vimrc(equivalent to "source" it):

:so $MYVIMRC
:source $MYVIMRC

so is short for source. You can check your vimrc by

:echo $MYVIMRC

To load another vim script:

:so my_vim_script.vim

Others

Operations Commands
Place text from buffer P or p
Start vi, open file if specified vi file
Save edits, quit file ZZ
No saving of edits, quit file :q!
  • J join two lines
  • CTRL-G: displaying the line number
  • CTRL-L: redraw screen
  • <Ctrl-a>: add 1
  • <Ctrl-x>: minus 1
  • :echo $VIM: Find Global
  • :echo $HOME: Find home
  • :echo $MYVIMRC: Find Personal
  • :echo $VIMRUNTIME
  • :!<cmd>: execute linux command:
  • :wq<Enter> or ZZ: Save
  • Retab: :retab

Learn / Get Help

  • Use :help: e.g. :help uganda or :help 'guioptions'
  • Learn from vimtutor: $ vimtutor
  • Learn by vimgolf: http://vimgolf.com/

Where to Set Configurations

  • Global config: /usr/share/vim/vimrc
  • Personal config: ~/.vimrc (Personal overrule global)
  • Inside vim: in normal mode, press : (Once exit the configs will be lost.)

Also:

  • To use a different config than ~/.vimrc: $ vim -u <vimrc>
  • To use factory setting: $ vim -u NONE

Configs

  • :syntax on: Enable syntax highlight:
  • :set ts=4: Set tab width
  • :set shiftwidth=4: Set shiftwidth (when using command like >>)
  • :set expandtab: Expand tab to space
  • :set laststatus=2: show statusline
  • :set laststatus=0: hide statusline
  • :set guioptions-=m: remove the menu
  • :set guioptions-=T: remove the toolbar
  • :set compatible: compatible
  • :set guifont=Courier\ 14: Change fonts: only work for gvim, space need to be escaped
  • :set guifont=Courier\ New\ 12, Arial\ 10: With alternative
  • :set guifont=*: show font list

statusline

:help 'statusline'
:set statusline format

:set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\
[HEX=\%02.2B]\ [POS=%04l,%04v]\ [%p%%]\ [LEN=%L]

Set cursor horizontal and vertical highlights

:set cursorline
:highlight CursorLine guibg=lightblue ctermbg=lightgray
:set cursorcolumn

Hide

:set nocursorline
:set nocursorcolumn

Show/hide line number

:set number
:set numberwidth=XXX
:set nonumber

spell check

:set spell
:set spelllang=de
:set spelllang=en,da,de,it
:set spellsuggest=X

Change Color Scheme

:colorscheme <ColorScheme>

e.g. :colorscheme default

Find installed schemes in /usr/share/vim/vim<version>/colors

Traverse installed schemes, press tab button after the whitespace: :colorscheme <tab>

Highlight:

:highlight MyGroup ctermbg=red guibg=red gctermfg=yellow
guifg=yellow term=bold
  • ctermb: Background color in console
  • guibg: Background color in Gvim
  • ctermf: Text color in console
  • guifg: Text color in Gvim
  • gui: Font formatting in Gvim
  • term: Font formatting in console (for example, bold)

Hightlighting

Add syntax file to (e.g. pig.vim) ~/.vim/syntax/pig.vim

Add these to ~/.vimrc

augroup filetypedetect
    au BufNewFile,BufRead \*.pig set filetype=pig syntax=pig
augroup END

Show whitespace Characters

:set list
:set nolist

Toggle

:set list!

In Vim, list is a boolean option that defaults to off. If list is on, whitespace characters are made visible. The listchars option can be used to customize the way whitespace characters are shown. The default displays ^I for each tab, and $ at each EOL (end of line, so trailing whitespace can be seen). :help 'list'

The following example toggles list, then sets listchars to not display an end-of-line character, and to display > for the first character occupied by a tab, and - for any subsequent characters that the tab may occupy.

:set list!
:set listchars=tab:>-

Example .vimrc

syntax on
set ts=4
set shiftwidth=4
set expandtab
set backspace=2

Add syntax highligh

syntax on

Use soft tab(spaces instead of tabs)

set expandtab

How To Turn Off Search highlight

To turn off the previous search highlight:

:noh

Or

set nohlsearch

To toggle

set hlsearch!

Backspace(delete) Not Working

Symptom

the backspace does not work

Solution

Add this to ~/.vimrc:

set backspace=2

Example

" Highlight "bad" spaces
let c_space_errors=1

" Also highlight empty lines at EOF.
match ErrorMsg /\s\+$\| \+\ze\t/

" Enable incremental search.
set incsearch

Set vim as default editor

Add this line to your ~/.bashrc file:

export EDITOR=vim

Enable modern Vim features not compatible with Vi spec.

set nocompatible