DEV Community

Cover image for 15 Vim shortcuts that will make your life easier
Sneh Chauhan for Kubernetes Community Days Chennai

Posted on • Updated on

15 Vim shortcuts that will make your life easier

Introduction

If you ever installed Linux in your life, I'm sure you might have come across Vim.

Vim is a command-line based text editor. It's a great tool but I won't say that it's the best text editor in Linux as it would lead to a never-ending debate.

Vim is actually an improved version of the old vi editor. It comes with many new features including multi-level undo, multiple window support, visual mode and command-line completion.

If you're using it for the first time, you may have hard time trying to exit Vim.
.
.
.
What? Don't you believe me?

Here's the proof :

In this blog, I'll list down 15 shortcuts that will save you a lot of time while working with vim.

Shortcut key Function
w To move forward by one word(you need to be in normal mode).
b To move backward by one word(you need to be in normal mode).
gg To move to the beginning of the file.
G To move to the end of the file(last line).
dw To delete the word, cursor is positioned upon.
dd To delete the line, cursor is positioned upon.
d2d To delete 2 lines, starting from the line, the cursor is upon.
You can replace '2' by any number and delete any number of lines.
u To undo the last operation performed.
Ctrl+r To redo the last operation performed.
/sample_text To search for the 'sample_text' in the file. Use n to move to next occurrence and N to move to the previous occurrence of the text(in Command mode).
:%s/old/new/g Replaces all the occurrences of the old text with new text(in Command mode).
:q! To quit the file discarding all the changes made to the file(in Command mode).
:wq To save the file and quit(in Command mode).
:w sample_filename To save the file with filename 'sample_filename'(in Command mode).
:q To quit Vim. It fails when changes has been made to file(in Command mode).

That's all for the blog. I hope you had good time reading the blog!πŸ˜ƒ

Top comments (9)

Collapse
 
moopet profile image
Ben Sinclair

I think some of these are slightly wrong:

w and b move forwards and backwards in normal mode, not command mode. In command mode, you have the little : prompt and can enter instructions for the editor to execute.

dw deletes from the cursor to the start of the next "word". To delete the word you're currently on, you'd use diw, daw, diW, or daW depending on exactly how you wanted to do it.

:%s/old/new will replace "old" with "new" in every line of the file, but it will only replace the first occurence of "old" in each line. If you want to replace every occurence, you'd pass the g flag like so: :%s/old/new/g

Your :w example suggests that it will use the filename "sample_filename" but you haven't put that in the command. It'd need to be :w sample_filename for that to work!

Collapse
 
sneh27 profile image
Sneh Chauhan

Thank you for your feedback Ben! I've made the corrections accordingly.

Collapse
 
grizzlysmit profile image
Francis Grizzly Smit

or to delete from the cursor to the end of the word you are on de

Collapse
 
willypuzzle profile image
Domenico Rizzo

Thanks for sharing. I use vim but in a very basic way!

Collapse
 
grizzlysmit profile image
Francis Grizzly Smit • Edited

we all start that way, then you add a few new commands, and a few more and so on, I'm learning all the time

Collapse
 
sneh27 profile image
Sneh Chauhan

Glad you liked it!

Collapse
 
kelseyjj profile image
Kelsey Jones

love this

Collapse
 
sneh27 profile image
Sneh Chauhan

Glad you like it Kelsey!

Collapse
 
netch80 profile image
Valentin Nechayev

Why omitted maybe the most important handy ones? ;))
ZZ to write and exit (like :x!, :wq!)
ZQ to exit without writing (like :q!)