DEV Community

loading...
Cover image for Another one short Vim cheat sheet

Another one short Vim cheat sheet

gleb_the_human profile image Gleb ・3 min read

I've used Vim time to time for a pretty long time, but mostly it was just some simple boring tasks like to change some configuration file on the remote server through SSH. Therefore I used the rather limited capabilities of Vim like just open the file, edit few lines and save it.

But recently I set out to study Vim deeper and read nice book for this - Learn Vim (the Smart Way). In the time of reading I made small notes about Vim's capabilities I discovered, and I'd to share them here in this short post with you!

Some of topics like advanced search, or Git, or etc I missed as I didn't want to put that all to my head for now (that's too much for me for the first time). So if I missed something important for you, probably I still need to grow to your level :) And yes, if you are mature experienced Vimmer, probably, all this stuff will be not pretty useful for you, as your fingers already knows all that.

Anyway here is the short list of Vim's stuff that you can to look for when you start to dig into it.

Insert Mode:
i - insert text before cursor
a - append text after cursor
o - starts new line below cursor & insert text
<esc> - exits insert mode

Normal mode:
. - replay previous change
u - undo
Ctrl-r - redo

Visual mode:
v - character-wise
V - line-wise
Ctrl-v - block-wise

Command-line mode:
:!command - execute an external command
:h - help

Buffers:
:new - create new buffer
:ls - list all buffers
:buffer [n] - select buffer n
:bdelete [n] - delete buffer n

Windows:
:split [filename] - horizontal slit
:vsplit [filename]- vertical split
Ctrl-w h - choose left window
Ctrl-w j - choose down window
Ctrl-w k - choose up window
Ctrl-w l - choose right window
Ctrl-w v - open vertical split
Ctrl-w s - open horizontal split
Ctrl-w c - close window
Ctrl-w o - make current window the only one on screen and closes other

Tabs:
:tabnew [filename] - open new tab
:tabclose - close tab
gt - go to next tab
gT - go to previous tab

Operators:
y - yank (copy)
d - delete and save to register
c - change
p - paste

Text Objects:
w - word
s - sentence
p - paragraph
i - inner text object
a - text object + whitespace

Motions:
h - left
j - down
k - up
l - right
0 - beginning of the line
$ - end of the line
w - beginning of the next word
e - end of the next word
b - beginning of the previous word
( - previous sentence
) - next sentence
{ - previous paragraph
} - next paragraph
% - another match of (), [], {}
nG - go to n line
gg - go to 1st line
G - go to last line

Search:
/ - search forward
? - search backward
n - next search match
N - previous search match
* - search for whole word under cursor forward
# - search for whole word under cursor backward
g* - search for word under cursor forward
g# - search for word under cursor backward

Substitution:
:s/old/new/ - substitute the first match
:s/old/new/g - substitute all matches

Macros:
qa - start recording macro in register a
q - stop recording macro
@a - execute macro from register a
@@ - execute last executed macros

Fold:
zf[operator] - fold according operator (motion or text object)
zo - open folded text
zc - close fold

For now that's all! I will continue to discover the magic of Vim world, hope you too:)

Discussion (9)

pic
Editor guide
Collapse
ayaanraj profile image
ayaanraj

Nice list.

Collapse
gleb_the_human profile image
Gleb Author

Thank you!

Collapse
dephraiim profile image
Ephraim Atta-Duncan

Thanks for the list.

Collapse
gleb_the_human profile image
Gleb Author

Thanks :)

Collapse
n8chz profile image
Lorraine Lee

Wow, I didn't even know about tabs. I know only a tiny tip of the iceberg, but enough to be addicted.

Collapse
gleb_the_human profile image
Gleb Author

I'm glad that you found something new for yourself :)
I really recommend to look at the book I mentioned in the post, it's not pretty huge, but there are a lot of useful information

Collapse
kryptobi profile image
kryptobi

Nice list

Collapse
sharakpl profile image
Sharak

Thanks! I kept trying to undo with Ctrl + z. This messed up everything and I had to :q! and start again.

What is even the purpose of Ctrl + z in Vim?

Collapse
gleb_the_human profile image
Gleb Author

Thank you!
I know that if you use console version of Vim in some shells like bash or zsh with 'Ctrl + z' you can suspend Vim and go to console. After that if you want then to return to Vim you can type 'fg'. It works not only with Vim.