blog.minskio.co.uk

Content and theme behind minskio.co.uk
Log | Files | Refs

personal-vim-cheatsheet.md (5381B)


      1 ---
      2 title: "Personal VIM cheatsheet"
      3 date: 2020-06-14T12:58:00
      4 lastmod: 2025-07-30T12:52:00
      5 tags : [ "Guides", "Learning", "Linux", "Servers", "Snippets", "Software", ]
      6 ---
      7 
      8 When editing files on Linux, I've always used `nano`, it was always installed and `vi` just seemed incredibly awkward to use, with all the memes about never being able to exit, and weird things happening being right up my street. I'd tried `vimtutor` but was left in pretty much the same place as I started.
      9 
     10 So, when the venerable [Luke Smith](https://lukesmith.xyz/) posted an [hour long walkthrough](https://www.youtube.com/watch?v=d8XtNXutVto) of his methods for completing vimtutor, I was hooked.
     11 
     12 I've now moved all my Linux machines over to neovim and haven't looked back. Without further ado I present the notes cheatsheet that I made while watching the video, for future reference:
     13 
     14 ```
     15 ZZ quit with saving
     16 ZQ quit, without saving
     17 zt makes current line the topmost
     18 zz centre window around current line
     19 
     20 A enters insert mode at the end of the line
     21 I enters insert mode at the start of the line
     22 a enters insert mode after current character
     23 i enters insert mode before current character
     24 o creates newline below current and enters insert mode
     25 O creates newline above current and enters insert mode
     26 
     27 Ctrl + r redo changes
     28 Ctrl + g shows location status bar
     29 gg go to the top line
     30 G go to last line
     31 gj move one line down in a text block
     32 gk move one line up in a text block
     33 g$ move to the end of a text blocks' current line
     34 g0 move to the start of a text blocks' current line
     35 25% move to 25% of the way through the file
     36 '' move back to previous location before % movement
     37 u undoes changes
     38 gu uncapitalise
     39 gU capitalise
     40 ~ switch case of current letter
     41 gv jump back to previously selected text
     42 
     43 J join current and next line
     44 gJ join current and next line, without spaces
     45 
     46 v start visual selection/highlighting
     47 Ctrl + v start visual selection as a block
     48 v start visual selection/highlighting, using complete lines
     49 
     50 gf open written filename in text, in vim
     51 
     52 y yank/copy
     53 yy yank whole line
     54 
     55 . redo last command run
     56 
     57 w move forwards word by word
     58 b move backwards word by word
     59 f find next character on line
     60 F find previous character on line
     61 t find next character on line, but the space before
     62 T find previous character on line, but the space before
     63 
     64 / search for text going forwards
     65 / enter n next search result
     66 / enter N previous search result
     67 ? search for text going backwards
     68 :set ic toggles case insensitivty
     69 :set hlsearch highlights search results
     70 :nohlsearch disables highlighted search results
     71 
     72 r replace current letter with next letter you type
     73 R replace mode, more than one character, but exact length matching
     74 
     75 { move cursor up 4 lines at a time
     76 } move cursor down 4 lines at a time
     77 
     78 v enters visual mode
     79 V enters visual mode, whole lines
     80 
     81 0 move to the start of the line
     82 $ move to the end of the line
     83 ^ move to the start of the line
     84 % jump to matching parenthesis
     85 W jump one word right
     86 
     87 :s/old/new/ replace old with new once on a line
     88 :s/old/new/g replace old with new every time on a line
     89 :%s/old/new/g replace old with new every time in the whole file
     90 :%s/old/new/gc replace old with new every time in the whole file, but prompting beforehand
     91 
     92 :! run shell command
     93 :r filename read text from filename
     94 :r ! command read output from running command
     95 
     96 :norm run command on highlighted lines
     97 
     98 :setlocal spell! spelllang=en_gb start spellcheck
     99 :setlocal spell! stop spellcheck
    100 z= correct misspelled word when highlighted
    101 ]s jumps to next misspelled word
    102 
    103 p put/paste previously deleted lines
    104 P put/paste at current cursor, instead of after
    105 5p paste, 5 times
    106 
    107 x delete character under cursor
    108 dw delete word (at the start of the word)
    109 daw delete whole word including whitespace
    110 diw delete whole word, excluding whitespace
    111 di( delete everything inside parentheis
    112 da( delete everything inside, and including parenthesis
    113 dw delete word (current word, anywhere)
    114 d$ delete the remainder of the line
    115 D delete the remainder of the line
    116 db delete one word backwards
    117 d5w deletes 5 words forwards
    118 dd deletes the whole line
    119 3dd deletes the next 3 lines
    120 
    121 c change mode, same as d, but goes to insert mode after
    122 cw delete word, then enter insert mode
    123 o insert new line and enter insert mode
    124 O insert new line above current and enter insert mode
    125 
    126 0 move to start of line
    127 2w move 2 words to the left
    128 
    129 :earlier 5m undo 5 minutes worth of changes
    130 :later 5m redo 5 minutes worth of changes after undoing
    131 ```
    132 
    133 One nice bonus that I picked up on from Lukes dotfiles was fixing line endings to the Linux format on every save which is just great.
    134 
    135 Add the below to your `init.vim` file (if using neovim):
    136 
    137 ```
    138 " Automatically deletes all trailing whitespace and newlines at end of file on save.
    139 	autocmd BufWritePre * %s/\s\+$//e
    140 	autocmd BufWritepre * %s/\n\+\%$//e
    141 ```
    142 
    143 Another point worth adding is sometimes files formatted outside vim will contain '^@' or '^M' symbols scattered between letters. These represent null or line break characters and can be removed by running:
    144 ```
    145 %s/[\x0]//g
    146 %s/\r//g
    147 ```
    148 
    149 * **Edit 2020-12-02:** Added notes regarding reading file contents and command output.
    150 * **Edit 2021-01-02:** Added null character removal note.
    151 * **Edit 2021-02-11:** Added additional line break removal note.
    152 * **Edit 2023-03-10:** Add P/paste at position line
    153 * **Edit 2025-07-30:** Add finding caracters/newline-inserts