[HN Gopher] Elisp in Replacement String
       ___________________________________________________________________
        
       Elisp in Replacement String
        
       Author : todsacerdoti
       Score  : 62 points
       Date   : 2024-01-11 09:45 UTC (1 days ago)
        
 (HTM) web link (susam.net)
 (TXT) w3m dump (susam.net)
        
       | jhellan wrote:
       | Nice!
        
       | koito17 wrote:
       | Somehow, I was unaware of C-M-% for replacements, though I use
       | M-% all the time for a (naive) string search then replace all
       | instances by pressing the ! key.
       | 
       | This is a very neat trick, and little things like this are why I
       | keep using Emacs. Part of me wishes there were some "universal"
       | version of the M-: keybind for evaluating elisp expressions in
       | any interactive command.
        
         | froh wrote:
         | just to be sure: you don't look for recursive edit do you?
         | 
         | https://www.gnu.org/software/emacs/manual/html_node/emacs/Re...
        
         | taeric wrote:
         | I'm not clear what you mean there, but it seems that C-u C-x
         | C-e, gets a lot of the way there?
        
         | __david__ wrote:
         | It's not exactly what you want, but "C-x ESC ESC" will bring up
         | the last run command in lisp form and let you edit it before
         | running it again. That's how I used to repeat M-% replacements
         | before they added the nice history to it.
        
       | skulk wrote:
       | This all works in evil-mode as well, and by default you get the
       | cool live-preview (not sure how to enable that for the vanilla
       | C-M-%)                   :%s/\([0-9]+\))/\,(1- \#1))/
       | 
       | will turn                   1) one         2) two         3)
       | three
       | 
       | into                   0) one         1) two         2) three
        
         | mgdlbp wrote:
         | :%s/\([0-9]\+\))/\=submatch(1)-1.')'    " vim            perl
         | -pe 's/([0-9]+)\)/$1-1 .")"/e'            ruby -pe
         | 'sub(/([0-9]+)\)/){"#{$1.to_i-1})"}'
        
       | agumonkey wrote:
       | cute trick I always forget
        
       | onedognight wrote:
       | For those wondering about why the \, is used to indicate the
       | start a lisp expression, the backslash is used to escape all
       | replacements and the comma recalls the lisp backquote (`)
       | function (operator) which is like quote ('), but allows
       | interpolation of evaluated lisp expressions when preceded by a
       | comma[0].
       | 
       | [0]
       | https://www.gnu.org/software/emacs/manual/html_node/elisp/Ba...
        
         | bewaretheirs wrote:
         | I would have said that the comma recalls the way that comma is
         | used inside backquoted expressions.
        
         | globular-toast wrote:
         | Thanks, that's a great way to remember it!
        
       | mbork_pl wrote:
       | Plug:
       | https://mbork.pl/2013-09-18_Selective_replacement_in_LaTeX_d...
        
         | tmtvl wrote:
         | > _It 's so good to be an Emacs user._
         | 
         | I wholeheartedly agree. While Elisp may not be the best Lisp,
         | it's still very much good enough to give you superpowers when
         | you need them.
        
       | globular-toast wrote:
       | I didn't know about using elisp in the replacement! Can't wait to
       | need to use it.
       | 
       | You can also do this replacement across an entire project and
       | emacs also uses "smart" caps by default so that case is preserved
       | from match to replacement. I wrote about it here:
       | https://blog.gpkb.org/posts/emacs-regexp-replace/
        
       ___________________________________________________________________
       (page generated 2024-01-12 23:00 UTC)