12

During the work with tex files with auctex+emacs (LateX-mode), I use more frequently _ (underline) instead of - (minus sign). The same as ^ instead of 6. The inconvenience is that I should press more frequently the shift-key.

It would be desirable to customize emacs in such a way that when emacs is in LateX-mode (and just in this mode), when I type - the the emacs types _ and when I type _ the emacs types -. i.e., the following key-bindings:

- gives _

_ gives -

6 gives ^

^ gives 6

Is it possible?

16

Something like this:

(add-hook 'LaTeX-mode-hook 'my-latex-hook)

(defun my-latex-hook ()
  (require 'tex-site)
  (define-key LaTeX-mode-map "-" (lambda () (interactive) (insert "_")))
  (define-key LaTeX-mode-map "_" (lambda () (interactive) (insert "-")))
  (define-key LaTeX-mode-map "6" (lambda () (interactive) (insert "^")))
  (define-key LaTeX-mode-map "^" (lambda () (interactive) (insert "6"))))
  • wonderful, your code perfectly works. – Name Nov 25 '14 at 17:34
  • 1
    (define-key LaTeX-mode-map "-" "_") and so on should be sufficient ("_" is interpreted as a keyboard macro), plus this works with TeX-electric-sub-and-superscript. – Constantine Nov 25 '14 at 17:38
  • 1
    Nope, @Constantine. What you suggest will cause recursion. – abo-abo Nov 25 '14 at 17:38
  • Oh, right. It does if _ and - are mapped to each other. – Constantine Nov 25 '14 at 17:40

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.