If you want to replace or insert characters at the beginning of a line,
the line beginning is represented by the
caret character, i.e.,
^
(Shift-6 on a standard computer keyboard), and line
endings are represented by the
dollar sign character,
i.e., $
.
E.g., the following quote has three occurences of the word "us". If I wanted
to replace only the "us" at the end of the line with "ourselves", I could hit
the colon key while not in vi's insert mode, which would give me a colon prompt
and then enter s /us$/ourselves/
which would replace the occurence
of "us" at the end of the lne with "ourselves".
What lies behind us and what lies before us are tiny compared to what lies within us
~ Ralph Waldo Emerson
The s
tells vi that I want to perform a substituion while
the
forward slashes separate the
string
I want to replace, us$
, which is the word "us" at the end of the
line with the new string, "ourselves".
If I wanted to replace all occurences of the word "us" in the line with
ourselves, I could use s /us/ourselves/g
. I.e., I would omit the
$
and add a g
at the end to indicate I want to
replace the word globally on the line, i.e., all instances of it.
If you wish to replace a forward slash in a line, you can use a
backslash
as an escape character
, i.e., its presence before a character "escapes" the meaning that would
otherwise be attached to that character. E.g., if I wanted to replace both of
the forward slashes in "1/4 and 1/2" with dashes, I could use s /\//-/g
. You don't necessarily have to use forward slashes to separate the
string you want to locate and the replacement string. E.g., for the same
replacement I could use colons, i.e., s:\/:-:g
or
s :us$:ourselves:
in the example of the Ralph Waldo
Emerson quote. And, if I'm not using the forward slash to separate the
to be replaced pattern and the replacement pattern I don't have to escape
the meaning of the forward slash, i.e,, I could use s :/:-:g
to replace the forward slashes with a dash.
If I want to perform a substitution only at the beginning of a line, I can
use the caret, ^
character. E.g., suppose I have the following
lines:
this royal throne of kings, this sceptred isle,
this earth of majesty, this seat of Mars,
this other Eden, demi-paradise,
this fortress built by Nature for herself
Against infection and the hand of war,
this happy breed of men, this little world,
this precious stone set in the silver sea,
Which serves it in the office of a wall,
Or as [a] moat defensive to a house,
Against the envy of less happier lands;
this blessed plot, this earth, this realm, this England...
If I want to change all occurences of "this" where the word occurs at the beginning of a line to have a capital "T", but only when the word occurs at the beginning of a line, if I position the cursor at the first line and if the last line above is the last line in the file, I can use the following:
.,$ s/^this/This/g
The period specifies I want to start making changes from the current line and the dollar sign specifies I want to go all the way through to the last line. You can also specify specific line numbers, e.g., if the first line was line 58 and the last line was line 67, I could use the following, instead.
58,67 s/^this/This/g
Sometimes you might want to perform a substitution only if the line begins with a character in a certain range of characters. E.g., suppose I have the following lines in a text file that I want to convert to an HTML file for a web page:
Part Number:
- CM-9E8A-B
Case Color:
- Black (Red)
Case Material:
- 0.5mm SECC Steel
Tower Size:
- Micro ATX/ ITX Tower
Motherboard Support:
- Micro ATX/ ITX
I want to insert "<tr><td>" before any line that begins with alphabetic character that is a capital letter from A to Z. I don't want to insert the "<tr><td>" before any of the lines that begin with a dash. I could use the following to make that subsitution assuming the relevant line numbers are lines 176 through 186.
176,186 s /^[A-Z]/<tr><td>&/g
By putting "A-Z" within the brackets, I'm specifying that I want to
search for any occurences of the letters A through Z, though just the capital
letters. Putting the "^" before the left-most bracket means that I only want
to perform the substitution when the letter occurs at the beginning of the
line. The
ampersand, &
, represents whatever pattern I searched for,
so for the substitution "<tr><td>" will be inserted followed
by whatever it was I searched for, i.e., the search pattern of a capital
A through Z at the beginning of a line. So I would get the following,
instead, as a result of the substitution:
<tr><td>Part Number:
- CM-9E8A-B
<tr><td>Case Color:
- Black (Red)
<tr><td>Case Material:
- 0.5mm SECC Steel
<tr><td>Tower Size:
- Micro ATX/ ITX Tower
<tr><td>Motherboard Support:
- Micro ATX/ ITX