Skip to content

VIM default keystrokes

sergeylukin edited this page Oct 27, 2012 · 57 revisions

Below you'll find list of keystrokes/commands built-in VIM by default which I'm using frequently. It is important to note that most (maybe even all) VIM commands related to text search/manipulation affect only first occurence by default.

General

Normal mode

  • ~ - invert case (upper->lower; lower->upper) of current character
  • xp - swap two characters around
  • dj - cut current line and next line
  • CTRL-a, CTRL-x - increment, decrement next number on same line as the cursor
  • J - join the next line to the current line
  • f( - moves the cursor to the first occurence of "("
  • t( - moves the cursor right before the first occurence of "("
  • di" - removes the contents that is being wrapped by double quotes on the same line
  • ci" - removes the contents that is being wrapped by double quotes on the same line and switches to Insert mode
  • da" - removes the double quotes with contents inside
  • M - move cursor to the middle of the screen
  • H - move cursor to the top of the screen
  • 3H - move cursor to the 3rd line from top
  • L - move cursor to the bottom of the screen
  • 3L - move cursor to the 3rd line from bottom
  • CTRL-u - move the screen up by half
  • CTRL-d - move the screen down by half
  • CTRL-f - move the whole screen down
  • CTRL-b - move the whole screen up
  • CTRL-e - move the screen up by one line
  • CTRL-y - move the screen down by one line
  • zz - move the screen so that current line would be in the middle
  • zt - move the screen so that current line would be in the top
  • 100zt - move the screen so that line number 100 would be in the top
  • zb - move the screen so that current line would be in the bottom
  • ' ' - move the cursor to previously visited line
  • > > - indent current line
  • 2 > > - indent current line and next line
  • < < - remove one indent for current line
  • = = - auto-indent current line
  • = G - auto-indent everything from current line to the end of file
  • zf5j - fold current line and 5 more lines below it
  • zo - expand fold
  • zc - collapse fold

Visual mode

  • gq - Break selected line into multiple lines
  • gu - lowercase line
  • gU - uppercase line
  • o - move the cursor the the opposite end of selection (very useful if you want to add/remove selection from opposite side)
  • :!ls - pass selected content to the "ls" command and paste in the output

Command mode

  • :set paste - enables paste mode which disables all automatic formatting. Highly useful when pasting some preformatted piece of code.
  • :set nopaste - disables paste mode
  • :set list - show tabs (displayed as "^I") and line endings (displayed as "$")
  • :set ft=javascript - set the filetype of current file to "javascript"
  • :!date - run date command in your shell and print the output
  • :read !date - run date command in your shell and paste the output in editor
  • :read !curl --silent http://example.com/file.txt - download a file via curl and paste it's content in editor

Insert mode

  • CTRL-t, CTRL-d - add indentation, remove indentation for current line

Macros

Recording a macro

  • Press q
  • Press any letter or number to assign macro to
  • Start typing commands to save in macro
  • Finish recording by pressing q

Using an existing macro

  • Press @
  • Press letter or number that represents desired macro or @ to run last executed macro

Markers

Saving a marker

  • Press m
  • Press any letter or number to assign marker to

Move cursor to a saved marker

  • Press '
  • Press letter or number that represents desired marker

Buffers

  • :ls - prints all buffers
  • :bf - switch to the first buffer
  • :bp - switch to previous buffer
  • :bn - switch to next buffer
  • :b# - switch to last visited buffer
  • :b2 - switch to buffer number 2
  • :bd - delete current buffer (in other words close the file)
  • :bd2 - delete buffer number 2

Splits

  • :sp file.txt - split current window horizontally and open file.txt
  • :vs file.txt - split current window vertically and open file.txt
  • :sb2 - split current window horizontally and open in it buffer number 2
  • :vert sb2 - split current window vertically and open in it buffer number 2
  • CTRL-w l - navigate to the closest window from the right
  • CTRL-w h - navigate to the closest window from the left
  • CTRL-w j - navigate to the closest window from the bottom
  • CTRL-w k - navigate to the closest window from the top
  • CTRL-w L - move current window to the most right
  • CTRL-w H - move current window to the most left
  • CTRL-w J - move current window to the most bottom
  • CTRL-w K - move current window to the most top
  • CTRL-w + - increase the height of current window by one line
  • CTRL-w - - decrease the height of current window by one line
  • CTRL-w > - increase the width of current window by one column
  • CTRL-w < - decrease the width of current window by one column
  • CTRL-w = - equally distribute the heights and the widths between visible windows
  • CTRL-w T - moves current window to a new separate tab

Tabs

  • :tabe file.txt - create a new tab and open in it file.txt
  • gt - move to the next tab
  • gT - move to the previous tab