0

I'd like to create an abbreviation that will change the following (in a markdown file):

- list item 1

into this:

-   list item 1

Whenever I type - f it should be expanded to - f (from 1 space to 3 spaces between the dash and the next non-white char).

This seems like an ideal situation to use abbreviations in, but help abbreviations doesn't mention regex.

If abbreviations don't accept regular expressions, could someone suggest an alternative approach? Ideally without needing a plugin.

3
  • 2
    You can't define an abbreviation with a regex. I think you have several options: 1) Use a loop to define as many abbreviations as there are combinations of - [symbol] which is not ideal because you can miss possibilities 2) Create a mapping which will run :s/- \(\w\)/- \1/ this way you can trigger the substitution easily 3) Use a snippet engine plugin, you could define - L as a snippet trigger which would allow you to replace L with your actual item – statox Jun 8 at 9:41
  • 1
    Note you cannot define abbreviations with spaces, according to :help abbreviations – D. Ben Knoble Jun 8 at 14:23
  • 2
    Also, welcome to Vi and Vim! – D. Ben Knoble Jun 8 at 14:24
1

Thanks to the comments, especially @statox.

I think the easiest way to do this is to use UltiSnips and define an automatic snippet. It's somewhat frustrating how limited vim abbreviations are, but UltiSnips really seems like a well designed and reliable plugin.

I made the following snippet in UltiSnips/markdown.snippets - replace the underscores with spaces.

snippet "^- " "markdown unordered list" rbA
-___
endsnippet

snippet "^[0-9]. " "markdown ordered list" rbA
1.__
endsnippet

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.