|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
| Old News ;-) | See Also | Recommended Links | Reference | .vimrc | History | Humor | Etc |
However, most vi users have a difficult time searching and
replacing. Luckily this is quite easy. Simply use :'a,'bs/old/new/g
where a and b are marks or line numbers (without
the '). The optional g means to replace everywhere. Without it, only the first
occurance on each line will be replaced. To replace green with blue in your
entire document, use :1,$s/green/blue/g To replace blue with green in lines 10
through 24 use :10,24:s/blue/green/g
Searching and replacing text vi has a number of (pretty obscure) search command. You can search for individual characters through to regular expressions. The main two character based search commands are f and t.
fc Find the next character c. Moves RIGHT to the next.
Fc Find the next character c. Moves LEFT to the preceding.
tc Move RIGHT to character before the next c.
Tc Move LEFT to the character following the preceding c.
(Some clones this is the same as Fc)
; Repeats the last f,F,t,T command
, Same as ; but reverses the direction to the original command.
If the character you were searching for was not found, vi will beep or give some other sort of signal.
vi allows you to search for a string in the edit buffer.
/str Searches Right and Down for the next occurrence of str.
?str Searches Left and UP for the next occurrence of str.
n Repeat the last / or ? command
N Repeats the last / or ? in the Reverse direction.
When using the / or ? commands a line will be cleared along the bottom of the screen. You enter the search string followed by RETURN.
The string in the command [/] or [?] can be a regular expression. A regular expression is a description of a set of characters. The description is build using text intermixed with special characters. The special characters in regular expressions are . * [] ^$ .
. Matches any single character except newline.
\ Escapes any special characters.
* Matches 0 or More occurrences of the preceding character.
[] Matches exactly one of the enclosed characters.
^ Match of the next character must be at the begining of the line.
$ Matches characters preceding at the end of the line.
[^] Matches anything not enclosed after the not character.
[-] Matches a range of characters.
The only way to get used to the regular expression is to use them. Following is a series of examples.
c.pe Matches cope, cape, caper etc
c\.pe Matches c.pe, c.per etc
sto*p Matches stp, stop, stoop etc
car.*n Matches carton, cartoon, carmen etc
xyz.* Matches xyz to the end of the line.
^The Matches any line starting with The.
atime$ Matches any line ending with atime.
^Only$ Matches any line with Only as the only word in the line.
b[aou]rn Matches barn, born, burn.
Ver[D-F] Matches VerD, VerE, VerF.
Ver[^1-9] Matches Ver followed by any non digit.
the[ir][re] Matches their,therr, there, theie.
[A-Za-z][A-Za-z]* Matches any word.
vi uses ex command mode to perform search and replace operations. All commands which start with a colon are requests in ex mode.
The search and replace command allows regular expression to be used over a range of lines and replace the matching string. The user can ask for confirmation before the substitution is performed. It may be well worth a review of line number representation in the ed tutorial.
:<start>,<finish>s/<find>/<replace>/g General command
:1,$s/the/The/g Search the entire file and replace the with The.
:%s/the/The/g % means the complete file. (Same as above).
:.,5s/^.*//g Delete the contents from the current to 5th line.
:%s/the/The/gc Replace the with The but ask before substituting.
:%s/^....//g Delete the first four characters on each line.
The search command is very powerful when combined with the regular expression search strings. If the g directive is not included then the change is performed only on the first occurrence of a match on each line.
Sometimes you may want to use the original search string in the replacement result. You could retype the command on the line but vi allows the replacement string to contain some special characters.
:1,5s/help/&ing/g Replaces help with helping on the first 5 lines.
:%s/ */&&/g Double the number of spaces between the words.
Using the complete match string has its limits hence vi uses the
escaped parentheses [
(] and [)] to select the range of the substitution. Using an escaped digit [1]
which identifies the range in the order of the definition the replacement can be
build.
:s/^\(.*\):.*/\1/g Delete everything after and including the colon.
:s/\(.*\):\(.*\)/\2:\1/g Swap the words either side of the colon.
You will most likely read the last series of gems again. vi offers powerful commands that many more modern editors do not or can not offer. The cost for this power is also the main argument against vi. The commands can be difficult to learn and read. Though most good things can be a little awkward at first. With a little practice and time, the vi command set will become second nature.
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: February 28, 2008