/ factorpad.com / tech / vim / reference / hjkl-vim.html
An ad-free and cookie-free website.
A tutorial on beginning navigation of near-proximity movements of the Vim cursor without taking your hands off the keyboard.
If you prefer, this topic is also covered in video.
Videos can also be accessed from our Vim Reference Playlist on YouTube.
hjkl Vim - Learn Vim keyboard navigation with hjkl keys and numbers (9:20)
Beginner
The most granular level of Vim navigation is accomplished with four keys moving the cursor one character at a time.
h
- leftj
- downk
- upl
- rightSo a common first step with online coding courses for beginning Vim editors is to move around in a text file using just those four keys. Okay, it is time to begin your life as a computer programmer so be prepared to start coding!
Using hjkl
keys instead of a mouse
and arrows may seem archaic, but there's a reason professional
programmers use Vim. And later you will see how we can make this even
faster when we add numeric keyboard shortcuts.
First we will create and review a text file to learn the basics. At the
same time we will be doubly productive by navigating the help file for
Vim. Quickly create a text file in the current working directory using
the vim
command to get the help screen
like this.
This is a fairly standard help screen, on Linux and Mac machines at least, as most programs accessed from the command line have help files found using this convention command -h or command --help.
Open the file with vim hjkl-1.txt
.
This is Linux, so if it fails to work for you, simply open any text
file to practice hjkl navigation.
Now, without looking at your hands, navigate to the part of this file
that explains what we did when we created it (clue: finding help).
And only use four keys: h
(left),
j
(down),
k
(up) and
l
(right).
Okay, so if you hit the j
character
about 49 times, you will come across a line that looks like this:
So the instructions we gave earlier with
vim -h > hjkl-1.txt
printed help
vim -h
,
but instead of printing to the screen, we redirected output with
>
to a file called
hjkl-1.txt
. So that line should look
familiar. Also, if you noticed near the top was a line that details
how syntax is input when launching Vim.
This line describes how to open and edit files and as a convention at
FactorPad, and other online coding resources, it is common to use the
term options (or flags) instead of
arguments here, so to open the file we used the command
vim
followed by the
option -h
and
finally the file we wanted to open and edit, or
hjkl-1.txt
.
With the goal of making this a valuable free online coding resource for
you, the other purpose of using this file is so you see a few
advanced features in Vim. Try not to get bogged down with it, much
of this will not make sense, but a few lines will. And if you make a
mistake by hitting the i
key for
example you will go from Normal Mode to
Insert Mode which we would rather not do just yet. If
this happens to you, hit the Esc
key
to return to Normal Mode.
Keep the file open for the next example.
With that same file open try hitting
k
a few times until you come across
this line.
To interpret, the plus symbol +
and
lnum
number allow you to open Vim
right to a line of your choosing, so if we opened this same file
instructing Vim to go to line number 50, we could save time.
Give it a try. To close the file and quit Vim, make sure you are in
Normal Mode by hitting
Esc
, then type
:q
, or
:q!
if you made any changes to the
text file.
After you return to the command line, type this:
The cursor will now be at row 50, column 1. Imagine how helpful this is when text files approach 1,000 lines.
See how much fun navigating Vim can be?!?
:)
Okay, everything earlier was done one character at a time. Now we will
get our first glimpse at why Vim is so fast. First, navigate to the
position: row 50, column 1 using only the four keys mentioned earlier
hjkl
.
Now type: 10k
followed by
12l
and notice what happened.
Your cursor moved up to the line we discussed earlier, so
10k
means go up 10 lines, and
12l
moves right 12. So you can think
of the numbers in front of navigation keys like multipliers.
Also, if you noticed, Vim moved more than 12 characters to the right. The reason for this is because some of those characters are tabs, but let's not focus on that for now. The point is to see how to move around more quickly than one character at a time.
I suggest playing around with this, move around until you feel
comfortable before moving on. Also, don't forget,
if you mistakenly go from Normal Mode to
Insert Mode, just hit the
Esc
key.
With the basics covered, take some time to practice reinforcing these one-character navigation keys in your memory until you stop using your brain, and just as importantly, stay away from your mouse and arrow keys.
A maze game is a perfect way to do that. With the Vimazing Race - hjkl Edition you take 417 steps through a maze from start to finish using just hjkl and the penalty for an error is returning to the starting line. Try that a few times, put yourself on the clock and after some time you will have mastered the hjkl keys and will be ready for more advanced Vim navigation.
Read more about the Vimazing Race - hjkl Edition maze game, including how to get the free download and get started.
Q: How will you know when it is time to move on?
A: Like driving a car or riding a bicycle, if
you can move around without looking at your hands and without thinking,
you are ready to keep advancing.
Please subscribe and visit our YouTube Channel and @factorpad for other free opportunities to learn.
/ factorpad.com / tech / vim / reference / hjkl-vim.html
A newly-updated free resource. Connect and refer a friend today.