Skip to content

Instantly share code, notes, and snippets.

@JGAntunes
Forked from FabioAntunes/vim-cheat-sheet.md
Last active October 22, 2021 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JGAntunes/515c109784f63a25888825614fb9ccca to your computer and use it in GitHub Desktop.
Save JGAntunes/515c109784f63a25888825614fb9ccca to your computer and use it in GitHub Desktop.
Vim cheat sheet

Find (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/yolo/swag/gc find and replace yolo with swag and ask for confirmation. c asks for confirmation

Goto

  • gg beginning of file
  • shift + g end of file
  • $ end of line
  • ^ beggining of the line
  • g, to move forwards through the changelist.
  • g; to move backwards through the changelist.

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) + = set all splits to equal width
  • (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

Spell check

  • :set spell spelllang=<lang> activate spell check
  • z= see suggestions for the selected word
  • 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">
  • s open selected file in new window
  • t open selected file in new tab
  • <leader>gm show popup window with the commit for where the cursor is positioned
  • <leader>gm<leader>gm move the cursor to inside the window
  • With the cursor inside the window:
    • d show the diff for changes in this file in this commit
    • D show the diff for changes in ALL the files in this commit
    • o/O move between older and recent commit changes in this file history
    • q close window
  • Go to code navigation
    • <Leader>gd Go to definition
    • <Leader>gy Go to type definition
    • <Leader>gi Go to implementation
    • <Leader>gr Go to references
  • Navigation:
    • gfJump to definition
    • ctrl + o Jump back to previous file
    • ctrl + i Jump forward

Rename files

Run :Explore or :Sex (shorthand for split explore) then navigate on the files and press R to rename and D to delete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment