0

I wish to create a function to put in my .emacs (but I am no Elisp expert) in order to perform a certain number of replacements within an Emacs buffer. In particular, I want to:

  • replace any occurrence of \pnotes{*} (where * is some variable stuff, e.g. \pnotes{1.73}, \pnotes{1.41}...) with \notes followed by a line break

  • delete any occurrence of \ast{*} (where * is some variable stuff, e.g. \ast{0.03}, \ast{.70}...)

  • replace any occurrence of \nextvoice with the same string preceded by a line break

  • replace any occurrence of \ib with the same string preceded by a line break;

  • replace any occurrence of | with the same character preceded by a line break

  • replace any occurrence of & with the same character preceded by a line break

  • replace any occurrence of \en% with \en preceded by a line break

How can I achieve that?

  • 1
    Does this answer your question? [Repeat replacement until not possible?](https://emacs.stackexchange.com/questions/7585/repeat-replacement-until-not-possible) – Drew Dec 31 '20 at 17:13
  • 1
    There are many duplicates of this question. Search for tag `[replace]`. And see the Elisp manual, node [Search and Replace](https://www.gnu.org/software/emacs/manual/html_node/elisp/Search-and-Replace.html). – Drew Dec 31 '20 at 17:17
  • No, it does not answer my question because my main difficulty is to replace expressions including variables, as explained in my post. Thanks. – Rodolfo Medina Dec 31 '20 at 19:19
  • Then [this one](https://emacs.stackexchange.com/q/51707/105). It keeps the part you want to remove instead of the part you want to keep, but the idea and answer are the same. And [this one](https://emacs.stackexchange.com/q/4214/105) can help. And [this one](https://emacs.stackexchange.com/q/7481/105) says how to inject the value of a variable into Lisp code, if you're really talking about using *a variable* and not *variable text* (your question suggests the latter, but it's unclear). – Drew Dec 31 '20 at 20:37
  • Start by defining a keyboard macro, with `^X(`, then type the emacs commands to do what you want, then `^X)` and `ESC X edit-last-kbd-macro` to see the recorded elisp. – waltinator Dec 31 '20 at 21:11

1 Answers1

0

All your questions have a solution involving replacements using regular expressions. Browse for

(info "(emacs) Regular Expression Search")

. You can use M-x re-builder to build a regular expression.

For example

(query-replace-regexp "\\\\pnotes {[^}] *}" "\\\\notes
") 

will solve your first question,

C-M-% \\pnotes{[^}]*} RET \\notes C-q C-j RET 

in interactive mode.

(query-replace-regexp "\\\\ast{[^}]*}" "")

will solve your second.

(query-replace-regexp "\\(\\\\nextvoice\\)" "
\& ")

will solve the third and subsequent ones by replacing \\\\nextvoice with an appropriate expression.

In interactive mode, backslashes don't need to be doubled, for example for the last regexp, type

C-M-% \(\\nextvoice \) RET C-q C-j \& RET

The newline character is inserted by C-q C-j, you will understand.

gigiair
  • 2,124
  • 1
  • 8
  • 14
  • https://stackoverflow.com/help/someone-answers. Please do not add a comment on your question or on an answer to say "Thank you". – gigiair Jan 02 '21 at 07:42