Useful Vim Tricks

  • Autoformating Code
    • gg=G: formats all code
      Explanation: go to top of the file (gg), command to fix the indentation (=), perform operation to the end of the file (G).
  • Moving Around
    • 0: move to the first of physical line
    • $: move to the end of physical line
    • ^: move to first non-whitespace character in line
    • g_: move to last non-whitespace character in line
    • g0: move to start of the screen line
    • g$: move to end of the screen line (if current line wraps around visible screen)
  • Spell Checks
    • :set spell: check spelling
    • :set spell spelllang=en_us: specify language
    • [s, ]s: navigate between misspelled words
    • z=: bring up autocorrect suggestions under cursor
  • Word Completion: CTRL-N, CTRL-P
    • Need option to be on by stating set complete+=kspell
    • Available in insert mode.
  • Natural Split Opening
    • set splitbelow: open new windows below current window
    • set splitright: open new windows right to current window
  • Resizing Split Windows
    • [CTRL+w] _: max out height of current split
    • [CTRL+w] |: max out width of current split
    • [CTRL+w] +: increase height of current split
    • [CTRL+w] -: decrease height of current split
  • Tab Settings
    • set tabstop=n: size of a hard tabstop
    • set shiftwidth=n: size of an indent
    • set softtabstop=n: combination of spaces and tabs used to simulate tab stops at a width other than the (hard)tabstop (i.e. number of columns for a TAB)
    • set expandtab: expand TABs to spaces
    • set smarttab: make TAB insert indents instead of TABs at the beginning of a line
  • Auto Indentation
    • :set paste: prevent re-tabbing the pasted code
  • Setting Status Lines
  • Closing all windows
    • :qa: quit all
    • :wqa: save and quit all
  • Pasting from Registers
    • "+p: paste from system clipboard
      Explanation: use the upcoming register ("), the system clipboard register (+), paste (p)
    • Check the available registers with :reg
  • Pasting While in Insert Mode
    • [CTRL+R] {register}
      • ex) [CTRL-R] +: insert contents of clipboard
      • ex) [CTRL-R] ": insert contents of last delete or yank
  • Useful Resources
Open Comments