< Zurück | Inhalt | Weiter >

Chapter 2

An Embarrassment of Riches: Editors


Here the joys of creating and changing text files are introduced, the rudiments of the venerable vi editor are presented, and the power of text is exalted.



Readers are encouraged, but not required, to plumb the depths of vi. Other text editor choices are briefly covered.

Remember our recommendation: If you learn only one shell, learn bash. If you learn only one editor, learn vi. Some basic shell scripting will go a long way to eliminating mundane, repetitive tasks. Some basic vi editing will let you do things much faster than you can with GUI editors.



Java programs consist of Java classes. Java classes are text files with Java state- ments and expressions. In order to write a Java program, then, you need to be able to enter text into a file. Sounds simple enough.

With Linux and its GNU tools, you have an amazing array of choices for how to do this. Some are GUI tools not unlike simple word processors. Others, like vi and Emacs, predate GUI tools, but provide much the same capability without the luxury (or annoyance) of a mouse or menus.1

The editor named vi (pronounced as you would spell it: “vee-eye”) is one of the most enduring tools in Linux. Its popularity comes from a combination of power and ubiquity—you can find it on virtually every release of UNIX and Linux since 1985. But it is a powerful editor that can do a lot with only a few keystrokes.

There are actually several variants of vi from which to choose. Each is

someone’s attempt to go one better on vi, but all retain the same basic syntax and what you learn here will work equally well on any of the vi clones. You can choose among




Start up vi by typing the command name at a shell prompt, followed by the name of the file(s) that you want to edit:

$ vi Account.java

Keep in mind that vi was developed in the days of character-only video screens. Keyboards didn’t always have arrow keys or other special characters, which have since been (largely) standardized by the advent of the IBM PC. In that situation, the authors of vi had only the alphabetic characters to use for all


1. We realize that vi is famous for being difficult to learn and nonintuitive. The UI design of vi dates back to earliest cursor-addressable display terminals. User interface design has come a long way since then. The vi UI does indeed show its age. But the program refuses to die. Why? Because while simple GUI editors make the easy stuff easy, vi makes the hard stuff easy. You can fall back on pico or kate or other GUI editors if you want, but bear with us. Mastering vi really does pay off.

of their commands . . . and did they make good use of those keys! Virtually every letter is used, both lower and upper case, to mean something unique in

vi. But don’t be put off by the large number of commands to learn; they fit


some patterns that will make it easy for you to become proficient in a short time by learning a few commands and applying the patterns.


If you really can’t bear to part with your mouse and menus, try gvim. We haven’t used it, but we hear that it has support for mice to help with cut and paste and the like. After you learn vi and get a little practice, though, you may find that you’re never reaching for your mouse any more when you edit.

There are three modes to vi: the regular vi mode, some extended commands in the ex mode, and the input mode.


The simplest mode is the input mode. In input mode, every character you type becomes part of the text of the file. It’s how you enter the bulk of the text for your Java programs. But vi doesn’t start up in input mode; you have to “get into” input mode, and then get back out. Once out, you can use other vi commands to save the text and exit vi. More about those in a bit.


Get out of input mode by pressing the Escape key. You can press it more than once, too, just to be sure that you are no longer in input mode. If you are no longer in input mode and you press escape, it will beep at you—with an audible or visual notification, depending on how your terminal window is set to respond.

In both vim and elvis (two popular vi clones) there is a status line at the bottom of the window that will show if you are in input mode. In vim, look in the lower left and elvis, the lower right. When in input mode, you will see a status word displayed like insert or replace.

In the vi mode, the default mode that you start in, all the keystrokes are interpreted as commands to the editor. They are not displayed as characters. So when we describe a command, such as dt;, you can type those three characters but will not see those characters on your screen. Instead you will see some ac- tion taken by vi—in this case it will delete text from your cursor up to the first semicolon on that line, if any (otherwise it will just beep).

There are several ways to get into input mode, depending on where you want to do the insert. When the file you’re editing is completely empty, all these commands are equivalent, but for nonempty files, each command will begin input mode in a different place in the file:

i inserts before the cursor.

I inserts at the beginning of the line.

a appends after the cursor.

A appends at the end of the line.

o “opens” a line for input after the line on which the cursor sits.

O “opens” a line for input before the line on which the cursor sits.

Remember that this is character-based editing, before the days of mice and I-bars. So there is no meta-character for the cursor to show its position between two characters in the file. Instead, the cursor sits on top of a character, and thus inserts or appends will happen before or after that character.

Reminder: Get out of input mode by pressing the Escape key.

Next, let’s move the cursor around. The simplest way to do that is one character at a time. Using the (lowercase) h, j, k, and l keys—notice that they’re all in a row on QWERTY keyboards—you have the “arrow” keys for left, down, up, and right. One of the common enhancements for vi clones is to include support for the arrow keys on standard PC keyboards. Even so, the convenience of having the motion keys on the “home row” for touch typists can be a great speedup.

h moves left one character.

j moves down one line.

k moves up one line.

l moves right one character; same as a space.

Often, character- or line-at-a-time is too slow. Move to the beginning of the line that you are on with 0 (zero), or to the end of the line with $. Move to the top and bottom of the window with H (think “High”) and L (think “Low”). So first type L then hold down j. To move back in a file, first type H then hold down k. That gets the display moving down or up respectively.

H (“high”) moves to the top line of the window.

M (“middle”) moves to the middle line of the window.

L (“low”) moves to the bottom line of the window.

0 moves to the beginning of the line.

$ moves to the end of the line.

This may still be too slow for you, especially if you are working your way through a large file. If you want to page up and down half a page at a time, try

^U and ^D (think “up” and “down”). To move a full page at each keystroke, try

^F and ^B (think “forward” and “back”).

This may still take a while, especially if you want to get to the absolute beginning or end of the file. For those locations, type a three-character se- quence, starting with a colon—which will jump your cursor to the status line of the window—then type either zero or the dollar sign, then press the Enter key. For example, :$.

So what’s with the colon? Just when you thought you were getting the hang of the vi keystrokes, this odd pattern appears. It’s called ex mode, and has to do with the history of vi being built atop the ex editor. Typing the colon got you back giving commands to ex, without the fancier screen-based GUI. (Even editors can have command lines.) There are many powerful search/replace commands that you can do from the ex command line; more on that later. For now, though, remember that you can type the colon, then a line number, then the Enter key and vi will position the cursor (and thus what is displayed on the screen) to that line number. The 0 and $ are just special cases of that more generic way to position your place in the file.

Back to our positioning in the file. Recall that h, j, k, and l will move you one unit (char or line) at a time. Now enhance that motion by typing a number first, then the h, j, k, or l. So to move five lines up type 5k (just be sure you use a lowercase letter). You can move way down in a file by typing something like 2000j which will move down 2,000 lines. If the file doesn’t have that many lines, you will find your cursor at the end of the file.

The point here is that almost any vi command can be preceded by a count,

to repeat it that many times.

A few more navigation tips. Another useful way to move through text is a word at a time. You can move your cursor forward by a word with the letter w (for “word”). You can move “back” with the letter b. You can move five words at a time with 5w or 5b. See?

The definition of “word” to vi has to do with alphanumerics separated by

whitespace, but also by certain punctuation characters. So to vi, the following Java code consists of how many words? Seven.

myArraylist.doSomething(magical); // cool

From the beginning of the line, you’d need to type w seven times (or know to type 7w—but how could you guess seven?) To help out, vi uses the upper- case W to skip words defined not by punctuation but solely by white space. Think of it as “bigger” words. And of course B will go “back” by these bigger words. So on our example line, a single W will get you to the start of the comment.

Be sure that you’re not just reading these descriptions. Run vi on any file that you can find and practice navigating by lines or words or screens. Once you get the hang of it, it can be so much faster than reaching for the mouse and trying to maneuver the cursor into just the right spot between letters.

Sometimes you can see where you want to go based on the characters of text in the document. See that “x”? That’s a relatively rare character on any line of text. If you see a character, your cursor can “find” it if you type f and then the character you are looking for. So fx would search forward on the line for an “x”. And Fx would search backward from the cursor. To repeat the search, just type a semicolon (“;”).

Searching for a string is another good way to move your way through a file. To search forward, type a slash (/), then the characters for which you want to search, and end the string with a second slash and then Enter:2


To search backwards (towards the first line of the file) use the question mark rather than the slash to bracket your search string. In either case, to jump to the next occurrence, type n, or 27n to jump to the 27th occurrence. Whether you are searching forward (/) or backward (?), using uppercase N will reverse the direction as it searches for the next occurrence. So, you can search forward with /myVar/ and then press n for each next occurrence forward. If you go too far, just type N to back up. Similarly, if you were going backwards looking for an occurrence of a constructor, say something like: ?new HotClass?; then each n will search toward the top of the file, and each N will search toward the end of file.


2. The second slash is optional in most vi implementations, but used for consistency with the same command in ex mode which has optional suffix characters.

In both cases, when you hit the top or bottom of the file, vi will wrap and keep searching from the opposite end, though a warning message will appear in the status bar.

OK, enough navigation. Let’s start modifying text.

Copy and paste operations can be done easily on whole lines. Just yank and put the lines. You can yank a single line or several at a time (e.g., 7y) and then a single put (p) will deposit a copy just after the current line (the line where your cursor sits). If you want to put the text before, not after, the current line, use uppercase P.

Go ahead. Try it on the file you’re practicing on. It’s the best way to get a feel for what we’re describing here.

Cut and paste operations involve deleting the lines, not just copying them. This gets us into our third and final kind of syntax in vi, the double letter commands. Use dd to delete a line. Try it and you will find that the line your cursor is on just got deleted, and the cursor now rests comfortably on the next line. To paste that line back, use the same p or P that we used for to put the lines that we had copied (a.k.a. “yanked”) above.

But why the dd? What’s with the double letters? Think of “delete” for d and then add another letter to describe how much you want to delete—dw for “delete word” or dW for “delete the bigger words” (see above). So why dd for a line? We don’t know for a fact, but we suspect that it’s just for speed. You can also follow a d with the h, j, k, or l of our cursor movement, and that will delete either a character or a line in the appropriate direction.

A faster way (one keystroke, not two) to delete a single character is with the x key. And of course 5x, or 27x, will delete multiple characters. But if you’re deleting many characters you will probably get it done faster by deleting “words” (dw or dW).

Another powerful way to delete text is to delete it from the cursor up to a

specific character. The sequence dt; will delete from the cursor up to (but not including) the semicolon on the current line. If there is no semicolon, vi will beep, and no change will be made. To delete from the cursor up to and includ- ing the semicolon, use df;.

Everything you’ve just learned about delete is also true for change, the c in vi. You can combine it with itself (cc) to change a whole line. You can combine it with w to change a word (cw), or you can change from the cursor up to the next semicolon (ct;), and so on.

Change does the delete, then puts you in input mode. (Notice the re- minder on the status line.) Remember, to get out of input mode and back into vi mode, press the Escape key.

Sometimes you’ve done too much, and you’d like to undo what you’ve just done. Typing u will undo the last change that you made. But here’s a dif- ference between the classic vi and some of the new, improved versions. In classic vi, if you type another u, then you are telling vi to undo what it just did—which was an undo. So the undo of an undo remakes the change that you had originally made. But in vim, “vi improved,” typing u again and again will just keep undoing previous changes. If you want to undo the undo, in vim, you need to type :redo and then Enter.

The vi editor has shortcuts for helping you to change the indentation of

your code. Typing two less-than signs (<<) will shift the line to the left; typing two greater-than signs (>>) will shift the line to the right. Typing a number first and then the less-than or greater-than signs will shift that many lines at once. But how far will they shift? The default is usually set at eight, but you can set it to any value you want. In ex mode you can set all sorts of values and flags, customizing vi’s operation. The value we’re interested in here is shiftwidth which can be abbreviated sw. So the command would be :set sw=4 if you want each shift to move by four characters. For more about this, and how to make it your default, see Section 2.2.4.

Any command that you do may be worth repeating. Say, you just shifted 14 lines and you’d like to shift them further. Or you just deleted five lines, and would like to delete five more. Well, you could just retype the command, but an easier way is just to type the period (.) and let vi repeat it for you.


2.2.1 Exiting

2.2.2 Search and Replace

2.2.3 The Joy of Regular Expressions

2.2.4 Starting Off Right: .exrc

2.3.1 Editing Your Pipes (sed, the Stream EDitor)

2.3.2 Simple Graphical Editors