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.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel sapien ut sapien finibus finibus. | |
Ut sed facilisis est, vel aliquam nibh. Etiam sodales nunc id tortor commodo ultrices. | |
Fusce purus nisi, hendrerit in lorem et, pulvinar tempor elit. Aenean suscipit mi | |
id augue suscipit dapibus. Quisque ut ligula quis metus tincidunt molestie. | |
Ut fermentum pharetra pellentesque. Donec ac consectetur orci, quis porta libero. | |
Vivamus vel rhoncus nisi. Maecenas molestie eget eros et condimentum. | |
Aliquam malesuada sem quis ipsum tempus, eget eleifend purus tempus. | |
Nulla quis semper justo. Pellentesque habitant morbi tristique senectus et netus | |
et malesuada fames ac turpis egestas. Vivamus et est et odio viverra maximus. | |
Integer pretium elementum eros ac accumsan. Proin vitae eleifend sapien. | |
Nam egestas, metus sit amet cursus aliquam, erat dui lobortis sapien, sit amet | |
ultricies neque diam vitae diam. Nullam ut nisl ut dui eleifend aliquam. | |
Suspendisse ac magna a odio tincidunt pellentesque. Aenean non ligula in nulla | |
luctus dignissim eget eu tellus. Mauris finibus diam sapien, in vulputate lacus | |
iaculis sit amet. Suspendisse pellentesque elit id bibendum tincidunt. Aenean | |
congue orci tristique cursus mollis. Cras bibendum lectus eu justo consectetur | |
posuere. Proin hendrerit tempor velit a tempus. Vestibulum mi diam, commodo vitae | |
est sit amet, fringilla fringilla augue. Phasellus porta, massa vitae pellentesque auctor, | |
sapien neque semper libero, ac tempor orci sem eu diam. Pellentesque habitant morbi | |
tristique senectus et netus et malesuada fames ac turpis egestas. Quisque pretium | |
velit ex, sit amet scelerisque sapien suscipit et. Vivamus aliquam pretium nunc venenatis cursus. | |
Sed interdum sed ex sed pulvinar. Aenean a pulvinar augue, et ultrices orci. | |
Praesent risus sem, feugiat id odio sed, pharetra rhoncus eros. Phasellus ultricies | |
fermentum justo, sed posuere tortor placerat a. Etiam varius vulputate sollicitudin. | |
In in lorem vulputate, imperdiet sapien placerat, aliquet lacus. Pellentesque habitant | |
morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum | |
tempus eu enim ac pellentesque. Etiam facilisis dictum fermentum. Morbi pellentesque mollis convallis. | |
Integer efficitur augue sit amet eros blandit, at rutrum lectus dictum. Curabitur blandit tincidunt nisl id vulputate. | |
Etiam a bibendum massa. Donec tempus, purus iaculis pulvinar lacinia, nulla ligula | |
sollicitudin tellus, quis pretium orci nulla et lorem. Nunc maximus dolor ut volutpat laoreet. | |
Vivamus tincidunt aliquet nulla, vel vehicula nibh eleifend et. Sed eleifend ultricies euismod. | |
Nulla a nunc sed nibh malesuada convallis non ac massa. Nam id est id magna maximus aliquet a at ante. | |
Mauris at lacus blandit, malesuada felis eu, semper diam. Nam bibendum vitae ante id ullamcorper. | |
Donec vulputate efficitur arcu. Aenean viverra auctor tortor, vestibulum ultricies leo congue a. | |
Mauris semper tellus nec semper ornare. Praesent at eleifend orci. | |
Donec sapien est, placerat eget metus quis, aliquam dapibus justo. Sed dictum, ante a | |
pulvinar rhoncus, leo risus fringilla leo, ac suscipit magna leo non dolor. | |
Etiam faucibus ante mauris, ac condimentum nunc fringilla id. Donec tincidunt nulla velit. | |
Morbi at mi vel lorem tempor tristique. Mauris quis lectus nibh. Quisque ullamcorper, | |
urna quis malesuada facilisis, tortor diam maximus nisl, sit amet congue sem purus ac elit. | |
Aenean condimentum mollis orci eu dapibus. Nullam semper turpis vel tortor porttitor convallis. Sed et faucibus ex. |
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)