In this post, I'm going to teach you how to efficiently navigate in Vim editor instead of you spamming keys j, k, l, h
(or the arrow keys) just to go to a certain character or line.
This post is for aspiring Vim ninjas that want to make Vim as their primary editor when coding, or even writing anything.
I recommend not to read the whole post but instead follow the guide that I will leave on each paragraph, so you would have time to learn and build that muscle memory as we go learn each command in Vim.
I suggest to archive or save this so you can go back to this post anytime. The goal is to learn it step-by-step, and not learning all at once.
Vim is great and powerful (if you have mastered all the keystrokes, shortcuts, mnemonics, etc), but it does take time to get used to it, especially building that muscle memory. It takes guts to fully grasp Vim and hone your skills with it.
Without further ado, let's start!
But first, let's go back to basics. To navigate to each character:
j
to go down, k
to go up, h
to get left, and l
to go right. For beginners, this may seem awkward because we used to do it on arrow keys or WASD
keys, in VIM, it's different.
When Bill Joy created the vi text editor he used the ADM-3A terminal, which had the arrows on hjkl keys. Naturally he reused the same keys and the rest is history.
https://catonmat.net/why-vim-uses-hjkl-as-arrow-keys
And for me, this is fine, so I can rest my fingers where I can reach the most common shortcut keys in Vim.
Now you know the basic navigation, let's take another level, will take it slowly, as we tackle each command that we will be using.
Legend
TTL - Time to Learn 😅
To start, copy this gist and save it somewhere you can easily find.
Once done, open your terminal and run this command vim path-to-lorem.txt
, this will bring up the lorem.txt
that you just saved. We will be using this sample text file to practice.
In order to navigate easily, we have to enable the Line Number
, most IDE has this built-in on their application, but in Vim, you have to enable this feature.
In your VIM make sure you are in Normal mode, type :set number
and hit enter, as you can see this will show the line number(s) in your editor, most of Vim users I know use Relative Number as this is much easier to navigate line by line.
You can see more about relative number by typing :h relativenumber
Show the line number relative to the line with the cursor in front of each line.
Commands
Navigate through each word
w
jump to the start of the word this includes punctuation.
W
jump to the start of the word, does not include punctuations.
b
jump to the end of the word this includes punctuation.
B
jump to the end of the word, does not include punctuations.
** Jump to the start or the end of the line **
0
jump to the start of the line
$
jump to the end of the line.
*** TTL: 5 to 10 minutes ***
End of Part 1
Top comments (0)