I would like Emacs or AucTeX to do the following:

here's some SELECTED TEXT for testing

Then, I could hit a key stroke and convert the highlighted text "SELECTED TEXT" to

here's some \mycustomcommand{SELECTED}{TEXT} for testing

  • 1
    How automatic should it be? I mean do you want to do it for a lot of commands, or only one? And what should happen if there are three words?
    – T. Verron
    Commented Apr 22, 2014 at 5:20
  • 1
    Easiest thing I can think of is simply recording a keyboard macro. Aside from that, C-c C-m will insert the command closely to what you want: \mycustomcommand{SELECTED TEXT} Commented Apr 22, 2014 at 5:21
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. Commented Oct 24, 2016 at 19:04

1 Answer 1


There are many ways to achieve this kind of behavior.

One is a dedicated elisp function, such as the following quick and ugly draft:

(defun insert-mycustomcommand (beg end)
  (interactive "r") ;; take the region
    (goto-char beg)
    (insert "\\mycustomcommand{")
    (insert "}{")
    (delete-char 1)
    (goto-char (+ end (length "\\mycustomcommand{") 2))
    (insert "}")))

Then you can bind it to a key of your liking and have the desired behavior. However, it will require to be rewritten for all commands. It can be somewhat automated, but it won't be very convenient.

Another option is to write it in a style file for auctex, but I'm not familiar enough with these to know if it will be easier. I don't think there is any command at the moment using this kind of "automatic cut".

Finally, you have the easy "by-hand" option. Normally, if you have in your preamble the definition of your function, selecting "SELECTED TEXT", pressing C-c C-m and entering mycustomcommand gives you

\mycustomcommand{SELECTED TEXT}{}

It is then a matter of a few keystrokes to correct it as intended:

M-f ;; forward-word, move one word forward
C-d ;; delete-char, delete the space
}   ;; self-insert-command, insert the character
{   ;; self-insert-command
M-f ;; forward-word
C-f ;; forward-char
C-d ;; delete-char
C-d ;; delete-char

As an alternative, assuming you use electric parentheses features (for example with smartparens), you can simplify the key sequence in:

M-f   ;; forward-word, move one word forward
C-d   ;; delete-char, delete the space
{     ;; insert "{}"
C-t   ;; swaps the next character and the previous one
C-M-u ;; sp-up-sexp
C-d   ;; delete-char
C-d   ;; delete-char

The second key sequence will work even if there are more words after the separating braces, the first would require extra M-fes.

I promise you it is less tedious than it looks.

  • I didn't know what to look for in the AucTeX documentation, but I'll have to look-up style files, then. Thanks.
    – Geremia
    Commented Apr 22, 2014 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .