[Top][All Lists]

[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

  "This function is for interactive use only;
  in Lisp code use `re-search-forward' and `replace-match'

- t

Attachment: signature.asc
Description: Digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]