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 Apr 22 '14 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} – Sean Allred Apr 22 '14 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. – samcarter_is_at_topanswers.xyz Oct 24 '16 at 19:04

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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