clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Vim commands

Filed under: Webdev— Tagged with: text editor, vim

Here’s a comprehensive Vim command cheat cheat.

Cursor movement

hmove cursor left
jmove cursor down
kmove cursor up
lmove cursor right
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)
0jump to the start of the line
^jump to the first non-blank character of the line
$jump to the end of the line
Ggo to the last line of the document
5Ggo to line 5

Insert mode - inserting/appending text

iinsert before the cursor
Iinsert at the beginning of the line
ainsert (append) after the cursor
Ainsert (append) at the end of the line
oappend (open) a new line below the current line
Oappend (open) a new line above the current line
eainsert (append) at the end of the word
Escexit insert mode

Editing

rreplace a single character
Jjoin line below to the current one
ccchange (replace) entire line
cwchange (replace) to the end of the word
c$change (replace) to the end of the line
sdelete character and substitute text
Sdelete line and substitute text (same as cc)
xptranspose two letters (delete and paste)
uundo
Ctrl+rredo
.repeat last command

Marking text (visual mode)

vstart visual mode, mark lines, then do a command (like y-yank)
Vstart linewise visual mode
omove to other end of marked area
Ctrl+vstart visual block mode
Omove to other corner of block
awmark a word
aba block with ()
aBa block with
ibinner block with ()
iBinner block with
Escexit visual mode

Visual commands

>shift text right
<shift text left
yyank (copy) marked text
ddelete marked text
~switch case

Cut and paste

yyyank (copy) a line
2yyyank (copy) 2 lines
ywyank (copy) word
y$yank (copy) to end of line
pput (paste) the clipboard after cursor
Pput (paste) before cursor
dddelete (cut) a line
2dddelete (cut) 2 lines
dwdelete (cut) word
Ddelete (cut) to the end of the line
d$delete (cut) to the end of the line
xdelete (cut) character

Exiting

:wwrite (save) the file, but don't exit
:wqwrite (save) and quit
:xwrite (save) and quit
:qquit (fails if there are unsaved changes)
:q!quit and throw away unsaved changes

Search and replace

/patternsearch for pattern
?patternsearch backward for pattern
nrepeat search in same direction
Nrepeat search in opposite direction
:%s/old/new/greplace all old with new throughout file
:%s/old/new/gcreplace all old with new throughout file with confirmations

Working with multiple files

:e filenameedit a file in a new buffer
:bnext:bngo to the next buffer
:bprev:bpgo to the previous buffer
:bddelete a buffer (close a file)
:sp filenameopen a file in a new buffer and split window
:vsp filenameopen a file in a new buffer and vertically split window
Ctrl+wssplit window
Ctrl+wwswitch windows
Ctrl+wqquit a window
Ctrl+wvsplit window vertically
Ctrl+whmove cursor to next buffer (right)
Ctrl+wlmove cursor to previous buffer (left)

Tabs

:tabnew filename:tabn filenameopen a file in a new tab
Ctrl+wtmove the current split window into its own tab
gt:tabnext:tabnmove to the next tab
gT:tabprev:tabpmove to the previous tab
#gtmove to tab number #
:tabmove #move current tab to the #th position (indexed from 0)
:tabclose:tabcclose the current tab and all its windows
:tabonly:taboclose all tabs except for the current one

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • console.log('Smash the patriarchy!')
  • I love u!