Vi Tip of the Week: Moving a word at a time

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

If you want to move around in a document quickly with vi, there are 3 useful commands (or 6, depending on how you look at it) for moving word-by-word.

Pressing the w key will move you forward word-by-word.  For example, if you’ve got the line:

This is a test of moving word-by-word.

and your cursor is positioned on any character in “This”, you can press w and the cursor will be moved to the start of the next word, “is”.  Pressing w again will move the cursor again, this time to “a”.

If you want to move backwards word-by-word, you can use the b key.  For example, if the cursor is positioned at the start of “moving”, pressing b will move the cursor to the start of “of”.  Pressing b again will move the cursor to “test”, and so on.

If you want to move forward to the end of a word, you can press the e key.  With the cursor placed on the start of “This”, pressing e will move the cursor to the “s” at the end of “This”. If the cursor is already positioned at the end of a word, pressing e will move you to the end of the next word.  For example, with the cursor positioned on the “s” in “This”, pressing e will move the cursor to the end of “is”.

But I mentioned that there are really 6 commands.  Try the following line of text:

Let’s eat, Grandma.

(A classic example of the importance of punctuation!)  If the cursor is positioned on the “L” in “Let’s”, pressing w will move the cursor to the apostrophe.  Pressing it again will move the cursor to the “s”, and pressing it three more times will move you to the “e” in “eat”, the comma, and the “G” in “Grandma”.  So, as you can see, the w command considers punctuation to be separate words.

If you press Shift-W instead, vi ignores punctuation.  With the cursor on the “L” of “Let’s”, pressing Shift-W moves the cursor to the “e” in “eat”, and pressing it again will move the cursor to the “G” in “Grandma”.

Similarly, Shift-B moves the cursor backwards a word, ignoring punctuation.  With the cursor on the “a” at the end of “Grandma”, pressing Shift-B once will move the cursor to the beginning of “Grandma”.  If you were to press b now, the cursor would be positioned on the comma, but if you press Shift-B, the cursor is positioned on the “e” at the start of “eat”.

Finally, Shift-E works similarly with punctuation.  With the cursor on the “L” in “Let’s”, pressing e moves the cursor to the “t” in “Let’s”, but pressing Shift-E moves the cursor to the “s”.

Summary:

  • w moves the cursor forward a word at a time, and stops at punctuation
  • Shift-W moves the cursor forward a word at a time, ignoring punctuation
  • b moves the cursor backward a word at a time, and stops at punctuation
  • Shift-B moves the cursor backward a word at a time, ignoring punctuation
  • e moves the cursor to the end of the word, or to the next word if it’s already at the end of the word, and stops at punctuation
  • Shift-E moves the cursor to the end of the word, or to the next word if it’s already at the end of the word, ignoring punctuation.

Vi Tip of the Week: Searching

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

If you want to search for something in a file, you can use the forward slash character: /. Type forward-slash, the text that you want to search for, then press the Return key.  Vi will take you to the next place in the document where that text appears, and will place the cursor on the first character.  So, if I wanted to search for my name, Shane, I would type:

/Shane

and press Return.  If the text doesn’t appear in the file, I’ll get a message on the status line telling me that (the exact contents of the message depend on which version of vi you are running).

If you want to search for the same text again, you can just type the forward slash followed by Return — vi will remember what the last text you searched for, and will search for it again.  But, if you type anything after the slash, it will search for that instead.

Another thing you can do to search for the same text is to press the n key — it will take you to the next occurrence of that text!

/ searches forward; if you want to search backwards, use the question mark instead.  If I want to search for the previous occurrence of my name, I type

?Shane

And if I want to search for the same text again, it works just like the forward slash — type ? followed by Return, and it remembers what I searched for last, and searches backwards for that again.

OK, here’s some magic — if I press n, it searches in the same direction as my last search! If I had been searching forward, then press n to search for the same text, it searches forward.  But, if I had been searching backward with ?, pressing n will now search backward for the same text!

The search string can also include some simple regular expressions.  If I want to search for my name at the start of a line of text, I use ^:

/^Shane

Or, if I want to search for it at the end of a line of text, I use $:

/Shane$

Finally, if I want to go back to the line I was on just before my previous search, I can press the backquote twice: “.  This will take me to my previous position.

Have fun with searching!!!

Vi Tip of the Week: Scrolling up and down in a file

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

When someone wants to move up or down in a file that they are editing, I often notice that they either use the arrow keys to move one line at a time, or the Page Up and Page Down keys to move a screen at a time.  Although this works fine, it has the disadvantage that you have to move your hands off the home keys, which messes up your touch typing.  Fortunately, vi offers commands for doing the same thing:

  • Ctrl-F works like Page Down: it moves you down a page in the document, leaving the last two lines on the previous page as the top two lines of the next page to help retain context.  You can remember Ctrl-F as “move Forward”.
  • Ctrl-B works like Page Up: it moves you up a page in the document, leaving the top two lines on the previous page as the bottom two lines of the next page to help retain context.  The mnemonic for this command is “move Backward”.

But maybe you don’t want to move a full page at a time — maybe the common 2 lines isn’t enough context.  You can also scroll up and down half a page at a time:

  • Ctrl-D moves you down half a page.  The previous bottom line is now displayed in the middle of your screen.  You can remember this with “move Down”.
  • Ctrl-U moves you up half a page.  The previous top line is now displayed in the middle of your screen.   Remember this one with “move Up”.

Finally, maybe you just want to move the display one line up or down:

  • Ctrl-E moves the display up one line, but, unlike the previous commands, it doesn’t move your cursor — your cursor stays on the same line it was on before executing Ctrl-E.  Another way of thinking of this command is that it exposes a new line at the bottom of the screen, hence the mnemonic “Expose a line”.
  • Ctrl-Y is the opposite of Ctrl-E: it moves the display down one line (exposes a new line at the top of the screen), and leaves your cursor in the current position.  Sadly, I’ve got no easy memory trick for remembering this one — if you’ve got one, add it in the comments below!

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.

Vi Tip of the Week: Open a file in read-only mode

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

Sometimes I want to open up a file to look at it, but not change its contents.  I could use the commands “more” or “less”, but I prefer to use vi for longer files, mostly because I’m so used to using vi.  To do so, I can use the command:

vi -R filename

where “-R” tells vi to open the file in read-only mode.  Although, I never open it with the -R option; instead I use the command:

view filename

which also opens the file in read-only mode.

Sometimes, though, I’ll be looking at the file, and realize I actually do want to make a change to it.  The hard way would be to close the file, then open it again in the regular editing mode, but instead, I can use the command mode to force a write:

:w!

If I use “ZZ” or “:w” to try to write the file, I get an error message telling me that the “readonly” option is set, but if I use the exclamation mark “:w!”, I can force vi to write the file.

Vi Tip of the Week: Go to a specific line

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

Sometimes when I open up a file in vi, I want to go to a specific line.  Perhaps I had a compile error on a certain line, and I want to take a look at that line.  It’s nice and easy to do: when in command mode, just type the line number, followed by an uppercase G.  So, for example, if I type:

35G

I’ll be taken to line 35.

Actually, in the situation I mentioned, there’s an even better way to do it — when I open the file, I can specify what line to go to with the +linenumber option on the command line.  For example, to open a file called dummy.txt and go directly to line 35, I can type:

vi +35 dummy.txt

and the cursor will be placed on line 35.

There are a couple variants that I use frequently: if I want to go to the first line, I enter:

1G

and if I want to go to the very last line of the file, I don’t put a number first, just the uppercase G

G

takes me to the last line.

Vi Tip of the Week: Transpose characters

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

OK, this is my favourite command in vi, not because it’s especially useful, just because I think it’s neat.  If you make a typing mistake and get two characters switched around, it’s easy to fix.  For example, if I want to type “Linux”, but I mistakenly type “Liunx”, all I have to do is place my cursor on the “u”, use the “xp” (transpose) command, and it swaps the “u” and the “n” around, making “Linux” as I originally wanted.

I sort of lied; this isn’t a single command — it’s two commands that result in transposing the characters.  The first command, x, deletes the character under the cursor and saves it in vi’s “unnamed” buffer.  Then, the second command, p, spits out what’s in that buffer, placing it after the cursor.  So, the result is that the two characters are transposed (fancy word for switched around).  What I think is cool is that these two separate commands, x and p, when combined as xp, perform an operation that is easy to remember: xp = transpose!  Cool!!!