Vim has too many shortcuts and it can be confusing π΅βπ« for newcomers. So, I comprised a list of 8 shortcuts from the perspective of a person coming from normal text editors β¨οΈ.
For the people who are asking why not just go through Vim Tutor? Vim tutor is easy but It has too many shortcuts to wrap our mind around. Normally We don't use all of them all the time so learning all at the same time is not productive as we will eventually forgetπ΅ them!
To develop the πͺπ§ muscle memory, first we have to work with these limited shortcuts.
So, let's get started!! π₯³
π Prerequisites:
- Any Vim editor Or VS Code with Vim extension (I will be using this)
π΄ Before the shortcuts:
Vim has three text modes:
- Normal mode - Where you will use the vim shortcuts
- Insert mode - Where you can actually edit the text with normal keystrokes
- Visual mode - Where you will select the text
ποΈHow to switch between modes:
Escape
to exit current text mode or to enter Normal modei
to enter Insert Modev
to enter Visual Mode
Look at the status bar at the bottom to know which mode you currently in.
β¨οΈ Shortcuts:
π§ 1. Navigate cursor in Normal mode:
In Vim, we use h, j, k, l to navigate between lines.
- π
h
for moving right - π
l
for moving left - π
j
for moving down - π
k
for moving up
π 2. Edit Text
First enter Insert mode using i
. That's it! Now Text is editable normally.
Use Escape
to exit Insert mode.
π 3. Navigate to start or end of the line in normal mode
shift+6
or^
to get to the start of the line or the first non-blank charactershift+4
or$
to get the end of the lineshift+a
to append to the end of the line and switch to insert mode
π 4. Going to the end or beginning of the file:
shift + g
orG
- to go to last line of the filegg
- to go to the first line of the file
β©οΈ 5. Undo or Redo changes in file
u
to undo changesctrl + r
to redo changes
π 6. Search text
/
followed by the text you are searching forn
- to go to next occurrenceshift+n
- to go to previous occurrence
π 7. Replace text
:%s/text1/text2/g
- replace text1 with word you want to change and text2 is the replacement word- add
c
to the end like:%s/text1/text2/gc
to confirm every replacement- For every replacement you get to these options
y
for replacing the current entryn
to not replace the current entry and skips to next entrya
to change all entriesq
to quitl
replace only the first entry and quit
- For every replacement you get to these options
- add
πΎ 8. Save and quitting vim
shift+zz
or:wq!
- save and exit the file:q!
- exit file without saving
Tada!! You are done.
Liked my article? Give a thumps up!! Have some feedbacks? Comment them below!!