Java Reference
In-Depth Information
:7,.w! filename writes lines from line 7 up to and including the current
line to the named file, clobbering any previous contents (think of the “!”
as meaning “and don't argue with me!”).
:q quits, provided you've saved your changes (e.g., with :w ).
:q! quits without saving any changes to the file.
:n doesn't exit vi , but moves on to the next file if you started up with
more than one file to edit (e.g., vi Fir.java Pine.java ). When you've
reached the last file in the list, you need to quit—for example, with :q .
2.2.2
We've mentioned searching for a string with / or ? , but what about replacing?
Once you've located a string with / , you can use cw or C or R or other such
commands to effect the change. Search for the next occurrence with n , and then
you can repeat your change (the last c , s , r , and so on) by typing the period
. ” that will repeat that last substitution, insert, and so on.
But what if you want to make 225 substitutions? Typing n.n.n.n.n.n.
would get old after a while. Here, the ex mode, like any good command line,
comes to the rescue to help with repetitive tasks.
If we want to search and replace all occurrences of one string for another,
we can use the command
Search and Replace
:1,$s/one string/another/
Almost all ex commands take an address range , that is, the lines of the file
over which they will operate. If just one line number is given, the command
will operate on that one line. Two numbers, separated by commas, represent
the start and end lines—inclusive—of the operation. The first line is line 1, so
a 0 as line number would mean “before the first line.” The line where the cursor
is currently located is just “ . ” (a period). The last line of the file can be repre-
sented by the dollar sign ( $ ). You can even do some simple math on the
addresses—for example, .+2 meaning the second line in front of the cursor's
current line.
TIP
There is a shortcut for the 1,$ address range. Use % to mean “all lines”—for
example, %s/one string/another/ .
Search WWH ::




Custom Search