Cool Vim resources
Vim is a text editor that makes navigating and editing code easy and fast. Over the years it garnered a cult-like following and a very lively ecosystem online. A downside of such a big ecosystem is that there is not one central place to find all the information. What I offer here is many beginner and advanced Vim motions in one place.
In the first part you find many straight forward Vim tips. The last two parts are very helpful tips I learned from ThePrimeagen, a Vim enthusiast who is very popular on YouTube and Twitch.
What can you do with the resources below? Copy it all and use it as cheat sheet, pick from it what you find interesting or do whatever else you want to do with it.
This list is not fully comprehensive! This is just a lot of the stuff that I like and use often.
Vim cheat sheet
GENERAL
i - insert text
c - change
d - delete
a - append text
y - yank (copy) text
x - cut text (similar to regular cut shortcut, Ctrl-x)
d - delete text (works similar to cut)
p - paste text
u - undo
w - jump by start of words. Essentially go to next word and land at the beginning.
e - jump to end of words. Essentially go to next word and land at the end.
b - jump backwards by words
$ - jump to end of line (same as $ in regex)
^ - jump to first non-space character of the line (similar to ^ in regex)
0 - Go to zeroth character of the line. (essentially beginning of the line)
>> - Shift text right
<< - Shift text left
:vsplit - vertical Split
:close - command to close current split
z - Control the folds. Use "Z-Fold" to memorize.
zo - open the fold
zc - close the fold
Objects:
w - word
p - paragraph
s - sentence
q - quotes
b - brackets
i - indention level
W - white space
Scopes:
a - around
i - inside
e.g.:
vaq - select around current word
yiW - copy between last and next white space
yiw - yank inner word
ciw - change (cut) inner word
ciq - change inside quotes
cib - change inside brackets
dii - delete everything at current indention
NAVIGATING
4j : move 4 lines down
4k : move 4 lines up
4w : move 4 words forward
4b : move 4 words backward
0 : jump to beginning of the line
_ : jump to first character of the line (or use ^)
$ : jump to the end of the line
f" : move cursor forward to "
% : when on a brace, % jumps to matching closing brace; do % again to jump back to opening brace
Ctrl + f : move down a page
Ctrl + b : move up a page
Ctrl + o : jump previous (older) location
Ctrl + i : jump to next (newer) location
g; : jump to last change
g, : jump to next change
gg : jump to top of document
G : jump to end of file
* : find next occurrence for word under cursor
# : find previous occurence for word under cursor
CTRL + j to move down; "n", "<A-j>", ":m .+1<CR>=="
CTRL + k to move up; "n", "<A-k>", ":m .+1<CR>=="
Search for patterns:
press /
enter pattern
press Enter to search
n : find next occurrence
N : find previous occurrence
:tabnew : open new buffer
:tabp : go to previous tab
:tabn : go to next tab
:write foo.txt : save buffer as foo.txt
:w foo.txt : save buffer as foo.txt
:edit : open file
:e : open file
Windows (like panes in tmux)
Ctrl + w : enter window command mode
Ctrl + w + s : new window, horizontal split
Ctrl + w + v : new window, vertical split
Ctrl + w + n : new empty buffer
Ctrl + w + w : go to next window
Ctrl + w + p : go to previous window
Ctrl + w + c : close current window
:only : closes all windows besides the one you're on
EDITING
y : copy
P : paste (uppercase)
d : delete
D : delete from cursor until end of line
c : cuts line
C or c$ : like D but also goes into insert mode
cc - change (replace) entire line
cw : change until end of current word
caw : change around the word (incl. whitespace)
ciw : change inside the word (excl. whitespace)
ciW : change the whole "thing" you're in
c2w : change next two words
5cl : change next 5 characters
2ct<SPACE> : change up to the 2nd space character
2cf<SPACE> : same thing but grabs the 2nd space character too
dw : delete word
daw : delete around word
diw : delete inside word
diW : delete whole "thing" around word
dis : delete sentence
d6w : delete 6 words
dt> : delete until >
di] : delete everything inside [ ]
ci} : change everything inside { ]
dd : delete whole line
4dd : delete 4 lines
dip : delete everything in paragraph
dap : delete around paragraph
s : delete one character or use cl or xi if s is used by Leap plugin in Neovim
S : delete line and go into insert mode
cc : delete line and go into insert mode
Ctrl + p : predictive completion (word)
dw : delete a word
d6w : delete 6 words
dt> : delete until >
di] : delete everything inside [ ]
ci} : change everything inside { ]
dd : delete whole line
4dd : delete 4 lines
dip : delete everything in paragraph
dap : delete around paragraph
s : delete one character or use cl or xi if s is used by Leap plugin in Neovim
cw : change until end of current word
caw : change around the word (incl. whitespace)
ciw : change inside the word (excl. whitespace)
ciW : change the whole "thing" you're in
c2w : change next two words
vaw : visually select word
guiw : make current word lower case
~ : changes the case of current character
guu : change current line from upper to lower.
gUU : change current LINE from lower to upper.
guw : change to end of current WORD from upper to lower.
guaw : change all of current WORD to lower.
gUw : change to end of current WORD from lower to upper.
gUaw : change all of current WORD to upper.
g~~ : invert case to entire line
g~w : invert case to current WORD
guG : change to lowercase until the end of document.
gU) : change until end of sentence to upper case
gu} : change to end of paragraph to lower case
gU5j : change 5 lines below to upper case
gu3k : change 3 lines above to lower case
yy : copy a line
3yy : copy 3 lines
cc : change a line ( change is delete and go in insert mode )
cap : change a paragraph
. : repeat last command
f'ci'hello : _find the next ' then change everything inside ' for hello
ddp : moves line below
ddkP : moves line above
r : replaces single character
R : replaces more than one character
5i lorem : inserts lorem 5 times
10gs : puts (neo)vim to sleep for 10 seconds
search with /
/word.*
Commenting, commentary.vim (github.com/tpope/vim-commentary)
gcc : comment out a line
gcap : comment out a paragraph
Multiple Line Editing (native vim)
Ctrl + v : visual block mode
*select multiple lines*
I : insert mode
*add word or edit whatever*
Esc : leave insert mode, realizes changes
Multiple Cursors via plugin (github.com/mg979/vim-visual-multi)
Ctrl + x : start multicursor and directly select all matches
FIND AND REPLACE / SUBSTITUTION
:g/pattern/d : remove lines matching the pattern
:g!/pattern/d : remove lines that do NOT match the pattern
:v/pattern/d : remove lines that do not match the pattern
Ranges https://vim.fandom.com/wiki/Ranges
:s/old/new/g changes old to new in the current line
:11,15s/old/new/g changes lines 11 to 15 inclusive
:%s/old/new/g changes all lines
This is a comment by ThePrimeagen on Reddit on how to get quicker at navigating with Vim:
Faster Navigation
from the Primeagen on Reddit https://www.reddit.com/r/vim/comments/m330z4/getting_faster/
Insert Modes:
Learn to take advantage of o and O, A. They are awesome.
yanks/highlights/dels:
ciw, yiw, viw are amazing, but if you need to do the _whole_ word, try yiW. I do this a bunch. Imagine the following: Namespace::Class foo and you want to copy Namespace::Class, you could put your cursor at N and yf<space> but yiW also works (you don't have to be at the beginning of the word) (for this example I don't provide much benefit, but its incredible once you get it)
Vertical Navigation
get use to page ups and downs. I have been resistant for 9.5 years on those, and only since I adopted tmux (traveling the output) have I finally leaned in. ctrl+d/u is exceptionally awesome and they dont alter jump list.
Jump List
Take advantage of the jump list. Example:
I need to add an include/import. I use ggOimport foo from "bar";<esc><ctrl+o>. This will go to the top, insert mode top line, adds import, leaves insert, and travels back from whence I came (like the ring).
File Navigation
Fuzzy finders are great when you don't have an instant jump to the file. Use them. Don't use nerdtree / netrw / dirvish / etc etc etc etc etc.
QuickFix / LocalFix
Learn quick fix menus and their navigation. I have quit using <Ctrl-j/k> and <leader>j/k for window nav and instead use C-j/k for quickfix navigation and <leader>j/k for localfix navigation. cdo or bust
Sorry for the brain dump, but its been an incredible journey for me and I absolutely love to share some wisdom.
Ultimately, how I envision vim should work and why it works so well is that you "think" of what you want and there are keystrokes to accomplish it. If you find yourself just aimlessly scrolling, stop, why should be a big question on your mind.
Lastly, I hate to fearlessly shill my own product, but if you are interested in an alternative to file navigation and use neovim, I would be glad to share my experimental plugin.
Life is to short to proof read
LEARN RELATIVE JUMPS.
The "Fighting one-eyed Kirby" is what ThePrimeagen calls a Vim regex capture group to replace text in the buffer.
Primeagen's "fighting one-eyed Kirby"
video: https://www.twitch.tv/theprimeagen/clip/GentleBumblingLadiesAMPEnergyCherry-MlGwozjH5QU2gV8t
Further explanation: https://waylonwalker.com/thoughts-200/
The following explanation is from this comment on Reddit: https://www.reddit.com/r/theprimeagen/comments/12v4q06/can_anyone_explain_how_to_do_the_fighting_one/
foo
bar
baz
:%s/\(.*\)/bar\1/g
barfoo
barbar
barbaz
Whats going on here....
%s replace all lines
/ start pattern
\(.*\) the \ are escapes, so the regex is (.*) this basically captures everything on the line and places it into capture group 1
/ substitute section
bar\1 bar is just text, the \ is an escape and the 1 is the previous capture group.
/g finish the set