When typing a report, we have a large list of acronyms defined in Acronyms.tex. In order to use it, we have to encapsulate an acronym such as KDF with the acronym command: \ac{KDF}. I often forget and find it rather cumbersome to type it every time.

I'm pretty sure I've seen auto-replacing in Vim, without using :%s/x/y/ manually. It may have been a plugin or it may have been supported natively, I don't know.

  • I can't find it right now but I am fairly certain that we already have a duplicate of this question.
    – statox
    Aug 1 '19 at 12:07
  • @statox Well, I'm happy to hear I'm not the only one who couldn't find it :)
    – Luc
    Aug 1 '19 at 12:28
  • Indeed :) I was just commenting to have a reminder to search it when I'll have more time, or to incite one of our long time user to link it if they find it more easily
    – statox
    Aug 1 '19 at 12:30

I just noticed a disadvantage

That's because you're trying to remap a command. While "auto-replacements" in Vim are supposed to be done with abbreviations (see :help abbreviations).

You can create an abbreviation like that:

:inoreabbrev KDF \ac{KDF}

Unlike commands, the abbreviations are typed as usual. And the substitution takes place when Vim knows that KDF makes a whole word, not a part of it (e.g. KDF<space> ---> \ac{KDF}<space>).

  • This doesn't seem to work with hyphenations or slashes, such as in CSMA/CD, SHA-1 or Wi-Fi. Do you have an idea how to solve that? Maybe I should use inoremap for those, though a lot of letters would then become 'dead' again.
    – Luc
    Aug 1 '19 at 11:44
  • 1
    @Luc see the help: some keys cannot be used
    – D. Ben Knoble
    Aug 1 '19 at 12:07
  • 2
    @Luc You can play with iskeyword option, like set isk+=45,47 to allow your examples. But that could have some consequences, and, in general, cannot be recommended, unless you understand what you're doing.
    – Matt
    Aug 1 '19 at 12:12
  • 1
    @Luc Unless there are both acronyms with and without dashes and slashes you can define your abbreviations without them and put them into the expanded text, e.g., :inoreabbrev CSMACD \ac{CSMA/CD}or :inoreabbrev SHA1 \ac{SHA-1}. Aug 2 '19 at 8:14

After some searching, it turns out that single character replacements work just as well for words. The answer is:

:inoremap KDF \ac{KDF}

As the help text says: "Map the key sequence {lhs} to {rhs}".

When typing the specified word ("lhs") in insert mode, it will automatically be replaced (by the "rhs"). For example, when simply typing When using a KDF for, it will automatically be replaced and result in: When using a \ac{KDF} for.

Beware that this also works in replace mode, e.g. when your cursor is on the beginning of a line of text starting with KEF testtest, typing RKDF will not turn the line into KDF testtest or \ac{KDF} testtest, but into \ac{KDF}test instead (since you are in replace mode, it will overwrite the following characters).

And beware that fixing a single character will not trigger the replacement. It maps the key sequence, not the word. Replacing the "E" in "KEF" using lrD (turning it into "KDF") does not trigger the replacement.

Edit: I just noticed a disadvantage: any uppercase characters will now wait for the next character. If I defined inoremaps for KDF and ABC, then typing an A or a K in insert mode will wait for the next character (with a 1-second timeout) before moving the cursor ahead. If I type KDF too slowly (1 second between each character), it is not replaced. Neither behaviour is impossible to work with, but neither is desirable, either. If anyone has a better solution, please post it as an answer to the question so I can accept that as answer instead!

  • 2
    You need inoreabbrev as suggested by @Matt. You may also be interested in this article.
    – jdhao
    Aug 1 '19 at 11:34

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.