8 ms·
Elisp in Replacement String
- mbork_pl 3y agoPlug: https://mbork.pl/2013-09-18_Selective_replacement_in_LaTeX_documents_(en) https://mbork.pl/2013-09-18_Selective_replacement_in_LaTeX_d...
- tmtvl 3y ago> 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.
- jhellan 3y agoNice!
- koito17 3y agoSomehow, 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 3y agojust to be sure: you don't look for recursive edit do you? https://www.gnu.org/software/emacs/manual/html_node/emacs/Recursive-Edit.html https://www.gnu.org/software/emacs/manual/html_node/emacs/Re...
- taeric 3y agoI'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__ 3y agoIt'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.
- kagevf 3y agoWow! and you can C-p back to see earlier ones too! Thank you for sharing this ... to me it's similar in impact to q: in vim.
- skulk 3y agoThis 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 3y ago:%s/\([0-9]\+\))/\=submatch(1)-1.')' " vim perl -pe 's/([0-9]+)\)/$1-1 .")"/e' ruby -pe 'sub(/([0-9]+)\)/){"#{$1.to_i-1})"}'
- globular-toast 3y agoWhat do you mean by live preview? My non-evil Emacs shows you the matches in the current buffer as you type. I don't think I did anything to enable it, but think it's a fairly recent thing (maybe Emacs 29).
- agumonkey 3y agocute trick I always forget
- onedognight 3y agoFor 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/Backquote.html https://www.gnu.org/software/emacs/manual/html_node/elisp/Ba...
- bewaretheirs 3y agoI would have said that the comma recalls the way that comma is used inside backquoted expressions.
- globular-toast 3y agoThanks, that's a great way to remember it!
- globular-toast 3y agoI 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/ https://blog.gpkb.org/posts/emacs-regexp-replace/
- foobarqux 3y agoAnother way to insert counters is the keyboard macro counter (info "(emacs) Keyboard Macro Counter") kmacro-set-counter (C-x C-k C-i) Then <F3> to start recording the macro and <F3> again to insert the counter. <F4> to end the macro and <F4> to run it. So <F3><F3><space><F4><F4><F4><F4><...> will insert the integers starting from zero separated by a space. This can be more useful in certain cases, for example when you need to do "structural editing" like editing a certain field in JSON where it can be easier to locate the edit point using keyboard shortcuts or search.