Sea of Tranquility    About    Archive    Feed

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
    1. Ctrl+e - Move the screen up. Cursor doesn’t move
    2. Ctrl+y - Move the screen down. Cursor doesn’t move
    3. Ctrl+u - Move cursor and screen up 1/2 page
    4. Ctrl+d - Move cursor and screen down 1/2 page
    5. zt - Move the current line to the top of the screen
    6. zb - Move the current line to the bottom of the screen
  • Open recent files
    1. Hit Ctrl-o-o. Keep hitting it until you get the file you want
    2. :browse oldfiles Esc File number Enter
    3. :browse oldfiles gives a list of last 100 files you have opened in vim
  • Code Indentation
    1. V}> - V} selects a block (paragraph) and > applies indentation
  • Reformat text
    1. Select the text
    2. Press gq
  • Disable search highlighting temporarily
    1. :nohlsearch or simply :noh
    2. Press n/N to continue previous search
  • Comment out multiple lines
    1. For entire file: :%s/^/#/g
    2. For a particular range, say from line 10 to 20: :10,20s/^/#
    3. To uncomment: :%s/^#//g
  • Deleting lines
    1. S - Delete the entire line and switch to insert mode
    2. C - Delete the rest of the line and switch to insert mode
    3. dd - Delete the entire line, and stay in normal mode
  • Deleting words
    1. dw - Delete rest of the word including the space after that. Stay in normal mode after the operation
    2. cw - Delete rest of the word excluding the space after that. Switch to normal mode after the operation
  • Switch case
    1. ~ - Change the case of current character
    2. gUw - Change the rest of the word to uppercase
    3. guw - Change the rest of the word to lowercase
    4. gUU - Change the current line to uppercase
    5. guu - Change the current line to lowercase
  • Redo
    1. Ctrl R to redo the last action
    2. 4 Ctrl R to redo the last 4 actions (I haven’t tried this)