, updated
Everyday Vim
The Vim commands I actually use every day, kept as a cheatsheet. Editing, navigating, searching, windows, and a few tips.
Vim, short for Vi Improved, is a highly configurable text editor that has been around for decades. It has a steep learning curve, but once it clicks, it makes everyday editing noticeably faster. This note is how I use Vim day to day, and the commands I reach for most.
Why Vim?
Vim is popular among developers and power users for a few reasons:
Speed- Complex edits happen from the keyboard alone. Once you’re fluent, you move through a file at an impressive pace.Efficiency- Your hands stay on the keyboard, which saves time and spares your wrists.Cool- 😎
Cheatsheet
- Exiting
- Editing
- Operators
- Navigating: arrow, word, line, document, paragraph, window, other
- Scrolling
- Searching: character, current word, pattern, replace
- Marking text (visual mode)
- Tabs
- Windows
- Tips
Exiting
:q- Close the file:w- Save (write) the file:wq/:x- Save and close the fileesc/control + c- Leave insert mode
Editing
i- Insert before the cursora- Insert after the cursorI- Insert at the beginning of the lineA- Insert at the end of the lineo- Open a new line below the current lineO- Open a new line above the current lineea- Insert at the end of the wordx- Delete a character (cut)r{character}- Replace the character under the cursor with characterp- Pasteu- Undocontrol + r- RedoJ- Join the line below with the current line
Operators
d- Deletey- Yank (copy)c- Change (delete, then insert)>- Indent right<- Indent leftgU- Change to uppercasegu- Change to lowercase
Operators combine with movement keys. For instance:
ci)- Change the text inside the parentheses (…)ci}- Change the text inside the curly braces {…}- and so on
Navigating
Arrow
h- Move the cursor leftj- Move the cursor downk- Move the cursor upl- Move the cursor right
To move several steps at once, put a count in front:
5jmoves down five lines instead of pressingjfive times.
Word
w- Jump to the start of the next wordb- Jump to the start of the previous worde- Jump to the end of the next wordge- Jump to the end of the previous word
Line
0- Jump to the start of the line^/0w- Jump to the first non-blank character of the line$- Jump to the end of the line
Document
gg- Go to the first lineG- Go to the last line:{number}- Go to line number
Paragraph
%- Jump between matching {}, [], ()}- Go to the next paragraph{- Go to the previous paragraph
Window
H- Jump to the top of the windowM- Jump to the middle of the windowL- Jump to the bottom of the window
Other
gD- Go to the global declaration of the variable under the cursor
Scrolling
zt- Put the current line at the top of the windowzz- Put the current line in the middle of the windowzb- Put the current line at the bottom of the windowcontrol + e- Scroll down one line (the cursor stays)control + y- Scroll up one line (the cursor stays)control + f- Scroll down one pagecontrol + b- Scroll up one pagecontrol + d- Scroll down half a pagecontrol + u- Scroll up half a page
Searching
Character
f{character}- Jump forward to characterF{character}- Jump backward to character;- Repeat the search forwards,- Repeat the search backwards
Current word
*- Jump to the next occurrence of the word under the cursor#- Jump to the previous occurrence of the word under the cursor
Pattern
/{pattern}- Search forward for pattern?{pattern}- Search backward for pattern/{pattern}\c- Search forward, ignoring casen- Repeat the search in the same directionN- Repeat the search in the opposite direction/thenenter- Repeat the last search/thencontrol + p- Step back through search history/thencontrol + n- Step forward through search history
Replace
:%s/{old}/{new}/g- Replace every old with new:%s/{old}/{new}/gc- The same, asking before each replacement
Marking text (visual mode)
v- Start visual modeV- Start visual line modecontrol + v- Start visual block mode
Once in visual mode, combine it with movement or operator keys. For instance:
vthen2j- Mark two lines, thenpto paste over themvthenip- Mark the paragraph under the cursor
Tabs
:tabnew {filename}- Open a new tab (the filename is optional):e {filename}- Edit a file in the current tabgt/:tabn- Go to the next tabgT/:tabp- Go to the previous tab{number}gt- Go to tab number:tabo- Close every tab except the current one:qa- Quit all tabs
Windows
:sp {filename}- Split the window horizontally (the filename is optional):vsp {filename}- Split the window verticallycontrol + wthens- Split horizontallycontrol + wthenv- Split verticallycontrol + wthenw- Switch between windowscontrol + wthenq- Close the current windowcontrol + wthenL- Move the current window to the far right
Tips
- Delete a line without overwriting the yank register:
"_ddsends the line to the black-hole register_ - Change the next search match, then repeat it:
cgn, then.for each following match :ter- Open a terminal:set nu- Show line numbers"+y- Copy the selection to the system clipboard"+p- Paste from the system clipboard:ju- Show the jump list;control + ogoes back through it andcontrol + igoes forward:noh- Clear search highlighting
Conclusion
Vim is a powerful tool for everyday work. Bringing it into my daily routine has made me noticeably faster. Give it a try, and you may wonder how you got along without it. Happy Vimming! ⌨️
Kudos to
- vim.org
- vim.rtorr.com
- devhints.io/vim
- vimsheet.com
- Replace a word with yanked text on the Vim wiki