Bladehoarse
Bladehoarse

Reputation: 85

extra space added when going into insert mode in an abbreviation

I'm trying to create an abbreviation which replace typed text by what I want but also move the cursor and enters insert mode.

the ab is as following: :abbreviate MSG `MSG(("")); <Esc>F"i

everything works fine except when entering insert mode, I have to extra spaces before the cursor. I've tried then to add <BS><BS> but it's leading to delete the first quote. Same thing if I'm putting only one <BS> (which is really strange, it seems the second <BS> has no effect at all...)

I guess I'm missing something but I can't figure out what...

Thanks for your help !

Upvotes: 3

Views: 690

Answers (1)

Ves
Ves

Reputation: 1292

A citation from Vim help system (:help abbreviations):

An abbreviation is only recognized when you type a non-keyword character. This can also be the <Esc> that ends insert mode or the <CR> that ends a command. The non-keyword character which ends the abbreviation is inserted after the expanded abbreviation. An exception to this is the character <C-]>, which is used to expand an abbreviation without inserting any extra characters.

Example:

:ab hh       hello
       "hh<Space>" is expanded to "hello<Space>"
       "hh<C-]>" is expanded to "hello"

So if you press <Space> after entering MSG a space is inserted after expanding your abbreviation.

To avoid adding a needless space you can invoke the abbreviation with pressing <C-]> after entering MSG or you can try elaborate a mapping like this:

:inoremap MSG `MSG(("")); <C-O>F"

But IMHO such a mapping is very inconvenient.

Another option may be to use use one of the many abbreviation plugins like this (first shown by Google).

Upvotes: 4

Related Questions