0

When working in LaTeX (specifically, with AUCTeX), I want to bind the following functionality to the C-{ and C-} keys:

  • C-{: insert \begingroup followed by a newline
  • C-}: insert \endgroup followed by a newline

As per this thread on LaTeX Stack Exchange I've tried the following:

(eval-after-load "latex"
  '(progn
     (define-key LaTeX-mode-map (kbd "C-{") "\\begingroup\n")
     (define-key LaTeX-mode-map (kbd "C-}") "\\endgroup\n")))

It doesn't work, however, and if I try C-h k C-} nothing happens. How can I make my keybindings work?

I have tested the above behavior in at least three modes, Emacs-Lisp, LaTeX (AucTex) and XML mode.

I am using GNU Emacs 24 in Ubuntu 14.04.

UPDATE 1

I completely emptied my init file and then restarted emacs. The odd behavior persists.

UPDATE 2

All the proposed solutions, including my original one, has started to work.

I am without a clue.

  • It works for me provided that you've bound a function to it. What is the behavior you are expecting? Are you running Emacs in a terminal window, by any chance? – Dan Nov 14 '14 at 10:54
  • @Dan I was trying use this solution, tex.stackexchange.com/a/212160/14103. It did not work. – Masroor Nov 14 '14 at 10:59
  • @Dan No, I am not using terminal mode. – Masroor Nov 14 '14 at 11:00
  • 1
    Are you sure this key works in other programs? Sounds like your OS is intercepting this key for something. – Malabarba Nov 14 '14 at 11:08
  • 1
    For future reference, you don't need to empty your init-file if you want to check whether a problem is present in stock Emacs. Just start Emacs via emacs -Q. This will prevent any customizations from being loaded. – itsjeyd Nov 14 '14 at 11:46
3

It's much more common to bind a key to a command rather than a string, although, as @Malabarba notes in the comment, you actually can bind a key to a string which is self-inserted (news to me, but it works!).

You can define two new functions (note the interactive part, which makes them commands):

(defun insert-begin-group ()
  "Inserts \"\\begingroup\" followed by a newline."
  (interactive)
  (insert "\\begingroup\n"))

(defun insert-end-group ()
  "Inserts \"\\endgroup\" followed by a newline."
  (interactive)
  (insert "\\endgroup\n"))

(define-key LaTeX-mode-map (kbd "C-{") 'insert-begin-group)
(define-key LaTeX-mode-map (kbd "C-}") 'insert-end-group)

To make sure they get bound after you load AUCTeX, you can adjust the code you were trying earlier:

(eval-after-load "latex"
  '(progn
     (define-key LaTeX-mode-map (kbd "C-{") 'insert-begin-group)
     (define-key LaTeX-mode-map (kbd "C-}") 'insert-end-group)))
  • 2
    Actually, binding a key to a string totally works. :-) The string is treated as a keyboard macro, which usually means it is inserted plainly. – Malabarba Nov 14 '14 at 11:27
  • @Malabarba: holy moly, you're right. I never knew that. I'll update the answer so it doesn't mislead. – Dan Nov 14 '14 at 11:29
  • 1
    This is "usually" equivalent, but not if one of the character-keys is bound to a non-usual behavior. In this specific example, if TeX-electric-escape is set to t, \ is bound to TeX-electric-macro and the last newline will be used to confirm the input macro (and so not included in the buffer). insert is more robust, for code meant to be used in unspecified environments. – T. Verron Nov 14 '14 at 12:23
  • I'm sorry, I don't wanna look like a jerk, but I just cannot understand in which way your answer seems to be better than mine? You posted essentially the same answer 6 minutes later, using function definitions instead of lambdas, then you added eval-after-load part from another answer that OP showed to us. All this stuff is self-explanatory. Sorry, but this sort of behavior is strongy discourages users from answering anything. – Mark Karpov Nov 14 '14 at 12:27
  • @Mark: very sorry, that wasn't the intent. I think the issue is that these materials are not always self-explanatory to a new user, and OP seems to be relatively new (though I could be wrong about that). That's why I included the additional material explaining how to define a command and then bind it to a key. Your answer definitely works (hence, I +1-ed it when I saw it), but for a new person, it strikes me as helpful to have a bit more explanation of what's going on (e.g., telling him what the lambda is all about). – Dan Nov 14 '14 at 12:49
1

If you just want to insert \endgroup RET into buffer, try this:

(define-key LaTeX-mode-map (kbd "C-}")
  (lambda ()
    (interactive)
    (insert "\\endgroup\n")))

You also can do similar thing for C-{ and \begingroup RET.

  • @Masroor: you can read up on lambda functions to see how this answer works. (The short version: it's a function, but one without a name.) – Dan Nov 14 '14 at 11:22
1

[ I am posting an answer, as I do not have enough reputation to post a comment. Feel free to delete this answer and turn it into a comment to the question. ]

As you are on Ubuntu, you can use the command line utility xev to test what X11 spits out when you press a certain key. If the key works with xev, it should be your window manager intercepting the key press, as @Malabarba already pointed out.

  • 1
    Welcome to mx.sx! Imo, your post looks like a good base for an answer, rather than a mere comment. Note that xev is available on any linux using X.org, which should be mostly any linux desktop. – T. Verron Nov 14 '14 at 12:14

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.