Note that this was written after I spent a few months learning Vim. If you’re just stuck in Vim and want to get out, see What to do when you get stuck in a Vim editor.
This will be another notey one. Really I just want to stick this somewhere I can easily access it. I’m going to publish my notes on the things that are useful in Vim but that I keep forgetting. My notes are split into four sections, so I’ll publish four posts:
- Miscellaneous Vim Stuff
- Vertical Columns in Vim (“visual blocks”)
- Navigating files, lines, blocks
- Searching in Vim
Before I get going though, a mildly funny anecdote: A colleague messaged me recently and mentioned that his vim had been upset by some house renovation work, and I wondered how said renovation could have such an impact on his command-line text editor…. before I realised that he meant vim as in “vim and vigour”.
OK, so here is some miscellaneous Vim stuff:
- Vim cheat sheet – http://hamwaves.com/vim.tutorial/images/vim.en.png
- Great online “Vim Adventures” game you can use to learn Vim: https://vim-adventures.com/
- If you make changes to ~/.vimrc and want to reload:
- Type :so $MYVIMRC
- …but actually you can just type $MY and then tab to autocomplete.
- Searching:
- See Searching in Vim
- Navigating files, lines, blocks
- Copy / paste:
- Copy current line (“yank”): yy – which is the same as Y
- Paste current line below the line you are on (“put”): p
- To replace one line with another: Y to yank a line, then go to the line you want to replace and type Vp
- V puts the whole line into visual mode, and then p pastes the register into the visual selection (the whole line).
- Append, Substitute and Change
- Append is a to append after current character or A to append at end of line
- (puts you into insert mode)
- Substitute is s to substitute current character and S to substitute current line
- (puts you into insert mode)
- Change is c to replace whatever you specify – eg aw for a word, iw for inner word (word without leading space)
- (puts you into insert mode)
- C is to replace from cursor to end of line
- See also separate section below on navigating blocks
- Append is a to append after current character or A to append at end of line
- Replace current word with contents of register: viwp
- v is visual mode
- iw is inner word
- p is put
- You might want to explicitly use the “0 register (like this: viw”0p), otherwise what’s in the default register might get replaced and if you try to repeat the action you get unexpected results
- But for this to work, you will have to have used “0y (or whatever) first, to get your text into the correct register
- Select a vertical column of text (like alt click)
- Text objects:
- aw is a word
- iw is an inner word
- daw means delete a word
- More here: https://blog.carbonfive.com/2011/10/17/vim-text-objects-the-definitive-guide/
- In Vim adventures, try :help aw
- To see line numbers: :set number
- To make that change (or any other change) permanent:
- Cmd: vim ~/.vimrc
- Type the line :set number into the file
- It will take effect immediately
- To turn line numbers off temporarily (for copy/pasting): :se nonu (then :se number to turn them back on again)
- To make that change (or any other change) permanent:
- Do one command while in Insert mode, then return to Insert mode: Ctrl + o
- This takes you to normal mode for one command
- Most commands in vim take a function and then an argument
- Eg j is a movement argument – so dj is the delete command with a “down” argument
- Commonly repeat the function if there is no argument – so dd means just delete
- u – undo
- Ctrl + r – Redo
- o – insert new line below (O = above)
- Note this will also put you in insert mode
- Tab (indent) left or right: < and > – eg << to just tab left
- To indent a whole block: Use v to go into visual mode, then up and down keys to select lines, then < and > to indent in or out
- Select an entire function definition
- I used a{ and i{ but I never got quite what I wanted
- I think the answer might lie here but I didn’t have the patience to read through all the suggested solutions: https://stackoverflow.com/questions/11723169/selecting-entire-function-definition-in-vim/11723259
- Set to use spaces instead of tabs
- Cmd: :set expandtab ts=2 sw=2
- ts = tabstop
- Note this means that you can use the tab command and it will automatically insert 2 spaces
- It also defines how he file will be displayed if it contains tab characters
- sw = Shiftwidth
- Something to do with what happens when you press enter, – automatic indentations?
- Multiples
- Add number at start
- Eg 2f_ – find the second instance of underscore on this line
- Delete characters – x for the char in front, X for the char behind (like backspace)
- d – delete line
- dd – delete current line
- 4dd – delete 4 lines
- dG – Delete all lines from current line to end of file
- Shift+d – delete to end of line
- Shift+c – delete to end of line and go into insert mode
- dw – delete a word
- J to join text that’s split across lines to turn it into one long string
- Eg This…
- Hey
- Hello
- You
- And also
- Hello
- Goodbye
- Hey
- … becomes this:
- Hey Hello You And also Goodbye
- Eg This…