Skip to content

Instantly share code, notes, and snippets.

@davidlaprade
Last active March 9, 2024 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidlaprade/ec6b0e26a6525f89293a to your computer and use it in GitHub Desktop.
Save davidlaprade/ec6b0e26a6525f89293a to your computer and use it in GitHub Desktop.
Vim Commands

General

  • . repeat last command
  • D delete from cursor position to end of line
  • C change to end of line (i.e. delete and open cursor)
  • d0 delete from cursor to begining of line
  • y0 yank/copy from cursor to end of line
  • y$ yank/copy from cursor to begining of line
  • p paste
  • P paste before cursor
  • A open insert at end of line
  • I open insert at begining of line
  • o add line below and open insert
  • O add line above and open insert
  • gq<motion> break the lines that moves over into 80 character lines, e.g.:
    • gqq break the current line into 80 char lines
    • gqG break all lines from current cursor position to end of file
  • <n>G go to line n
  • /<string> search down for string/regex
  • ?<string> search up for string/regex
    • n while in search mode, go to next match
    • N while in search mode, go to previous match
  • + hop to first non-whitespace character on the next line
  • - hop to fir non-whitespace character on the previous line
  • J delete all space between end of current line and start of non-whitespace after it
  • ^ jump to first non-space on line
  • $ jump to end of line
  • 0 jump to start of line
  • f<c> jumps to next character, where <c> is any character
  • <n>f<c> jumpts to nth character, where <c> is any character
  • ,<c> jump to previous character, where <c> is any character
  • r<c> replace currently selected character with <c>, where <c> is any character
  • x delete currently selected character
  • CTRL + C to exit insert mode
  • jk to exit insert mode (presupposes my dotfiles, specifically this)
  • ]] jump to next class/module definition
  • [[ jump to previous class/module definition
  • ]m jump to the start of the next method definition
  • [m jump to the start of the previous method definition
  • ]M jump to the end of the next method defintion
  • [M jump to the end of the previous method defintion
  • % jump between the start and end of a block of code, works for def . . . end, (. . .), [ . . . ], etc (requires matchit.vim to be enabled)
  • L go to bottom of visible page
  • H go to top of visible page
  • M go to middle of visible page
  • z. put the cursor in the middle of the page
  • CTRL + U scroll up one half page
  • CTRL + D scroll down one half page
  • <space> + ct index ctags for current directory, presupposes thoughtbot dotfiles
  • gcc comment/uncomment current line, presupposes tComment
  • } jump to end of paragraph
  • { jump to begining of paragraph
  • cst<tag><new-tag> changes the surrounding HTML tag specified to the new tag specified, presupposes vim-surround
  • dst<tag> deletes the surrounding HTML tag, presupposes vim-surround
  • cit change inside tag, can be used for any grouping symbol, e.g. [, {, (, ", etc, presupposes vim-surround
  • cat change around tag, presupposes vim-surround
  • <space>rs run spec that the cursor is on, presupposes vim-rspec and my dotfiles
  • <space>rl run last spec that was run, presupposes vim-rspec and my dotfiles
  • <space>rt run spec file from top, presupposes vim-rspec and my dotfiles

Insert Mode

  • <CTRL + w> delete previous word
  • <CTRL + h> delete previous letter The following presuppose my dotfiles:
  • <CTRL + u> undo inserted text
  • <CTRL + r> redo inserted text
  • <CTRL + k> delete to end of line
  • <CTRL + d> delete to begining of line
  • <CTRL + a> jump to start of line
  • <CTRL + e> jump to end of line

Vim Command Line

  • :!<shell script> executes shell script from within vim
  • :Ag <string> uses silver searcher to search through subdirectories for string
  • <CTRL + R><"> pastes yanked text into command line
  • :<some command> a apply command to all open windows, e.g. :qa! to force quit all windows
  • % path to current file
  • :jumps list all of the jumps you've made between files
  • :%s/<string>/<string2>/gc global replace of string 1 for string 2 with confirmation
  • : <CTRL + p> go back through command history
  • : <CTRL + n> go forward through command history
  • :Explore open directory navigator within path of current file
  • :saveas <path> saves a copy of the current file in the path specified
  • :help <command> gives documentation for the

Text Objects

NOTE: Most of these work for v (select), d (delete), = (auto-indent), and c (change) commands

  • vaw select around word (with spaces around it)
  • viw select inner word (no spaces around it)
  • vim select inside method (requires vim-ruby)
  • vam select around method (requires vim-ruby)
  • V select whole line
  • v0 select from cursor position to start of line
  • v$ select from cursor position to end of line
  • <select something> backspace deletes (presupposes thoughtbot dotfiles)
  • vt<character> select to character
  • vap select a paragraph, i.e. all of the code until a newline
  • vas select a sentence
  • vit select inside tags, e.g. <a>, <p>
  • vat select around tags, e.g. <a>, <p>
  • vi<char> select inside character, where is a character used for grouping, e.g. (, [, ", ', etc
  • v/<search string> select until search string
  • CTRL + v select column (only works for select)
  • U make selected text uppercase (only works for select)
  • u make selected text lowercase (only works for select)
  • <CTRL-v>jjjIhey_guys insert 'hey_guys' at the cusor position three lines down, SO post

Navigation in Folders

  • space space go to last open file (presupposes thoughtbot dotfiles)
  • CTRL + ] go to method definition (presupposes you have C-tabs installed and set up)
  • g] show list of ctags matches
  • CTRL + o go back to previous file; even open files from the last vim session
  • CTRL + i go to next file in stack
  • <n> + <CTRL + i/o> jump n places in stack of files forward/backward
  • CTRL + p find file in directory using fuzzy matching on file path
    • CTRL + j/k to navigate among results
    • CTRL + d to switch between matching on file name and file path name

Indenting

  • >> indent to the right
  • == indent line to the appropriate amount
  • <n>== indent n lines the appropriate amount

Windows

  • CTRL + w + s horizontal split
  • CTRL + w + q close window
  • CTRL + w + v vertical split
  • CTRL + <h,j,k,l> move between windows in direction
  • <CTRL + w> > resize vertical split to move it over to the right, can be prefixed with a number to do it multiple times
  • <CTRL + w> < resize vertical split to move it over to the left
  • <CTRL + w> +/- resize horizontal split
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment