Skip to content

Instantly share code, notes, and snippets.

@x1unix
Last active February 24, 2021 12:37
Show Gist options
  • Save x1unix/3ee28a5019e3f94b4653052718c56461 to your computer and use it in GitHub Desktop.
Save x1unix/3ee28a5019e3f94b4653052718c56461 to your computer and use it in GitHub Desktop.
VIM Cheatsheet

The VIM Cheatsheet

Links

Most of commands accept number before (eg: 3k moves down 3 times)

keys

  • [leader] is default to \ (use mapleader or see :help leader)

plugins

nerdtree

s will open the file currently under the cursor in a new vertically split window. Use t to open in a new tab.

modes

  • i - insert mode
  • v - visual, used for text selection
  • Ctrl+v - Visual block mode (used to select text blocks)

normal (default) mode

  • I - enter insert mode at the beginning of the line
  • A - insert at the end of the line
  • cw - Change word
  • cgn - Change the next search match (useful to change searched text)
  • u - ↩️ Undo
  • Ctrl+r Redo
  • . - 🔁 Repeat last command
  • Ndd - ❌ Delete N lines
  • x - ❌ Delete char
  • o - ↵ Add a new line
  • A - Insert at the line end

views

  • :open X - Open file
  • :ter[minal] - Open terminal
  • ctrl+w v - Split screen horizontally
  • ctrl+w s - Split vertically
  • ctrl+w l/h/j/k - Switch to right/left/up/down side
  • ctrl+w L/H/J/K - Switch to panel to right/left/up/down
  • ctrl+w </> - Resize panel width
  • ctrl+w +/- - Resize panel height
  • :tabl/r - Switch between tabs

visual mode commands

  • v - Go to visual mode
  • y - Copy text
  • d - delete selection
  • x - cut selection / delete character
  • p, P - paste selection before/after custor (or i in Command mode)
  • dw - delete word
  • V - select line
  • cw - change word
  • C - change whole line
  • D - Delete rest of line from cursor

navigate

  • j, k - ⬆️ ⬇️
  • h, l - ⬅️ ➡️
  • 0, $ - Go to Start/End of the line
  • w, W (or e, E, B) - Move by word
  • b - Back to word
  • gg - Go to end
  • G - Go to end
  • G; - Go to previous position

search and replace

  • /text - 🔍 search for text
  • ?text - Search top
  • n, N - Go to next/previous search result
  • :%s/str/rep - Replace str with rep
  • 10G - Go to line 10

bookmarks

  • mx - Create a mark with name x (mark this position)
  • 'x - Jump to mark x

view & layout

  • :split FILE_NAME - Split vertically and open file

macros

  • q1 - Start macros with number 1 (basically record and replay commands)
  • q - Stop macro record
  • @1 - Call 1 macro
  • 10@1 Run 1 macro 10 times

a few ways to get out of Vim

  • ZZ, :q!, :!kill -9 $PPID

special commands

Lines

  • :10,33> - Add intentation to lines between 10-33
  • :/Foo/+1m-2 - Move line down near Foo line
  • :/Foo/m$ - Move Foo line to the end of document

Misc

  • :ab x TEXT - Create abbreviation x.
    • Type x in insert mode to insert specified abbreviation.
    • Enter ctrl+v to prevent expansion.
  • :! command - Run shell command
    • :w !sudo tee % - If you have write permission error

Visual block commands

  • :'<,'> - Run command for all lines (from first to last)
  • norm - enter normal mode
  • :'<,'> norm I" - Insert doublequote at the beginning of all selected lines
    • :'<,'> norm A" - the same, but at the EOL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment