3

Q: is there a way to rotate between multiple expansions bound to the same abbrev?

I'm experimenting with abbrev-mode, and I have some abbreviations that I'd like to use for different expansions of the same abbrev.

For example, I often use the abbreviation "st" for "sometimes", but occasionally use it to mean "such that". I'd like to know if there's a way to bind the "st" abbrev to produce "sometimes" as its default expansion, but allow me to select "such that" as a secondary expansion.

Sound goofy? Here's how it's working in my mind. Let's say I've got a hypothetical command expand-abbrev-and-rotate bound to the tab key and * representing point:

  1. some text st*tab => some text sometimes*
  2. some text sometimes*tab => some text such that*
  3. some text such that*tab => some text sometimes*
  4. ... and so forth, rotating among a short list of possible expansions (2 in this case)

Does abbrev-mode have anything that could approximate this behavior? I've been peeking at the internals and can imagine a way to implement it, but wouldn't want to reinvent the wheel.

  • I haven't used abbrev, but from what I read, isn't the behavior of Dynamic Abbrev what you want; continuously hitting M-/ to cycle through the abbrev expansions? – Kaushal Modi Feb 19 '15 at 0:19
  • @kaushalmodi: I, in turn, haven't used dabbrev, but it doesn't look like it. My understanding of dabbrev is that it scans previous text in the buffer to create the abbrev options, whereas I'm looking to set the options for a particular abbrev, irrespective of prior text, in a set order. – Dan Feb 19 '15 at 0:24
  • Exactly, dabbrev will propose you expansions based in previous inserted text in the buffer. Not what we're looking for. Yes, I'll be interested by the behaviour you describe Dan. – Nsukami _ Feb 19 '15 at 1:13
  • @Dan If you do end up implementing it manually, you might want to look into how hippie-expand achieves this exact effect. – Malabarba Feb 26 '15 at 22:50
  • @Malabarba: thanks for the suggestion -- I'll look more closely into hippie-expand as well. – Dan Feb 26 '15 at 22:51
2

Yasnippet can do what you want. Either a shortcut key-binding or a prefix substring can expand to a particular snippet. And each snippet can present a list of options via yas-choose-value.

Here is an example snippet based on your example:

-- mode: snippet; require-final-newline: nil --

name: st

key: st

binding: direct-keybinding

--

${1:$$(yas-choose-value '("sometimes" "such that"))}

Now I can type "st" and hit the yas-trigger-key (defaults to TAB but I have set mine to SHIFT-TAB), and I am prompted for a selection between the 2 possibilities.

In my .init.el file I have the following:

(setq yas-prompt-functions '( yas-ido-prompt yas-x-prompt

which by default uses ido as the selection method. If you are on x-windows, you can use the x-prompt for a nice graphical popup. There are other selection methods as well.

  • It might be helpful to provide an example here of a snippt that presents a set of options. – glucas Feb 26 '15 at 18:10

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.