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 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,166
  • 1
  • 9
  • 15
  • 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