How to add dynamic touch to emacs macros and or replace query?

I often have to perform several query and replace operations on a line, because, for example, I want to change all occurrences from 23 to 24 on a line, then 24-25 on the next line, etc. (usually because I write an expression in a line, which I repeat several times in subsequent lines and should slightly change) ...

A macro with Cx q, or performing multiple query replacements regularly does not seem strong enough in such situations. Is there something more general / flexible to handle substitution variables or macro variables like these that I can look at? I believe that I once met an example on the Internet where lisp expressions were introduced into certain commands in order to be more powerful, but I cannot remember this or where I read about it.

+5
source share
4 answers

I'm not sure if this answers your question completely, but I found registers a useful tool for creating powerful macros. The most important features are:

  • C-x r n number-to-register copy number at point to register
  • C-x r + increment-register increases the value stored in the register
  • C-x r i insert-register inserts a register value into the buffer

, :

  • number-to-register
  • replace-string, insert-register, increment-register, insert-register

emacs : https://www.gnu.org/software/emacs/manual/html_node/emacs/Registers.html

+6

, , ataylor, .

, C-u M-: - , , , , , .

(, , elisp , C-u M-! C-u M-| .)

, , , - , ( elisp , , ); , .

- " → " ( ) . , , .

, , .

, , , - . , , .

+3

, , query-replace-regexp.

M-x query-replace-regexp
Regexp: \([0-9]+\)
Replace with: \,(+ 1 (string-to-number \1))

( Emacs \d) , .

:

  • - ,
  • \,
  • , .
+2

elisp , .

( , , .)

, , 23 , , :

C-2 C-3 F3

C-SPC       ;; set-mark-command
C-e         ;; move-end-of-line
<<replace-regexp>>  ;; replace-regexp
<f3>        ;; kmacro-start-macro-or-insert-counter
C--         ;; negative-argument
M-@         ;; mark-word
M-w         ;; kill-ring-save
RET         ;; indent-new-comment-line
\           ;; self-insert-command
,(1+        ;; self-insert-command * 4
SPC         ;; self-insert-command
C-y         ;; yank
)           ;; self-insert-command
RET         ;; indent-new-comment-line

F4, .

(n.b. , .)

C-x C-k r .

You can re-set the macro counter to any desired value with C-x C-k C-cto re-play the macro in another region.

And depending on how your data really looks, you could probably greatly simplify this by completely eliminating replace-regexp, and instead just moving to where you know the number, deleting the existing number and inserting the counter value in its place .

+1
source

All Articles