--- title: Vim date: 2020-11-25 18:28:43 icon: icon-vim background: bg-green-600 tags: - vi - editor - terminal - text categories: - Toolkit intro: | A useful collection of [Vim](http://www.vim.org/) 8.2 quick reference cheat sheets to help you learn vim editor faster. --- Getting started {.cols-3} --------------- ### Motion Diagrams {.row-span-2} ```bash ▼/▶ Cursor ▽/▷ Target ``` #### Left-right motions ```bash ┌───────────── | ├───────────── 0 $ ──────────────┐ │ ┌────────── ^ fe ────────┐ │ │ │ ┌─────── Fo te ───────┐│ │ │ │ │┌────── To 30| ───┐ ││ │ │ │ ││ ┌──── ge w ───┐ │ ││ │ │ │ ││ │ ┌── b e ─┐ │ │ ││ │ │ │ ││ │ │ ┌h l┐ │ │ │ ││ │ ▽ ▽ ▽▽ ▽ ▽ ▽▼ ▼▽ ▽ ▽ ▽ ▽▽ ▽ echo "A cheatsheet from quickref.me" ``` #### Up-down motions ```bash - SCREEN 1 START ┌─┬─────────▷ #!/usr/bin/python │ │ ┌───▷ │ │ │ print("Hello") │ │ { } ▶ print("Vim") │ │ │ print("!") │ │ └─▷ │ │ ┌───┬───▷ print("Welcome") G gg H M L k j ▶ print("to") │ │ └─▷ print("quickref.me") │ │ print("/vim") │ │ │ └─────▷ │ - SCREEN 1 END └──────────────▷ print("SCREEN 2") ``` ### Motions {.row-span-2} | Shortcut | Description | |--------------------------------|-------------------| | `h` _\|_ `j` _\|_ `k` _\|_ `l` | Arrow keys | | `` _/_ `` | Half-page up/down | | `` _/_ `` | Page up/down | {.shortcuts} #### Words{.left-text} | Shortcut | Description | |---------------|---------------------------| | `b` _/_ `w` | Previous/Next word | | `ge` _/_ `e` | Previous/Next end of word | {.shortcuts} #### Line | Shortcut | Description | |----------------------|-----------------------------| | `0` _(zero)_ _/_ `$` | Start/End of line | | `^` | Start of line _(non-blank)_ | {.shortcuts} #### Character | Shortcut | Description | |----------------|-------------------------------------| | `Fe` _/_ `fe` | Move to previous/next `e` | | `To` _/_ `to` | Move before/after previous/next `o` | | `|` _/_ `n|` | Go to first/`n`th column | {.shortcuts} #### Document | Shortcut | Description | |--------------|----------------------------| | `gg` _/_ `G` | First/Last line | | `:n` _\|_ `nG` | Go to line `n` | | `}` _/_ `{` | Next/Previous empty line | {.shortcuts} #### Window | Shortcut | Description | |-------------------------|-----------------------------| | `H` _/_ `M`_/_ `L` | Top/Middle/Bottom screen | | `zt` _/_ `zz` _/_ `zb` | Top/Center/Bottom this line | {.shortcuts} ### Insert Mode | Shortcut | Description | |------------------------|-----------------------------| | `i` _/_ `a` | Insert before/after cursor | | `I` _/_ `A` | Insert start/end of line | | `o` _/_ `O` _(letter)_ | Insert new line below/above | | `s` _/_ `S` | Delete char/line and insert | | `C` | Change to end of line | | `gi` | Insert at last insert point | | `Esc` _\|_ `` | Exit insert mode | {.shortcuts} ### Saving and Exiting | Shortcut | Description | |---------------------------|----------------------------| | `:w` | Save | | `:q` | Close file | | `:wq` _\|_ `:x` _\|_ `ZZ` | Save and quit | | `:wqa` | Save and quit all files | | `:q!` _\|_ `ZQ` | Force quit | | `:qa` | Close all files | | `:qa!` | Force quit all files | | `:w` new.txt | Write to `new.txt` | | `:sav` new.txt | Save and edit `new.txt` | | `:w` !sudo tee % | Write to readonly file | {.shortcuts} ### Normal Mode | Shortcut | Description | |-----------------------|--------------------------| | `r` | Replace one character | | `R` | Enter Replace mode | | `u` _/_ `3u` | Undo changes `1` / `3` times | | `U` | Undo changes on one line | | `J` | Join with next line | | `` _/_ 5 `` | Redo changes `1` / `5` times | {.shortcuts} ### Cut and paste {.row-span-2} | Shortcut | Description | |------------------|-------------------------------| | `x` | Delete character _(Cut)_ | | `p` _/_ `P` | Paste after/before | | `xp` | Swap two characters | | `D` | Delete to end of line _(Cut)_ | | `dw` | Delete word _(Cut)_ | | `dd` | Delete line _(Cut)_ | | `ddp` | Swap two lines | | `yy` | Yank line _(Copy)_ | | `"*p` _\|_ `"+p` | Paste from system clipboard | | `"*y` _\|_ `"+y` | Paste to system clipboard | {.shortcuts} #### In visual mode | Shortcut | Description | |--------------|---------------------------| | `d` _\|_ `x` | Delete selection _(Cut)_ | | `s` | Replace selection | | `y` | Yank selection _(Copy)_ | {.shortcuts} ### Repeating | Shortcut | Description | |----------|---------------------------------------------| | `.` | Repeat last command | | `;` | Repeat latest `f`, `t`, `F` or `T` | | `,` | Repeat latest `f`, `t`, `F` or `T` reversed | | `&` | Repeat last `:s` | | `@:` | Repeat a command-line command | {.shortcuts} ### Visual mode | Shortcut | Description | |-------------|-------------------------| | `v` | Enter visual mode | | `V` | Enter visual line mode | | `` | Enter visual block mode | | `ggVG` | Select all text | | `>` _/_ `<` | Shift text right/left | {.shortcuts} ### Macros | - | - | |-------|-----------------------| | `qi` | Record macro `i` | | `q` | Stop recording macro | | `@i` | Run macro `i` | | `7@i` | Run macro `i` 7 times | | `@@` | Repeat last macro | {.shortcuts} Operators {.cols-3} --------- ### Usage {.secondary} | Shortcut | Description | |----------|--------------| | `d` | w | | Operator | Motion | Combine [operators](#available-operators) with [motions](#motions) to use them ### Available Operators {.row-span-2} | Shortcut | Description | |----------|---------------------------------| | `d` | Delete | | `y` | Yank _(copy)_ | | `c` | Change _(delete then insert)_ | | `p` | Paste | | `=` | Formats code | | `g~` | Toggle case | | `gU` | Uppercase | | `gu` | Lowercase | | `>` | Indent right | | `<` | Indent left | | `!` | Filter through external program | ### Examples {.row-span-2} | Combination | Description | |----------------------|-----------------------------| | `d`d | Delete current line | | `d`j | Delete two lines | | `d`w | Delete to next word | | `d`b | Delete to beginning of word | | `d`fa | Delete until `a` char | | `d`/hello | Delete until `hello` | | `c`c | Change current line | | `y`y | Copy current line | | `>`j | Indent 2 lines | | gg`d`G | Delete a complete document | | gg`=`G | Indent a complete document | | gg`y`G | Copy a whole document | {.show-header} ### Counts ``` [count] [count] ``` --- | Combination | Description | |------------------|----------------------------| | 2`d`d | Delete `2` lines | | 6`y`y | Copy `6` lines | | `d`3w | Delete `3` words | | `d`5j | Delete `5` lines downwards | | `>`4k | Indent `4` lines upwards | Text objects {.cols-3} ------------ ### Usage {.secondary} | Shortcut | Description | - | |----------|-----------------------------------------------------------|--------------| | `v` |         i _/_ a | p | | Operator | inner _/_ around | Text object | Operate with an [operator](#available-operators) inner or around text blocks ### Text objects {.row-span-2} | Shortcut | Description | |------------------------------------------------------|----------------------------------------| | p | Paragraph | | w | Word | | W | WORD
_(surrounded by whitespace)_ | | s | Sentence | | [ ( { \< | A [], (), or {} block | | ] ) } \> | A [], (), or {} block | | ' " \` | A quoted string | | b | A block [( | | B | A block in [{ | | t | A HTML tag block | See `:help text-objects` ### Delete | Shortcut | Description | |-----------------------------|---------------------------------------| | `d`iw | Delete inner word | | `d`is | Delete inner sentence | | `d`i" | Delete in quotes | | `d`a" | Delete in quotes _(including quotes)_ | | `d`ip | Delete a paragraph | ### Selections | Shortcut | Description | |-----------------------------------------------------|-------------------------------------------| | `v`i" | Select inner quotes "`...`{.underline}" | | `v`a" | Select quotes `"..."`{.underline} | | `v`i[ | Select inner brackets [`...`{.underline}] | | `v`a[ | Select brackets `[...]`{.underline} | | `v`iw | Select inner word | | `v`ip | Select inner paragraph | | `v`ipip | Select more paragraph | ### Misc | Shortcut | Description | |-----------------------------|--------------------------------------| | `c`iw | Change inner word | | `c`i" | Change inner quotes | | `c`it | Change inner tags (HTML) | | `c`ip | Change inner paragraph | | `y`ip | Yank inner paragraph | | `y`ap | Yank paragraph _(including newline)_ | Multiple files {.cols-3} ------------- ### Buffers | - | - | |-------------|----------------------------------| | `:e file` | Edit a file in a new buffer | | `:bn` | Go to the next buffer | | `:bp` | Go to the previous buffer | | `:bd` | Remove file from buffer list | | `:b 5` | Open buffer #5 | | `:b file` | Go to a buffer by file | | `:ls` | List all open buffers | | `:sp file` | Open and split window | | `:vs file` | Open and vertically split window | | `:hid` | Hide this buffer | | `:wn` | Write file and move to next | | `:tab ba` | Edit all buffers as tabs | ### Windows | - | - | |----------------------|-----------------------------| | `` `s` | Split window | | `` `v` | Split window vertically | | `` `w` | Switch windows | | `` `q` | Quit a window | | `` `T` | Break out into a new tab | | `` `x` | Swap current with next | | `` `-` _/_ `+` | Decrease/Increase height | | `` `<` _/_ `>` | Decrease/Increase width | | `` `|` | Max out the width | | `` `=` | Equally high and wide | | `` `h` _/_ `l` | Go to the left/right window | | `` `j` _/_ `k` | Go to the up/down window | {.shortcuts} ### Tabs | Shortcut | Description | |--------------------|-----------------------------------| | `:tabe [file]` | Edit file in a new tab | | `:tabf [file]` | Open if exists in new tab | | `:tabc` | Close current tab | | `:tabo` | Close other tabs | | `:tabs` | List all tabs | | `:tabr` | Go to first tab | | `:tabl` | Go to last tab | | `:tabm 0` | Move to position `0` | | `:tabn` | Go to next tab | | `:tabp` | Go to previous tab | #### Normal mode | Shortcut | Description | |----------|-------------------------------| | `gt` | Go to next tab | | `gT` | Go to previous tab | | `2gt` | Go to tab number `2` | {.shortcuts} Search and Replace {.cols-3} ------------------ ### Search | - | - | |----------|-------------------------------------------------| | `/foo` | Search forward | | `/foo\c` | Search forward _(case insensitive)_ | | `?foo` | Search backward | | `/\v\d+` | Search with [regex](/regex) | | `n` | Next matching search pattern | | `N` | Previous match | | `*` | Search for current word forward | | `#` | Search for current word backward | {.shortcuts} ### Replace LINE ```vim :[range]s/{pattern}/{str}/[flags] ``` --- | | | |-------------------|---------------------------------| | `:s/old/new` | Replace first | | `:s/old/new/g` | Replace all | | `:s/\vold/new/g` | Replace all with [regex](/regex)| | `:s/old/new/gc` | replace all _(Confirm)_ | | `:s/old/new/i` | Ignore case replace first | | `:2,6s/old/new/g` | Replace between lines `2`-`6` | ### Replace FILE ```vim :%s/{pattern}/{str}/[flags] ``` --- | | | |-------------------|-----------------------------| | `:%s/old/new` | Replace first | | `:%s/old/new/g` | Replace all | | `:%s/old/new/gc` | Replace all _(Confirm)_ | | `:%s/old/new/gi` | Replace all _(ignore case)_ | | `:%s/\vold/new/g` | Replace all with [regex](/regex)| ### Ranges {.row-span-2} | - | - | |---------|--------------------| | `%` | Entire file | | `’<,’>` | Current selection | | `5` | Line `5` | | `5,10` | Lines `5` to `10` | | `$` | Last line | | `2,$` | Lines `2` to Last | | `.` | Current line | | `,3` | Next `3` lines | | `-3,` | Forward `3` lines | ### Global command {.row-span-2} ```vim :[range]g/{pattern}/[command] ``` --- | | | |--------------|------------------------------------| | `:g/foo/d` | Delete lines containing `foo` | | `:g!/foo/d` | Delete lines not containing `foo` | | `:g/^\s*$/d` | Delete all blank lines | | `:g/foo/t$` | Copy lines containing `foo` to EOF | | `:g/foo/m$` | Move lines containing `foo` to EOF | | `:g/^/m0` | Reverse a file | | `:g/^/t.` | Duplicate every line | ### Inverse :g ```vim :[range]v/{pattern}/[command] ``` --- | | | |---------------|------------------------------------------------------------| | `:v/foo/d` | Delete lines not containing `foo`
_(also `:g!/foo/d`)_ | ### Flags | - | - | |-----|---------------------------| | `g` | Replace all occurrences | | `i` | Ignore case | | `I` | Don't ignore case | | `c` | Confirm each substitution | ### Substitute expression (magic) | - | - | |--------------|----------------------------------| | `&` _\|_ `\0`| Replace with the whole matched | | `\1`...`\9` | Replace with the group 0-9 | | `\u` | Uppercase next letter | | `\U` | Uppercase following characters | | `\l` | Lowercase next letter | | `\L` | Lowercase following characters | | `\e` | End of `\u`, `\U`, `\l` and `\L` | | `\E` | End of `\u`, `\U`, `\l` and `\L` | ### Examples {.col-span-2} ```c {.wrap} :s/a\|b/xxx\0xxx/g # Modifies "a b" to "xxxaxxx xxxbxxx" :s/test/\U& file/ # Modifies "test" to "TEST FILE" :s/\(test\)/\U\1\e file/ # Modifies "test" to "TEST file" :s/\v([abc])([efg])/\2\1/g # Modifies "af fa bg" to "fa fa gb" :s/\v\w+/\u\0/g # Modifies "bla bla" to "Bla Bla" :s/\v([ab])|([cd])/\1x/g # Modifies "a b c d" to "ax bx x x" :%s/.*/\L&/ # Modifies "HTML" to "html" :s/\v<(.)(\w*)/\u\1\L\2/g # Make every first letter of a word uppercase :%s/^\(.*\)\n\1/\1/ # Remove duplicate lines :%s/<\/\=\(\w\+\)\>/\U&/g # Convert HTML-Tags to uppercase :g/^pattern/s/$/mytext # Find and append text to the end :g/pattern/norm! @i # Run a macro on matching lines /^\(.*\)\(\r\?\n\1\)\+$ # View the duplicates lines :v/./,/./-j # Compress blank lines into a blank line :g//,//d # Delete inclusively from to ``` Vimdiff {.cols-3} ------- ### Usage {.secondary} ```shell script $ vimdiff file1 file2 [file3] $ vim -d file1 file2 [file3] ``` ### Editing {.row-span-2} ``` :[range]diffget [bufspec] :[range]diffput [bufspec] ``` --- | Shortcut | Description | |---------------------|-------------------------| | `do` _/_ `:diffget` | Obtain (get) difference | | `dp` _/_ `:diffput` | Put difference | | `:dif` | Re-scan differences | | `:diffo` | Switch off diff mode | | `:1,$+1diffget` | Get all differences | | `ZQ` | Quit without changes | {.shortcuts} See: [Ranges](#ranges) ### Folds {.row-span-2} | Shortcut | Description | | --- | --- | | `zo` _/_ `zO` | Open | | `zc` _/_ `zC` | Close | | `za` _/_ `zA` | Toggle | | `zv` | Open folds for this line | | `zM` | Close all | | `zR` | Open all | | `zm` | Fold more _(foldlevel += 1)_ | | `zr` | Fold less _(foldlevel -= 1)_ | | `zx` | Update folds | {.shortcuts} ### Jumping
| Shortcut | Description | | --- | --- | | `]c` | Next difference | | `[c` | Previous difference | {.shortcuts} Miscellaneous {.cols-3} ------------- ### Case {.row-span-2} | Shortcut | Description | |----------------|-------------------------| | `vU` | _Uppercase_ character | | `vu` | _Lowercase_ character | | `~` | _Toggle case_ character | | `viw` `U` | _Uppercase_ word | | `viw` `u` | _Lowercase_ word | | `viw` `~` | _Toggle case_ word | | `VU` _/_ `gUU` | _Uppercase_ line | | `Vu` _/_ `guu` | _Lowercase_ line | | `V~` _/_ `g~~` | _Toggle case_ line | | `gggUG` | _Uppercase_ all text | | `ggguG` | _Lowercase_ all text | | `ggg~G` | _Toggle case_ all text | {.shortcuts} ### Jumping | Shortcut | Description | |----------|----------------------| | `` | Go back to previous | | `` | Go forward | | `gf` | Go to file in cursor | | `ga` | Display hex, ascii value | {.shortcuts} ### Misc command-lines {.row-span-2} | - | - | |------------------------|-------------------------------| | `:h` | Help open help view | | `:edit!` | Reload current file | | `:2,8m0` | Move lines `2`-`8` to `0` | | `:noh` | Clear search highlights | | `:sort` | Sort lines | | `:ter` | Open a terminal window | | `:set paste` | Enable Insert Paste sub-mode | | `:set nopaste` | disable Insert Paste sub-mode | | `:cq` | Exiting with an error
_(aborting Git)_ | ### Navigating | Shortcut | Description | |--------------------------|---------------------------| | `%` | Nearest/matching `{[()]}` | | `[(` _\|_ `[{` _\|_ `[<` | Previous `(`, `{` or `<` | | `])` _\|_ `]{` _\|_ `]>` | Next `)`, `}` or `>` | | `[m` | Previous method start | | `[M` | Previous method end | {.shortcuts} ### Counters | Shortcut | Description | | --- | --- | | `` | Increase number | | `` | Decrease number | {.shortcuts} ### Tags {.row-span-2 .col-span-2} | Shortcut | Description | | --- | --- | | `:tag Classname` | Jump to first definition of Classname | | `` | Jump to definition | | `g]` | See all definitions | | `` | Go back to last tag | | ` ` | Back/forward | | `:tselect Classname` | Find definitions of Classname | | `:tjump Classname` | Find definitions of Classname (auto-select 1st) | {.shortcuts} ### Formatting | - | - | |-----------------|---| | `:ce 8` | Center lines between `8` columns | | `:ri 4` | Right-align lines at `4` columns | | `:le` | Left-align lines | See `:help formatting` ### Marks {.col-span-2 .row-span-4} | Shortcut | Description | | --- | --- | | \`^ | Last position of cursor in insert mode | | \`. | Last change in current buffer | | \`" | Last exited current buffer | | \`0 | In last file edited | | '' | Back to line in current buffer where jumped from | | \`\` | Back to position in current buffer where jumped from | | \`[ | To beginning of previously changed or yanked text | | \`] | To end of previously changed or yanked text | | \`< | To beginning of last visual selection | | \`> | To end of last visual selection | | `ma` | Mark this cursor position as `a` | | \`a | Jump to the cursor position `a` | | `'a` | Jump to the beginning of the line with position `a` | | d'a | Delete from current line to line of mark `a` | | d\`a | Delete from current position to position of mark `a` | | c'a | Change text from current line to line of `a` | | y\`a | Yank text from current position to position of `a` | | `:marks` | List all current marks | | `:delm a` | Delete mark `a` | | `:delm a-d` | Delete marks `a`, `b`, `c`, `d` | | `:delm abc` | Delete marks `a`, `b`, `c` | {.shortcuts} ### Calculator | Shortcut | Description | | --- | --- | | `` `=` 7*7 | Shows the result | | `` `=` 10/2 | Shows the result | {.shortcuts} Do this in INSERT mode ### Shell | - | - | |------------------------|-------------------------------| | `:!` | Interpret Shell Command | | `:r!` | Read in output of shell | | `:r!date` | Insert date | | `:!!date` | Replace current line with date | ### Command line | Shortcut | Description | | --- | --- | | `` | Insert current word into the command line | | `"` | Paste from " register | | `` | Auto-completion of path in insert mode | {.shortcuts} ### Tricks Remove duplicate lines ```shell script :sort | %!uniq -u ``` To number the lines in the file ```shell script :%!cat -n ``` Copy whole doc to clipboard ```shell script :%w !pbcopy # Mac OS X :%w !xclip -i -sel c # GNU/Linux :%w !xsel -i -b # GNU/Linux ``` Also see -------- - [Devhints](https://devhints.io/vim) _(devhints.io)_ - [Vim cheatsheet](https://vim.rtorr.com/) _(vim.rotrr.com)_ - [Vim documentation](http://vimdoc.sourceforge.net/htmldoc/) _(vimdoc.sourceforge.net)_ - [Interactive Vim tutorial](http://openvim.com/) _(openvim.com)_