Vi Tip of the Week: Moving one character at a time

This is part of a series of tips on using Vi.  To see the complete series, click here.

I notice a lot of my students using the arrow keys to move around when they’re editing a file in vi.  That works fine, but the big problem with it is that you have to move your hands off of the home keys to do it.  vi has a much better way of doing it: use the h, j, k, and l keys to move around one character at a time!  It’s so much quicker, because your hands stay on the home keys of the keyboard.

The keys and how they move the cursor are:

  • h: moves one character to the left (like the left arrow key)
  • j: moves down one line, staying in the same column (like the down arrow key)
  • k: moves up one line, staying in the same column (like the up arrow key)
  • l: moves one character to the right (like the right arrow key)

They seem awkward at first, but you get used to them pretty quickly.  Actually, they’ve become so ingrained to me, I don’t even think about them — in fact, I had to open up vi and try them out to remember which key moved in which direction!

To help you get used to the keys, it helps to practice by playing the old-school game rogue, as it uses the same directional keys!

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.