Vim Cheat Sheet
Tags: vim, development, terminal, cheetsheet
Published: 2021-01-10 23:45:07



### Global

> :__h[elp]__ keyword - open help for keyword
> __:sav[eas]__ file - save file as
> __:clo[se]__ - close current pane
> __:ter[minal]__ - open a terminal window
__K __- open man page for word under the cursor

---


## *Tip Run vimtutor in a terminal to learn the first Vim commands.*

# Cursor movement
>__h__ - move cursor left
__j__ - move cursor down
__k__ - move cursor up
__l__ - move cursor right
__H__ - move to top of screen
__M__ - move to middle of screen
__L__ - move to bottom of screen
__w__ - jump forwards to the start of a word
__W__ - jump forwards to the start of a word (words can contain punctuation)
__e__ - jump forwards to the end of a word
__E__ - jump forwards to the end of a word (words can contain punctuation)
__b__ - jump backwards to the start of a word
__B__ - jump backwards to the start of a word (words can contain punctuation)
__%__ - move to matching character (default supported pairs: '()', '{}', '[]' 
__- use :h__ matchpairs in vim for more info)
__0__ - jump to the start of the line
__^__ - jump to the first non-blank character of the line
__$__ - jump to the end of the line
__g___ - jump to the last non-blank character of the line
__gg__ - go to the first line of the document
__G__ - go to the last line of the document
__5gg or 5G __- go to line 5
__fx__ - jump to next occurrence of character x
__tx__ - jump to before next occurrence of character x
__Fx__ - jump to previous occurence of character x
__Tx__ - jump to after previous occurence of character x
__;__ - repeat previous f, t, F or T movement
__,__ - repeat previous f, t, F or T movement, backwards
__}__ - jump to next paragraph (or function/block, when editing code)
__{ __- jump to previous paragraph (or function/block, when editing code)
__zz__ - center cursor on screen
__Ctrl + e __- move screen down one line (without moving cursor)
__Ctrl + y__ - move screen up one line (without moving cursor)
__Ctrl + b__ - move back one full screen
__Ctrl + f__ - move forward one full screen
__Ctrl + d__ - move forward 1/2 a screen
__Ctrl + u__ - move back 1/2 a screen

### Tip Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
>__Insert mode - inserting/appending text__
__i__ - insert before the cursor
__I __- insert at the beginning of the line
__a __- insert (append) after the cursor
__A__ - insert (append) at the end of the line
__o__ - append (open) a new line below the current line
__O__ - append (open) a new line above the current line
__ea__ - insert (append) at the end of the word
__Ctrl + h__ - delete the character before the cursor during insert mode
__Ctrl + w__ - delete word before the cursor during insert mode
__Ctrl + j__ - begin new line during insert mode
__Ctrl + t__ - indent (move right) line one shiftwidth during insert mode
__Ctrl + d__ - de-indent (move left) line one shiftwidth during insert mode
__Ctrl + n__ - insert (auto-complete) next match before the cursor during insert mode
__Ctrl + p __ - insert (auto-complete) previous match before the cursor during insert mode
__Esc__ - exit insert mode

---



### Editing
> __r__ - replace a single character
__J__ - join line below to the current one with one space in between
__gJ__ - join line below to the current one without space in between
__gwip__ - reflow paragraph
__g~__ - switch case up to motion
__gu__ - change to lowercase up to motion
__gU__ - change to uppercase up to motion
__cc__ - change (replace) entire line
__C__ - change (replace) to the end of the line
__c$__ - change (replace) to the end of the line
__ciw__ - change (replace) entire word
__cw__ - change (replace) to the end of the word
__s__ - delete character and substitute text
__S__ - delete line and substitute text (same as cc)
__xp__ - transpose two letters (delete and paste)
__u__ - undo
__U__ - restore (undo) last changed line
__Ctrl + r __- redo
__.__ - repeat last command

---



### Marking text (visual mode)
> __v__ - start visual mode, mark lines, then do a command (like y-yank)
__V__ - start linewise visual mode
__o__ - move to other end of marked area
__Ctrl + v__ - start visual block mode
__O__ - move to other corner of block
__aw__ - mark a word
__ab__ - a block with ()
__aB__ - a block with {}
__at__ - a block with "<" ">" tags
__ib__ - inner block with ()
__iB__ - inner block with {}
__it__ - inner block with "<" ">" tags
__Esc__ - exit visual mode

---
### Tip Instead of b or B one can also use ( or { respectively.

# Visual commands
> __> __- shift text right
__<__ - shift text left
__y__ - yank (copy) marked text
__d__ - delete marked text
__~__ - switch case
__u__ - change marked text to lowercase
__U__ - change marked text to uppercase
Registers
__:reg[isters]__ - show registers content
__"xy__ - yank into register x
__"xp__ - paste contents of register x
__"+y__ - yank into the system clipboard register
__"+p__ - paste from the system clipboard register
__Tip Registers are being stored in ~/.viminfo, and will be loaded again on next restart of vim.__
__Tip Special registers:__
 __0__ - last yank
__ "__ - unnamed register, last delete or yank
__ %__ - current file name
 

---
##  alternate file name
 
 
> __* __- clipboard contents (X11 primary)
__ +__ - clipboard contents (X11 clipboard)
__ /__ - last search pattern
 __:__ - last command-line
__ .__ - last inserted text
__ - __- last small (less than a line) delete
__ = __- expression register
__ _ __- black hole register

---

## Marks and positions
> __:marks__ - list of marks
__ma__ - set current position for mark A
__`a__ - jump to position of mark A
__y`a__ - yank text to position of mark A
__`0__ - go to the position where Vim was previously exited
__`"__ - go to the position when last editing this file
__`.__ - go to the position of the last change in this file
__``__ - go to the position before the last jump
__:ju[mps]__ - list of jumps
__Ctrl + i__ - go to newer position in jump list
__Ctrl + o__ - go to older position in jump list
__:changes__ - list of changes
__g,__ - go to newer position in change list
__g;__ - go to older position in change list
__Ctrl + ]__ - jump to the tag under cursor

__Tip To jump to a mark you can either use a backtick (`) or an apostrophe ('). Using an apostrophe jumps to the beginning (first non-black) of the line holding the mark.__

---
### __Macros____

>__ qa__ - record macro a
__q__ - stop recording macro
__@a __- run macro a
__@@__ - rerun last run macro
__Cut and paste__
__yy__ - yank (copy) a line
__2yy__ - yank (copy) 2 lines
__yw__ - yank (copy) the characters of the word from the cursor position to the start of the next word
__y$__ - yank (copy) to end of line
__p__ - put (paste) the clipboard after cursor
__P__ - put (paste) before cursor
__dd__ - delete (cut) a line
__2dd__ - delete (cut) 2 lines
__dw__ - delete (cut) the characters of the word from the cursor position to the start of the next word
__D__ - delete (cut) to the end of the line
__d$__ - delete (cut) to the end of the line
__x__ - delete (cut) character

---
### Tabs
> __:tabnew__ or :tabnew {page.words.file} - open a file in a new tab
__Ctrl + wT__ - move the current split window into its own tab
__gt or :tabn[ext]__ - move to the next tab
__gT or :tabp[revious]__ - move to the previous tab
__#gt __- move to tab number #
__:tabm[ove] #__ - move current tab to the #th position (indexed from 0)
__:tabc[lose] __- close the current tab and all its windows
__:tabo[nly] __- close all tabs except for the current one
__:tabdo command__ - run the command on all tabs (e.g. :tabdo q - closes all opened tabs)

__Working with multiple files__

> __:e[dit]__ file - edit a file in a new buffer
__:bn[ext]__ or :bn - go to the next buffer
__:bp[revious]__ or :bp - go to the previous buffer
__:bd[elete]__ - delete a buffer (close a file)
__:b#__ - go to a buffer by #
__:b file__ - go to a buffer by file
__:ls __- list all open buffers
__:sp[lit] file__ - open a file in a new buffer and split window
__:vs[plit] file__ - open a file in a new buffer and vertically split window
__:vert[ical] ba[ll]__ - edit all buffers as vertical windows
__:tab ba[ll]__ - edit all buffers as tabs
__Ctrl + ws__ - split window
__Ctrl + wv__ - split window vertically
__Ctrl + ww __ - switch windows
__Ctrl + wq__ - quit a window
__Ctrl + wx__ - exchange current window with next one
__Ctrl + w=__ - make all windows equal height & width
__Ctrl + wh __- move cursor to the left window (vertical split)
__Ctrl + wl __- move cursor to the right window (vertical split)
__Ctrl + wj __- move cursor to the window below (horizontal split)
__Ctrl + wk __- move cursor to the window above (horizontal split)

---
## Diff
> __zf__ - manually define a fold up to motion
__zd __- delete fold under the cursor
__za__ - toggle fold under the cursor
__zo__ - open fold under the cursor
__zc__ - close fold under the cursor
__zr__ - reduce (open) all folds by one level
__zm__ - fold more (close) all folds by one level
__zi__ - toggle folding functionality
__]c__ - jump to start of next change
__[c __ - jump to start of previous change
__do or :diffg[et]__ - obtain (get) difference (from other buffer)
__dp or :diffpu[t]__ - put difference (to other buffer)
__:diffthis__ - make current window part of diff
__:dif[fupdate]__ - update differences
__:diffo[ff]__ - switch off diff mode for current window

---
__Tip The commands for folding (e.g. za) operate on one level. To operate on all levels, use uppercase letters (e.g. zA).__
__Tip To view the differences of files, one can directly start Vim in diff mode by running vimdiff in a terminal. One can even set this as git difftool.__


## ThankYou 😊