Skip to content

Instantly share code, notes, and snippets.

@FabioAntunes
Last active March 17, 2021 17:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FabioAntunes/cd555f80fb14e4e238ff3450c6956a2b to your computer and use it in GitHub Desktop.
Save FabioAntunes/cd555f80fb14e4e238ff3450c6956a2b to your computer and use it in GitHub Desktop.
Vim cheat sheet

Search (and replace)

  • /[pattern] search forward for pattern
  • ?[pattern] search backward for pattern
  • n repeat search forward
  • N repeat search backward
  • * search for word currently under cursor
  • g* search for word partially under cursor
  • [I show lines with matching word under cursor
  • %s/background/&-color/g find and replace background with background-color. & inserts the matched pattern %s/\(foo\)bar/\1baz/g find and replace foobar with foobaz. \1 uses the captured group foo from the \(\)
  • %s/yolo/swag/gc find and replace yolo with swag and ask for confirmation. c asks for confirmation
  • \zs and \ze in substitute command to ignore pattern before and after respectively .
" foobar -> foobaz
%s/foo\zsbar/baz/

" foobar -> bazbar
%s/foo\zebar/baz/

Search and replace across multiple files with fzf

  • search across the project with fzf, this will open a quick fix window with the results. Select the ones to be replaced with tab. Press enter. Run the following command to replace yolo with yoloswagin the selected files :cfdo %s/yolo/yoloswag/g | update

Save file as sudo, useful when we don't have right permissions

  • :w !sudo tee %

Undo or redo changes

  • :earlier 15m
  • g- and g+ go backward and forward in time.
  • g, to move forwards through the changelist.
  • g; to move backwards through the changelist.

Commands

  • :! [command] Executes an external command while you're in Vim.
  • :.! [command] Executes an external command and dump the results into the buffer eg: :.! date
  • (ctrl + w) + R move windows up/left
  • (ctrl + w) + r move windows down/right
  • (ctrl + w) + L Move the current window to the "far right"
  • (ctrl + w) + H Move the current window to the "far left"
  • (ctrl + w) + J Move the current window to the "very bottom"
  • (ctrl + w) + K Move the current window to the "very top"
  • (ctrl + w) + x Exchange current window with next one. If there is no next window, exchange with previous

Delete

  • diw delete the current word
  • dat delete everything inside html tag, and the tag itself
  • di delete within limiters. Eg: di{, di[,di', di(, di<
  • dd delete line
  • dt delete until. Eg: dt", dt<space>
  • df delete until, and including. Eg: df", df<space>
  • D delete rest of the line to the right of the cursor

Change

  • ciw change current word
  • cat change everything inside html tag, and the tag itself
  • ci"change within limiters. Eg: ci{, ci[,ci', ci(, ci<
  • cc change current line
  • ct change until. Eg: ct", ct<space>
  • cf change until, and including. Eg: cf", cf<space>
  • C change rest of the line to the right of the cursor

Yank (Copy)

  • yiw copy current word
  • yat copy everything inside html tag, and the tag itself
  • yi" yank within limiters. Eg: yi{, yi[,yi', yi(, yi<
  • yy yank current line
  • yt yank until. Eg: yt", yt<space>
  • yf yank until, and including. Eg: yf", yf<space>
  • Y yank rest of the line to the right of the cursor

Edit multiple lines, same column

  • ctrl + v start visual block, press j or k to select the lines, press I (capital i) type text desired, press <ESC>

Generate HTML file with the current file:

  • :%TOhtml

Buffers

  • :ls List opened buffers
  • :bd close current buffer append ! to force closing buffer after unsaved changes were made
  • :bd[Number] to close a buffer by number
  • :bw close and save current buffer
  • :bw[Number] to close and save a buffer by number

Numbers

  • ctrl + x decrease the number under the cursor
  • ctrl + a increase the number under the cursor

Visual Mode

  • gv repeat lst visual selection
  • cs"' change the surroudning pairs ie. double quotes to single quotes
  • cs'<q> change surrounding single quotes to html tag <q>
  • cst" change a surrounding tag to double quote
  • ds" delete surrouding pairs, ie double quotes
  • ysiw] surround only the word under the cursor, using [, {, etc will add spaces between the word and the surrounding
  • yss) surround the entire line with parentheses
  • On visual mode, surround selection S<p class="important">

Diff files

  • windo diffthis uses vimdiff between two opened buffers and compares them
  • windo diffoff stop vimdiff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment