Assume that you want to add a double asterisk (**) to every line in a document.
e.g. you want to replace
hello, this is a line
this is another line
this is a third line
with
hello, this is a line**
this is another line**
this is a third line**
There are many ways to doing this, but one of the more general ways of doing this with regular expressions (which can be easily extended for other tasks) is
:%s/\(^.*\)/\1**
An extension: What if you want to introduce a line break before the word line?
Use \r
:%s/\(^.*\) \(line\*\*\)/\1\r\2/g should do it for you
As you notice \1 contains the match within the first parentheses. If there were multiple parentheses, the contents can be accessed using \1, \2 etc.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment