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?

5
  • 1
    Does this answer your question? Repeat replacement until not possible?
    – Drew
    Commented Dec 31, 2020 at 17:13
  • 1
    There are many duplicates of this question. Search for tag [replace]. And see the Elisp manual, node Search and Replace.
    – Drew
    Commented Dec 31, 2020 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. Commented Dec 31, 2020 at 19:19
  • Then this one. 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 can help. And this one 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
    Commented Dec 31, 2020 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
    Commented Dec 31, 2020 at 21:11

1 Answer 1

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.