2

I'm trying to create an abbreviations to insert a comment and to insert a TODO comment. I wrote them as following:

iab co /*__*/<Left><Left><Left>

This one produces the following (where it has three spaces)

/*__|_*/

And for the TODO comment, I wrote it like this:

iab td /*_TODO:_*/<Left><Left><Left>

This one produces

/* TODO:____|_*/

For some reason, vim adds extra spaces when interpreting the abbreviations. Could someone tell me why does it do this and how can I solve it?

Note: I replaced spaces with underscores for clarity.

4

This happens usually, because the abbreviation is triggered once you type a final space after the characters that trigger the abbreviation, e.g. after tdSpace, so that the final space gets added at the cursor position after your expanded abbreviation.

One way around it is to make use of the Eatchar function mentioned at the help (there is no actual helptag for it, you can do :helpgrep Eatchar to find it).

Here is the definition from the help together with your TODO abbreviation.

func Eatchar(pat)
   let c = nr2char(getchar(0))
   return (c =~ a:pat) ? '' : c
endfunc
iab td /* TODO: */<Left><Left><Left><c-r>=Eatchar('\s')<cr>
  • Super. You clarified it very well. For me it added more spaces because I was hitting tab after the trigger and not space. Now I modified the abbreviation so that the added space is considered in the abbreviation. Thank you – Salahuddin Ahmed Oct 23 '18 at 8:47

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.