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!

 

Vi Tip of the Week: Find the matching brace

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

A lot of the time when I’m working on a program, I want to quickly move my cursor to the matching brace.  For example, if I’m editing a nested if statement in C or Java, sometimes I’ll want to find the matching brace so that I know I’ve got my scope correct.  It’s very easy to do in vi — just place your cursor on the brace you want to match, then press the “%” key, and voila, you’re taken to the matching brace!

This works for parentheses and square brackets, too — it’s great for when you’re writing a long mathematical expression and you want to quickly check to make sure you’ve got your parentheses right!

Vi Tip of the Week: Move the current line to the centre of the screen

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

Sometimes when I’m editing a file, I like to have the line I’m looking at centred on the screen.  This helps me to see an equal amount of context both above and below that line.  I seem to never remember the shortcut that centres the screen on the current line, so I end up using the j or k key to move around in the document until I’ve got the line I want in the middle of the screen, and then I move back to that line.  That’s a waste of effort, but vi has a nice easy way of doing it: press z then the period (z.), and it does it for you!

Let’s see what this looks like.  In this screenshot, I’m editing a file, and I’ve got my cursor on the comment line that starts “This function calculates…”:

Before centreing the current line

But that’s too low on the screen for my liking; I want that line centred.  Press z period (z.), and my screen now looks like:

After centreing the current line

The line with my cursor is now in the centre of the screen!  OK, it’s not that exciting, but the shortcut saves a bit of time when you want to do this.

There’s a couple variations on this that come in handy, too.  I can move the current line to the top of the screen by pressing z then the return key (z RETURN), or I can move it to the bottom of the screen by pressing z then the minus sign (z-).

Vi Tip of the Week: What line number am I on?

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

If you are editing a file in vi, and you want to know what line number you are on, it’s very easy to find it: press Control-G, and the status line at the bottom of the screen will tell you!  See, for example, the following screenshot taken just after I pressed Control-G:

Vi: screenshot after pressing Control-G

As you can see, the status line displays the name of the file I am currently editing (in this case, Hamurabi.java), the line number that my cursor is currently on (line 30),  the total number of lines in this file (351), how far down into the file I am as a percentage (8% of the way through the file), and which column that my cursor is currently on (column 9).

Depending on which version of vi you are using, your status line may look slightly different. On both a Mac (where the above screenshot is from) and on a Debian Linux box, I see identical status lines.  However, with the version of vi on Fedora Linux, I don’t even need to press Control-G, as the status line always tells me which line and column I’m on:

In this case, the “30,9” indicates that my cursor is currently on line 30, column 9.  The “6%” on the right-hand side of the status line tells me that the top line displayed in my window is 6% of the way through the file.  If I press Control-G now, I get a little bit of additional information:

As with the Mac and Debian versions of vi, I am told the name of the file, how many lines in the file, and how far I am through the file.  It’s the same information, just presented slightly differently.