Vim: A Superpower!


Why Vim editor?
In my opinion, Vim is a powerful yet underrated text editor that can be used to write and edit the code quickly. It offers you speed and efficiency, allowing you to navigate and edit text without leaving the keyboard.
Problems that I faced while learning to use Vim :
Struggled with text insertion in the editor.
Had trouble exiting Vim.
Encountered issues saving changes in the editor and quitting simultaneously.
Needed to exit Vim to run basic Linux commands.
Confusion in normal, insert and command mode.
How I solved those problems?
To insert any text inside the Vim editor, you Press i.
To exit Vim, you Press esc + :q. (Note: If you haven't written any text)
To save the inserted text and quit simultaneously, you Press esc + :wq. (i.e write and quit). If you insert a code and then follow the above command Press esc + :q then you get the following error.
To run Linux commands inside Vim, you Press esc + :! (command name)
Key takeaways :
The first thing I would like you to know is about Normal, Insert and Command mode.
When you enter Vim, you are by default in the Normal mode.
If you want to insert text or code, you Press i. Then you are eligible to insert a text.
When you want enter into command mode, press esc. Now you are eligible to run commands such as quit, save, linux commands and so much more with a colon before.
Bonus tricks :
To exit Vim forcefully without saving changes, use
:q!
. To save changes and quit, use:wq
or type:w
followed by:q
.To enter Insert mode and move the cursor to the end of the line, press
Shift + A
.To undo changes, press
Esc
and thenu
.Move the cursor to the beginning of the line press
Esc
0
and to the end with (Shift + 4)$
.In Normal mode, use
x
to delete a character anddd
to delete an entire line.Navigate in Normal mode with
h
(left),l
(right),j
(down), andk
(up).Import file contents of another file with
:r filename
and run Linux commands with:! command
(e.g.,:! ls
).Use
%s/text to replace/new text
to find and replace text.Go to the start of the file with
Esc + gg
and the bottom withShift + g
.Toggle line numbers with
:set number
and:set nonumber
.
Additionals :
Here are the Vim shortcuts for each of the frequent tasks.
1. Navigate to the top or first line
- Shortcut:
gg
- Shortcut:
2. Navigate to the last line
- Shortcut:
G
3. Navigate to the first word
- Shortcut:
^
(Moves the cursor to the first non-whitespace character of the current line.)
4. Navigate to the end of the current line
- Shortcut:
$
5. Navigate to the start of the current line
- Shortcut:
0
(Moves the cursor to the absolute beginning of the line, including spaces.)
6. Copy a line
- Shortcut:
yy
7. Paste a line
Shortcut:
p
(Pastes the copied or cut content below the cursor.)Shortcut:
P
(Pastes the copied or cut content above the cursor.)
8. Copy whole code
Shortcut:
ggVGy
gg
: Go to the top of the file.V
: Enter visual line mode.G
: Select all lines until the end of the file.y
: Yank (copy) the selection.
9. Paste whole code
- Shortcut:
p
(Place the cursor where you want to paste the entire code, then pressp
.)
10. Delete a line
- Shortcut:
dd
11. Undo delete
- Shortcut:
u
12. Delete whole code
Shortcut:
ggdG
gg
: Go to the top of the file.dG
: Delete everything from the top to the end of the file.
13. Undo delete whole code
- Shortcut:
u
Resources that helped me to learn:
Vim - [Free] : https://youtube.com/playlist?list=PLT98CRl2KxKHy4A5N70jMRYAROzzC2a6x&feature=shared
Subscribe to my newsletter
Read articles from Sahil Naik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sahil Naik
Sahil Naik
๐ป Sahil learns, codes, and automates, documenting his journey every step of the way. ๐