, updated
Everyday Vim
The Vim commands I actually use every day, kept as a cheatsheet. Editing, moving around, searching, tabs and windows, and a few tips.
Vim, short for Vi Improved, has been around for decades and it’s hard to learn at first. It’s worth the trouble because you do everything from the keyboard. Your hands never leave it, and once you know the commands you stop thinking about them. This note is the part of Vim I actually use, not everything it can do.
Exiting
esc/control + c- Leave insert mode:w- Save:q- Close the file:wq/:x- Save and close
Editing
i/a- Insert before / after the cursorI/A- Insert at the start / end of the lineo/O- Open a new line below / aboveea- Insert at the end of the wordx- Delete a characterr{character}- Replace the character under the cursor with characterJ- Pull the line below up onto this onep- Pasteu/control + r- Undo / redo
Operators
d- Deletey- Yank (copy)c- Change: delete it, then start typing</>- Indent left / rightgu/gU- Lowercase / uppercase
An operator needs a movement to act on. Put the two together and you get a command: ci) changes what’s inside the parentheses, ci} what’s inside the curly braces, d2w deletes the next two words.
Moving around
hjkl- Left, down, up, rightw/b- Start of the next / previous worde/ge- End of the next / previous word0/$- Start / end of the line^/0w- First character on the line that isn’t a spacegg/G- First / last line of the file:{number}- Go to line number{/}- Previous / next paragraph%- Jump to the matching{},[]or()H/M/L- Top / middle / bottom of the windowgD- Go to where the variable under the cursor is declared
Type a number first and the move repeats:
5jgoes down five lines,3wforward three words.
Scrolling
zt/zz/zb- Put the current line at the top / middle / bottom of the windowcontrol + e/control + y- Scroll down / up one line without moving the cursorcontrol + d/control + u- Scroll down / up half a pagecontrol + f/control + b- Scroll down / up one page
Searching
f{character}/F{character}- Jump forward / backward to character;/,- Do that jump again, forwards / backwards*/#- Next / previous place the word under the cursor shows up/{pattern}/?{pattern}- Search forward / backward for pattern/{pattern}\c- Search forward, ignoring casen/N- Search again, same direction / the other way/thenenter- Run the last search again/thencontrol + p/control + n- Go back / forward through past searches
Replacing
:%s/{old}/{new}/g- Replace every old with new:%s/{old}/{new}/gc- The same, but it asks before each one
Visual mode
v- Start visual modeV- Start visual line modecontrol + v- Start visual block mode
Once you’ve selected something, a movement or an operator works on it. v then 2j marks two lines so you can p over them, and v then ip marks the paragraph you’re in.
Tabs
:tabnew {filename}- Open a new tab, the filename is optional:e {filename}- Open a file in the current tabgt/gT- Next / previous tab, or:tabn/:tabp{number}gt- Go to tab number:tabo- Close every tab but this one:qa- Quit all tabs
Windows
:sp {filename}/:vsp {filename}- Split across / down, the filename is optionalcontrol + wthens/v- The same splits, without typing a commandcontrol + wthenw- Switch between windowscontrol + wthenq- Close this windowcontrol + wthenL- Move this window over to the far right
Tips
"_dddeletes a line without touching what you copied:_is the black hole registercgnchanges the next search match, then.does the same to the one after it"+y/"+p- Copy to / paste from the system clipboard:ju- Show the jump list;control + ogoes back through it andcontrol + iforward:noh- Turn off the search highlighting:set nu- Show line numbers:ter- Open a terminal
Further Reading
- vim.org
- vim.rtorr.com
- devhints.io/vim
- vimsheet.com
- Replace a word with yanked text on the Vim wiki