I'd like to write an elisp function that allows me to replace any line in my buffer matching a regex pattern <PATTERN>
with text <TEXT>
. In other words, if line N
matches <PATTERN>
, then line N
is replaced with <TEXT>
.
For example, if <PATTERN>
is ^foo
and <TEXT>
is bar
, then running the elisp function within the buffer
Arcu ante habitasse inceptos congue.
Odio magnis at aenean at est.
foo eget, eu vitae.
Class.
Adipiscing dis donec tellus imperdiet curae.
fooSem sit tristique lacus ut porta accumsan.
Amet ad duis velit sit amet.
would change the buffer to
Arcu ante habitasse inceptos congue.
Odio magnis at aenean at est.
bar
Class.
Adipiscing dis donec tellus imperdiet curae.
bar
Amet ad duis velit sit amet.
This is exactly the behavior of the sed
command sed '/^foo/c\bar'
.
.*
suffixed to your regexp. Or^.*foo.*
for matching anywhere in a line. Of course you can write a function which searches for only the original pattern and then manipulates the current line each time it finds a match, but my point is that you don't need to do that unless you really want to.