vim Notes - robbiehume/CS-Notes GitHub Wiki

Links:

Cheatsheets:

Comment multiple lines:

  • Ctrl+V to enter 'visual block mode, then select first character of lines you want to comment out
  • Shift+i to enter INSERT mode, then type #
  • Then hit ESC

Find and replace (like Ctrl-D in VScode)

  • Find and replace instructions
  • Find and replace details
  • Search for a word, highlight and delete it, then replace it, enter Normal mode and press . to replace the next occurrence
  • Can press n to go to the next highlighted occurrence and skip the current one without replacing it

Find and replace current or multiple lines (Link)

  • Current line: s/<search_term>/<replace_term>/g
  • Current line and n-1 lines after: s/<search_term>/<replace_term>/g n
  • Range of lines: start_line_number, end_line_number s/<search_term>/<replace_term>/g

Copy lines without line numbers (due to mouse=a not allowing copy select):

  • Hold Alt and start selecting from the beginning of line (for Mac, use Ctrl instead)
    • Or Shift+Alt on Windows?
  • Or just turn off line numbers (:set nonu)
    • Add nu and nnu shortcuts in vimrc

To paste with mouse=a

  • Shift-right_click (or Shift-<mouse scroll wheel click>)

Save and run script: :w|!%:p

image

Stop and resume vim: * Stop: Ctrl-Z * Resume: fg

To 'unset' an option, just do :set <option>!

Replace all tabs with spaces: :%retab * Must have: set expandtab ts=4 sw=4 ai

To indent multiple lines: enter visual mode then select the lines the do > * Can do 2> or any other number to do multiple tabs * Can also just do 2>> (no visual mode) to tab 2 lines starting from the current one (current + next)

Keyboard shortcuts

  • I (in normal mode): insert before the first non-blank character of the line
  • S (in normal mode): forward delete one character and enter insert mode
  • * (in normal mode): go to next occurrence of word
  • Ctrl-R-" (in insert mode): paste from vim register
  • ".[p / P] (in normal mode): paste last inserted text
  • x / X (in normal mode): delete one character forward / back and stay in normal mode
  • Ctrl-V: record keystroke to get exact key binding
    • Example: converts "Ctrl-S" to "^S"
  • Move between tabs: link

CLI commands

  • :register: see the current values of all the registers

vimdiff

  • Ctrl-W-W: move between windows
  • Ctrl-W-=: set window sizes equal
  • When :set mouse=a is set, you can use the mouse to move the divider
⚠️ **GitHub.com Fallback** ⚠️