[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Adding String to Beginning of Lines
From: |
tomas |
Subject: |
Re: Adding String to Beginning of Lines |
Date: |
Tue, 10 Nov 2020 23:45:07 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Tue, Nov 10, 2020 at 02:40:08PM +0100, Christopher Dimech wrote:
> I now have got 'rec-cont', which lets me pass a string.
> How can I add a dafault if I don't pass a regex in the
> mini-buffer.
>
> (defun rec-cont (beg end regex)
> (interactive "r\ns rec-cont regex: ")
> (replace-regexp "^" regex nil beg end))
> ;; (replace-regexp "^" ";; + " nil beg end))
But watch out: the replacement string (confusingly called `regex' here,
others have already pointed that out) might contain special sequences
(e.g. "\\&") which have a meaning in this context.
If doing that from a program, better not use `replace-regexp' but do
it yourself in a loop along this lines:
(while (search-forward-regexp "^" nil 'noerror)
(replace-match replacement-string))
(I've taken the liberty to rename the replacement string `replacement-string').
The doc explicitly warns against using `replace-regexp' in
programs:
"This function is for interactive use only;
in Lisp code use `re-search-forward' and `replace-match'
instead."
Cheers
- t
signature.asc
Description: Digital signature
- Re: Adding String to Beginning of Lines, (continued)
- Re: Adding String to Beginning of Lines, Michael Heerdegen, 2020/11/10
- Re: Adding String to Beginning of Lines, Jean Louis, 2020/11/10
- Re: Adding String to Beginning of Lines, Michael Heerdegen, 2020/11/10
- Re: Adding String to Beginning of Lines, Jean Louis, 2020/11/10
- Re: Adding String to Beginning of Lines, Michael Heerdegen, 2020/11/11
- Re: Adding String to Beginning of Lines, Jean Louis, 2020/11/17
- Re: Adding String to Beginning of Lines, Michael Heerdegen, 2020/11/17
- Re: Adding String to Beginning of Lines, Stefan Monnier, 2020/11/10
- RE: Adding String to Beginning of Lines, Drew Adams, 2020/11/10
- Re: Adding String to Beginning of Lines, Michael Heerdegen, 2020/11/11
- Re: Adding String to Beginning of Lines,
tomas <=
- Re: Adding String to Beginning of Lines, Michael Heerdegen, 2020/11/11
- Re: Adding String to Beginning of Lines, Christopher Dimech, 2020/11/11
- Re: Adding String to Beginning of Lines, Michael Heerdegen, 2020/11/11
- Re: Adding String to Beginning of Lines, tomas, 2020/11/11
- Re: Adding String to Beginning of Lines, Christopher Dimech, 2020/11/11
- Re: Adding String to Beginning of Lines, Christopher Dimech, 2020/11/11
- Re: Adding String to Beginning of Lines, Michael Heerdegen, 2020/11/11
- Re: Adding String to Beginning of Lines, Stefan Monnier, 2020/11/11
- Re: Adding String to Beginning of Lines, Christopher Dimech, 2020/11/11
- Re: Adding String to Beginning of Lines, Jean Louis, 2020/11/10