Elisp in Replacement String
susam.net
susam.net
:%s/\([0-9]+\))/\,(1- \#1))/
will turn 1) one
2) two
3) three
into 0) one
1) two
2) three :%s/\([0-9]\+\))/\=submatch(1)-1.')' " vim
perl -pe 's/([0-9]+)\)/$1-1 .")"/e'
ruby -pe 'sub(/([0-9]+)\)/){"#{$1.to_i-1})"}'[0] https://www.gnu.org/software/emacs/manual/html_node/elisp/Ba...
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.
https://www.gnu.org/software/emacs/manual/html_node/emacs/Re...
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.
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/
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.