In a regular expression ^
signifies the beginning of a line and
$
specifies the end of a line. To have an operation apply
to multiple lines at once, you can specify a range of lines, e.g.
16,25
. To have the operation apply starting with the current
line, you can use a period, .
, to represent the current line,
so .,25
would mean apply the operation to the current line
and all subsequent lines up to and including line 25. Or you could specify
that the operation should be applied from the current line to the end of
the file by using $
to represent the last line in the file,
e.g., .,$
.
%
. E.g., the following line would insert 123
at the beginning of every line in a file.
:% s/^/123/
The s
indicates a substitute operation will follow and the
forward slashes, /
delineate the pattern to be replaced and
the replacement pattern. In the above case, the ^
indicates the
beginning of the line and the replacement pattern is 123
.
When you are using /
to delineate the patterns, it has a special
meaning and if you want to use it in a pattern you have to "escape" its
special meaning with an
escape character, which is the backslash, \
.
E.g. to insert </td>
at the end of every line from the
current line to line 25, the following regular expression could be used:
.,25 s/$/<\/td>/
Supposing that I wanted to put a <td>
at the beginning
of each line and a </td>
at the end of each line from the
current line through line 25. In that case I need to store the characters
in between the beginning and end of a line. You can specify text to be
stored by using parentheses, (
to mark the beginning of the
area on the line to be stored and )
to mark the end. Since a
period, .
, represents any character and an asterisk, *
represents multiple occurrences, ^(.*)$
would store
all the characters on the line between the beginning and end of a line.
You must also "escape" the (
and )
with the
backslash escape character as well. To reuse the characters you have stored
you recall them with \1
("1" is the number one). If you had
multiple occurrences of characters enclosed in parentheses, the second
instance could be recalled with \2
.
The line below would insert a <td> at the beginning of lines 16 through 25 and a </td> at the end of the lines.
:16,25 s/^\(.*\)$/<td>\1<\/td>
If you wished to have the editor prompt you as to whether you wanted
the change made on a line, you could add a /c
option at the
end of the command to the editor. The command below would perform the same
action as the one above on every line from line 16 to the end of the file,
but would prompt you at each line as to whether the change should be made.
:.,$ s/^\(.*\)$/<td>\1<\/td>/c
You would see each line highlighted one by one as you progressed through the file with the following prompt each time.
replace with <td>\1<\/td> (y/n/a/q/l/^E/^Y)?
Answering y
would result in the replacement occurring whereas
answering n
would result in the line remaining unchanged.
In this case, there is also a simpler means for inserting the table td
tags at the beginning and end of each line as below:
s/.*/<td>&<\/td>
Again the .*
represents the search pattern, which is every
character on the line. The ampersand, &
, also has a special
meaning; it is the text that matches the search pattern, so putting it between
the two tags results in all the text originally on the line remaining on
the line. The /
at the end of the replacement pattern can be
eliminated if there is nothing following it such as a /c
.
References: