Random vim notes
This is just a dump of vim tricks that I have found useful. They are taken from multiple sources.
- Move the screen
Ctrl+e- Move the screen up. Cursor doesn’t moveCtrl+y- Move the screen down. Cursor doesn’t moveCtrl+u- Move cursor and screen up 1/2 pageCtrl+d- Move cursor and screen down 1/2 pagezt- Move the current line to the top of the screenzb- Move the current line to the bottom of the screen
- Open recent files
- Hit
Ctrl-o-o. Keep hitting it until you get the file you want :browse oldfilesEscFile numberEnter:browse oldfilesgives a list of last 100 files you have opened in vim
- Hit
- Code Indentation
V}>-V}selects a block (paragraph) and>applies indentation
- Reformat text
- Select the text
- Press
gq
- Disable search highlighting temporarily
:nohlsearchor simply:noh- Press
n/Nto continue previous search
- Comment out multiple lines
- For entire file:
:%s/^/#/g - For a particular range, say from line 10 to 20:
:10,20s/^/# - To uncomment:
:%s/^#//g
- For entire file:
- Deleting lines
S- Delete the entire line and switch to insert modeC- Delete the rest of the line and switch to insert modedd- Delete the entire line, and stay in normal mode
- Deleting words
dw- Delete rest of the word including the space after that. Stay in normal mode after the operationcw- Delete rest of the word excluding the space after that. Switch to normal mode after the operation
- Switch case
~- Change the case of current charactergUw- Change the rest of the word to uppercaseguw- Change the rest of the word to lowercasegUU- Change the current line to uppercaseguu- Change the current line to lowercase
- Redo
CtrlRto redo the last action4CtrlRto redo the last 4 actions (I haven’t tried this)