[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to rename files to numbers in Eshell?

From: tomas
Subject: Re: How to rename files to numbers in Eshell?
Date: Mon, 13 Sep 2021 13:52:12 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

On Mon, Sep 13, 2021 at 12:28:24PM +0300, Jean Louis wrote:
> Dear Felix, maybe you forgot to insert mailing list in your answer.
> > Jean Louis <> writes:
> > > Is there any regular expression counter replacement so that each new
> > > replacement get a new number?
> > Example replacement:
> >     \,(1+ \#)
> > This starts counting at 1.  If you want to start at 0, then simply use:
>     \#
> That is definitely interesting, please correct me, as I don't get it how to 
> apply it, as following is not working:
> (replace-regexp-in-string "ABC" "\#" "ABCM ABCI ABCJ ABCY ABC8")

I think this notation is only for interactive input. Besides, the
wrapping "\,(...)" is important, meaning "interpolate this Lisp
expression's value here".

For what you have in mind you could make use of the fact that
`replace-regexp-in-string' also takes a function:

(defun make-counter (startval) ; the "classical" counter closure
  (lambda (match)
      (format "%d" startval) ; we have to evaluate to a string, I think
      (setq startval (1+ startval)))))

(replace-regexp-in-string "ABC" (make-counter 23) "ABCM ABCI ABCJ ABCY ABC8")

  => "23M 24I 25J 26Y 278"

(don't forget lexical binding, it won't work otherwise ;-)

 - t

Attachment: signature.asc
Description: Digital signature

reply via email to

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