FactorPad
Build a Better Process

Learn Vim Navigation with hjkl Keys and Vim Numbers

Learn Vim Navigation with hjkl Keys and Vim Numbers.
  1. Home row - Keep your hands on the home row to translate code from your brain to a text file faster.
  2. Vim hjkl keys - Switch from mouse and arrow key navigation to the hjkl keys.
  3. Vim numbers - Learn how to zoom straight to a line in a Vim file and navigate faster with numbers.
  4. Practice, practice, practice - Try the Vimazing Race - hjkl Edition maze game to reinforce good habits from the start.
face pic by Paul Alan Davis, CFA
Updated: February 25, 2021
Breaking free from the mouse sounds easy and will take practice. Keep reading to learn the secrets.

Outline Back Tip Next

/ factorpad.com / tech / vim / reference / hjkl-vim.html


An ad-free and cookie-free website.


Memorize Vim hjkl Keyboard Navigation Once and For All

A tutorial on beginning navigation of near-proximity movements of the Vim cursor without taking your hands off the keyboard.

Video Tutorial

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)

Video Script

Beginner

The most granular level of Vim navigation is accomplished with four keys moving the cursor one character at a time.

So 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!

Example 1 - Open a text file and navigate with hjkl only

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.

$ vim -h > hjkl-1.txt

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.

Note: you will learn faster if you follow along.
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Feb 13 2017 01:41:24) usage: vim [arguments] [file ..] edit specified file(s) or: vim [arguments] - read text from stdin or: vim [arguments] -t tag edit file where tag is defined or: vim [arguments] -q [errorfile] edit file with first error Arguments: -- Only file names after this -v Vi mode (like "vi") -e Ex mode (like "ex") -E Improved Ex mode -s Silent (batch) mode (only for "ex") -d Diff mode (like "vimdiff") -y Easy mode (like "evim", modeless) -R Readonly mode (like "view") -Z Restricted mode (like "rvim") -m Modifications (writing files) not allowed -M Modifications in text not allowed -b Binary mode -l Lisp mode -C Compatible with Vi: 'compatible' -N Not fully Vi compatible: 'nocompatible' -V[N][fname] Be verbose [level N] [log messages to fname] -D Debugging mode -n No swap file, use memory only -r List swap files and exit -r (with file name) Recover crashed session -L Same as -r -A start in Arabic mode -H Start in Hebrew mode -F Start in Farsi mode -T <terminal> Set terminal type to <terminal> -u <vimrc> Use <vimrc> instead of any .vimrc --noplugin Don't load plugin scripts -p[N] Open N tab pages (default: one for each file) -o[N] Open N windows (default: one for each file) -O[N] Like -o but split vertically + Start at end of file +<lnum> Start at line <lnum> --cmd <command> Execute <command> before loading any vimrc file -c <command> Execute <command> after loading the first file -S <session> Source file <session> after loading the first file -s <scriptin> Read Normal mode commands from file <scriptin> -w <scriptout> Append all typed commands to file <scriptout> -W <scriptout> Write all typed commands to file <scriptout> -x Edit encrypted files --startuptime <file> Write startup timing messages to <file> -i <viminfo> Use <viminfo> instead of .viminfo -h or --help Print Help (this message) and exit --version Print version information and exit "filename" 2L, 85C 1,1 All

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).

Yes, do this on your system now. It will help.

Okay, so if you hit the j character about 49 times, you will come across a line that looks like this:

-h or --help Print Help (this message) and exit

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.

usage: vim [arguments] [file ..] edit specified file(s)

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.

Example 2 - Learn how to open Vim and have Vim go to line number 50

With that same file open try hitting k a few times until you come across this line.

+<lnum> Start at line <lnum>

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:

$ vim +50 hjkl-1.txt

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?!? :)

Example 3 - Learn a keyboard shortcut with numerical characters

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.

-N Not fully Vi compatible: 'nocompatible' -V[N][fname] Be verbose [level N] [log messages to fname] -D Debugging mode -n No swap file, use memory only -r List swap files and exit -r (with file name) Recover crashed session -L Same as -r -A start in Arabic mode -H Start in Hebrew mode -F Start in Farsi mode -T <terminal> Set terminal type to <terminal> -u <vimrc> Use <vimrc> instead of any .vimrc --noplugin Don't load plugin scripts -p[N] Open N tab pages (default: one for each file) -o[N] Open N windows (default: one for each file) -O[N] Like -o but split vertically + Start at end of file +<lnum> Start at line <lnum> --cmd <command> Execute <command> before loading any vimrc file -c <command> Execute <command> after loading the first file -S <session> Source file <session> after loading the first file -s <scriptin> Read Normal mode commands from file <scriptin> -w <scriptout> Append all typed commands to file <scriptout> -W <scriptout> Write all typed commands to file <scriptout> -x Edit encrypted files --startuptime <file> Write startup timing messages to <file> -i <viminfo> Use <viminfo> instead of .viminfo -h or --help Print Help (this message) and exit --version Print version information and exit "filename" 2L, 85C 1,1 All

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.

Example 4 - Practice with the Vimazing Race - hjkl Edition maze game

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.


Vim Questions and Answers

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.


Related Vim Commands and Procedures


What's Next?

Please subscribe and visit our YouTube Channel and @factorpad for other free opportunities to learn.

Outline Back Tip Next

/ factorpad.com / tech / vim / reference / hjkl-vim.html


vim hjkl examples
hjkl vim
learn vim
vim keyboard
keyboard navigation
vim keys
vim shortcuts
vim navigation
hjkl keys
vim hjkl
free online vim tutorial
vim count
vim examples
vim tutorial
vim reference

A newly-updated free resource. Connect and refer a friend today.