Gigi Labs

Please follow Gigi Labs for the latest articles. Programmer's Ranch no longer has its domain, so please update your bookmarks and links to programmersranch.blogspot.com.

Wednesday, September 18, 2013

Linux: Navigating Text with vi

Howdy folks! :)

In the last article here, "Linux: vi Essentials", we looked at several of the vi text editor's basic commands. Today we'll take a look at a few more, and we'll focus on commands that allow you to quickly jump across portions of text. You will begin to notice patterns in how vi commands are structured, and this will allow you to harness its power and use it very efficiently.

Let's start off with some text that we can work with. First, open a Linux terminal and fire up vi with the following command:

vi test.txt

Enter insert mode using the i command, and press Shift+Insert to paste the following lyrics from Queen's song Friends Will Be Friends:

It's not easy love, but you've got friends you can trust,
Friends will be friends,
When you're in need of love they give you care and attention,
Friends will be friends,
When you're through with life and all hope is lost,
Hold out your hand cos friends will be friends right till the end.

Press ESC to go back into command mode. Move the cursor to the second line, and alternate between pressing e and b. You'll see that e takes you to the end of the word (the 's' in 'Friends'), and b takes you back to the beginning of the word (the 'F' in 'Friends'). Next, try pressing 'w' repeatedly - you'll notice you will move to the next word each time (first to the 'w' in 'will', then to the 'b' in 'be', and so on). This will continue to take you to the next word even if it is on another line.

If you've read "Linux: vi Essentials", you have already seen the 'w' command in use - as part of the 'cw' (change word) command. As a matter of fact, such commands are really made up of several parts. The first part is the command itself, such as c (change) or d (delete). The second part tells it until where it should work, so cw actually means "change from where the cursor is until the next word". You can achieve a similar effect with other commands - dw will delete characters until the next word.

The ^ command will take you to the beginning of the line the cursor is on, and the $ command will take you to the end of the line. Once again, you can combine these with other commands. c$ will change from the current cursor position until the end of the line. y^ will copy (for later pasting) from the current cursor position back to the beginning of the line.

These multi-character commands also have some other interesting patterns. If you re-type the command rather than specifying where it should end, it will affect the entire line you are on (no matter where in the line the cursor is) - we have already seen this with dd and yy. The same goes for cc. If you put a number between or before the command (as we have seen in the last article, d5d and 5dd both delete 5 lines), you affect a number of lines. If you type the command as a single uppercase character (e.g. D), you affect from the current cursor position until the end of the line - so C is actually equivalent to c$.

Other handy navigation commands allow you to run around the entire file or jump to specific line numbers. The gg command will take you to the beginning of the first line in the file, and the G command will take you to the beginning of the last line in the file. A handy way to clear an entire file is to first go to the beginning of the file (gg) and then delete until the end of the file (dG).

You can jump to a particular line number using :n, where n is the line number you want (e.g. :1 is equivalent to gg). The line number you're on is indicated near the bottom-right of the editor together with the column. If you want to show line numbers, you can use the special command :set nu, but the lines will take a portion of your editing space. Use :set nonu to turn off line numbers.


There are a bunch of navigation commands that you can check at the Vi Cheat Sheet if you really want to go deep. I think the ones I mentioned above are more than enough to get you rolling. I'll just mention one more thing you'll definitely find useful.

Use a forward slash (/) followed by a word to search for that word (e.g. /hand). vi is very powerful (I keep repeating that) and supports regular expressions as well as search and replace, but those are beyond the scope of this article. You can read about them at the Vi Cheat Sheet or elsewhere on the internet.

Nice! I hope this helps you in your day-to-day vi operations, and check back for more articles! :)

Update 2013.11.28: if you want to learn some of the more advanced things you can do with vi, the answer to this question is a really fantastic overview.

No comments:

Post a Comment