0

Is there a way to make emacs expand abbreviations as soon as I finish typing them, rather than having to use a key combination, tab, or space?

For example, I want "`a" to expand to "\alpha". It would be nice if it could expand as soon as I type the "a".

I have tried abbrev-mode, but I can't figure out how to do expansions without typing a space (or other expansion character) after the abbreviation. The EmacsWiki explains how to prevent the expansion character from appearing after typing it, but that's not what I want. I also tried yasnippet, but it requires a trigger key, as far as I can tell from the documentation.

My specific motivation is to reproduce Vim-LaTeX-Suite macros, similar to this more general question asked here before.

1
  • Look at "input methods", and particularly the latex input method, for your specific example.
    – Dan
    May 31 '20 at 15:58
0

Following Dan's suggestion, I figured it out using input methods:

(quail-define-package
 "vim-LaTeX-suite" "" "v-LTX-s" t
 "Input method to reproduce vim-LaTeX-suite keybindings"
 nil t nil nil nil nil nil nil nil nil t)

(quail-define-rules
 ("`a" ["\\alpha"])
 ("`b" ["\\beta"])
)

In vim-latex-suite, typing `/ for example produces \frac{}{<++>}<++>, with the cursor inside the first set of braces. This behavior can be reproduced using the advice feature of quail-define-rules. For example:

(quail-define-package
 "vim-LaTeX-suite" "" "v-LTX-s" t
 "Input method to reproduce vim-LaTeX-suite keybindings"
  nil t nil nil nil nil nil nil nil nil t)

; Jump cursor back to the first instance of <++>, like in vim-latex-suite
(defun my/quail-vls-jumper (str)
  (interactive)
  (let
    ((len (length str))
     (jpos (cl-search "<++>" str))
     )
    (if jpos
      (progn
        (backward-char (- len jpos))
        (delete-forward-char 4)
        (evil-insert-state)
        )
    )
  )
)

(quail-define-rules
 ((advice . my/quail-vls-jumper))
 ("`a" ["\\alpha"])
 ("`b" ["\\beta"])
 ("`/" ["\\frac{<++>}{<++>}<++>"])
 ("^^" ["^{<++>}<++>"])
)

I only started emacs and lisp in the last couple weeks, so there may be various bugs and poor style. But, this solution seems to work for me.

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.