Vim - Commands

Updated: 2020-06-29

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.

Compound Command

  • C: c$
  • s: cl
  • S: ^C
  • I: ^i
  • A: $a
  • o: A
  • 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

Moving Around

  • 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.
  • : 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

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

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

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

Others

  • J join two lines
  • CTRL-G: displaying the line number
  • CTRL-L: redraw screen
  • <Ctrl-a>: add 1
  • <Ctrl-x>: minus 1

Find Global :echo $VIM

Find Personal :echo HOME:echoHOME :echoMYVIMRC

:echo $VIMRUNTIME /usr/share/vim/vim72

execute linux command:

:!<cmd>

Save:

:wq<Enter>

or

ZZ