How to force Vim to reconcider abbreviations?

289 views
Skip to first unread message

Gevisz

unread,
Sep 22, 2014, 12:08:02 PM9/22/14
to vim...@googlegroups.com
I am extensively use mappings and abbreviations.

I have a lot of them and sometimes I type them
not quick enough for Vim to expand them. In this
case I should delete just typed abbreviation that
was not expanded as desired and retype it again.

Unfortunately, if I delete just typed abbreviation
just with backspace and retype it again (that is,
without exiting the input mode), Vim usually does
not expand it no matter how quick I retype it the
second time. :(

To fix that, I have to escape to the command mode
and re-enter the input mode. Then retype the
abbreviation once again.

Sometimes, Vim does not expand my abbreviation even
if I type it quite quick at the beginning. (I will
not insist on this fact but as far as I remember,
there were such cases.)

So, it would be nice to have a short key mapping,
for example with <F4>, that would force Vim to
reconsider just typed abbreviation and expand it
even it was typed in the last session.

Does anybody know how to do it?

I would like to put this mapping into
~./vim/ftplugin/ext.vim
that is where all my abbreviations defined.

Sorry, if this is already described in the documentation.

P.S. And I do not want to prolong the time during which
Vim evaluates *all* my abbreviations as it would
make it too slow.

Gevisz

unread,
Sep 22, 2014, 12:11:37 PM9/22/14
to vim...@googlegroups.com
On Mon, 22 Sep 2014 19:05:59 +0300
Gevisz <gev...@gmail.com> wrote:

> So, it would be nice to have a short key mapping,
> for example with <F4>, that would force Vim to
> reconsider just typed abbreviation and expand it
> even it was typed in the last session.

A small correction:

it would be nice to have a short key mapping,
for example with <F4>, that would force Vim to
reconsider the abbreviation just before the cursor

Ben Fritz

unread,
Sep 22, 2014, 1:37:20 PM9/22/14
to vim...@googlegroups.com

I was actually quite surprised to find that abbreviations don't work if you wait 'updatetime' milliseconds between starting and ending the abbreviation, triggering the CursorHoldI event. That could be due to specific events firing, I don't really know, but it is not documented anywhere I could find.

This mapping seems to do what you want, as a workaround:

inoremap <F4> <C-C>ciW<C-R>-<C-]>

<C-C> is used to end insert mode without triggering events or abbreviations, ciW then deletes the unexpanded abbreviation and enters insert mode, <C-R>- inserts that deleted text, and then <C-]> expands the abbreviation.

Christian Brabandt

unread,
Sep 22, 2014, 2:00:57 PM9/22/14
to vim...@googlegroups.com
On Mo, 22 Sep 2014, Gevisz wrote:

> So, it would be nice to have a short key mapping,
> for example with <F4>, that would force Vim to
> reconsider just typed abbreviation and expand it
> even it was typed in the last session.

Sounds like you are looking for Ctrl-] (:h i_CTRL-])

(Note, there were some bugs, that prevented to expand abbreviations in
Vim 7.3.X but they should have been fixed and it should work now with
7.4).

Best,
Christian
--
Jahrelang waren sie glücklich verheiratet, dann haben sie sich kennengelernt!

Ben Fritz

unread,
Sep 22, 2014, 9:54:36 PM9/22/14
to vim...@googlegroups.com
On Monday, September 22, 2014 1:00:57 PM UTC-5, Christian Brabandt wrote:
>
>
> Sounds like you are looking for Ctrl-] (:h i_CTRL-])
>
>
>
> (Note, there were some bugs, that prevented to expand abbreviations in
>
> Vim 7.3.X but they should have been fixed and it should work now with
>
> 7.4).
>
>

That was my first thought as well, but after my CursorHoldI autocmds fire, C-] does nothing at all.

I think maybe that's not supposed to make a difference.

Gevisz

unread,
Sep 23, 2014, 12:39:20 AM9/23/14
to vim...@googlegroups.com
On Mon, 22 Sep 2014 20:00:47 +0200
Christian Brabandt <cbl...@256bit.org> wrote:

> On Mo, 22 Sep 2014, Gevisz wrote:
>
> > So, it would be nice to have a short key mapping,
> > for example with <F4>, that would force Vim to
> > reconsider just typed abbreviation and expand it
> > even it was typed in the last session.
>
> Sounds like you are looking for Ctrl-] (:h i_CTRL-])

Thank you. I have made the mapping:
imap <F9> <C-]>
and it seems to work on the simple tests.
(The mapping for so short key is needed because
it eliminates the need to switch keyboard layout.)

If it refuse to work in some cases, I will remap it
to what Ben Fritz suggested.

> (Note, there were some bugs, that prevented to expand abbreviations
> in Vim 7.3.X but they should have been fixed and it should work now
> with 7.4).


I am currently using gvim 7.4.273.

Gevisz

unread,
Sep 24, 2014, 2:32:30 AM9/24/14
to vim...@googlegroups.com
Sorry but your suggestion does not work as <C-R> does not insert the
deleted text back. Moreover, my variation on your suggestion, namely
<C-C>"uciW<Esc>"upa<C-]>
does not work as well, as <C-]> do not want to expand the pasted text.

Any other suggestions?

Generally, I am very much frustrated with my abbreviation experience
in Vim because in addition to the problem just described above, some
abbreviation do not want to expand at all and I cannot see any reason
for that. (I have described that problem in another thread.)

toothpik

unread,
Sep 24, 2014, 3:20:17 AM9/24/14
to vim...@googlegroups.com
On Wed, Sep 24, 2014 at 09:30:22AM +0300, Gevisz wrote:
> On Mon, 22 Sep 2014 10:37:20 -0700 (PDT)
> Ben Fritz <fritzo...@gmail.com> wrote:
> > On Monday, September 22, 2014 11:11:37 AM UTC-5, gevisz wrote:
> > > On Mon, 22 Sep 2014 19:05:59 +0300
> > > Gevisz <gev...@gmail.com> wrote:
> > I was actually quite surprised to find that abbreviations don't work
> > if you wait 'updatetime' milliseconds between starting and ending the
> > abbreviation, triggering the CursorHoldI event. That could be due to
> > specific events firing, I don't really know, but it is not documented
> > anywhere I could find.
> >
> > This mapping seems to do what you want, as a workaround:
> >
> > inoremap <F4> <C-C>ciW<C-R>-<C-]>
> >
> > <C-C> is used to end insert mode without triggering events or
> > abbreviations, ciW then deletes the unexpanded abbreviation and
> > enters insert mode, <C-R>- inserts that deleted text, and then <C-]>
> > expands the abbreviation.

> Sorry but your suggestion does not work as <C-R> does not insert the
> deleted text back.

<C-R> isn't what inserts the text -- <C-R>- is

see

:h i_CTRL-R

if you want to understand it

Ben Fritz

unread,
Sep 24, 2014, 10:01:26 AM9/24/14
to vim...@googlegroups.com

Correct, the full suggestion was:

<C-C>ciW<C-R>-<C-]>

Note the '-' after the <C-R>, to insert the contents of the '-' register *as if typed*.

I actually tested this in a mapping and saw it working, I wasn't sure myself whether it would :-)

Your suggestion using a paste doesn't work, because abbreviations only expand on typed text, not on existing text. This is intentional, to allow you a method to actually type the text of an abbreviation when you don't want to expand it.

Gevisz

unread,
Sep 24, 2014, 12:53:59 PM9/24/14
to vim...@googlegroups.com
On Wed, 24 Sep 2014 07:01:25 -0700 (PDT)
Ben Fritz <fritzo...@gmail.com> wrote:

> On Wednesday, September 24, 2014 2:20:17 AM UTC-5, toothpik wrote:
> > On Wed, Sep 24, 2014 at 09:30:22AM +0300, Gevisz wrote:
> >
> > > Sorry but your suggestion does not work as <C-R> does not insert
> > > the deleted text back.
> >
> > <C-R> isn't what inserts the text -- <C-R>- is
>
> Correct, the full suggestion was:
>
> <C-C>ciW<C-R>-<C-]>
>
> Note the '-' after the <C-R>, to insert the contents of the '-'
> register *as if typed*.
>
> I actually tested this in a mapping and saw it working, I wasn't sure
> myself whether it would :-)
>
> Your suggestion using a paste doesn't work, because abbreviations
> only expand on typed text, not on existing text.

Yes, I have already got it. :)

And, yes, your suggestion does work *on abbreviations*,
at least on simple tests I made right now...

However, it does not work on imaps that I use in the cases
where the corresponding abbreviations never work. Even if
I change inoremap to imap...

Well, you may say: "It is not supposed to." And will be right. :(

Sorry, for unintentionally mixing those two cases together... :(

And, to separate them, I need a few more days to more carefully
look what is going on when I work with all my abbreviations.

Probably, solving the problem of never working abbreviations
described (and discussed) in another thread will be enough
(as in that case I will be able to change all my imaps to
abbreviations).

Tony Mechelynck

unread,
Sep 25, 2014, 1:10:09 AM9/25/14
to vim...@googlegroups.com
On 22/09/14 19:37, Ben Fritz wrote:
> I was actually quite surprised to find that abbreviations don't work if you wait 'updatetime' milliseconds between starting and ending the abbreviation, triggering the CursorHoldI event. That could be due to specific events firing, I don't really know, but it is not documented anywhere I could find.

What is documented is that mappings expire after 'timoutlen'
milliseconds. Abbreviations are not explicitly mentioned there, but I
imagine that they might share part of the mapping code.

- Try setting 'timeoutlen' and 'updatetime' to very different valuies so
you can easily tell which of them is relevant;
- If 'updatetime' does trigger abbreviations even when very different
from 'timeoulen', does it make a difference if you remove any CursoHoldI
autocommands that you might have?


Best regards,
Tony.
--
The only thing we learn from history is that we do not learn.
-- Earl Warren

That men do not learn very much from history is the most important of all
the lessons that history has to teach.
-- Aldous Huxley

We learn from history that we do not learn from history.
-- Georg Hegel

HISTORY: Papa Hegel he say that all we learn from history is that we learn
nothing from history. I know people who can't even learn from what happened
this morning. Hegel must have been taking the long view.
-- Chad C. Mulligan, "The Hipcrime Vocab"

Ben Fritz

unread,
Sep 25, 2014, 9:46:52 AM9/25/14
to vim...@googlegroups.com
On Thursday, September 25, 2014 12:10:09 AM UTC-5, Tony Mechelynck wrote:
> On 22/09/14 19:37, Ben Fritz wrote:
>
> > I was actually quite surprised to find that abbreviations don't work if you wait 'updatetime' milliseconds between starting and ending the abbreviation, triggering the CursorHoldI event. That could be due to specific events firing, I don't really know, but it is not documented anywhere I could find.
>
>
>
> What is documented is that mappings expire after 'timoutlen'
>
> milliseconds. Abbreviations are not explicitly mentioned there, but I
>
> imagine that they might share part of the mapping code.
>
>
>
> - Try setting 'timeoutlen' and 'updatetime' to very different valuies so
>
> you can easily tell which of them is relevant;
>
> - If 'updatetime' does trigger abbreviations even when very different
>
> from 'timeoulen', does it make a difference if you remove any CursoHoldI
>
> autocommands that you might have?
>
>

Oh, it's definitely 'updatetime' and CursorHoldI. In my case abbreviations would expand if I waited a few seconds, but if I waited longer the cursor would briefly pause in its flashing as my autocmds processed, and after that abbreviations would not expand. I checked my autocmds and found the culprit.

In my case the behavior is caused by this autocmd, which I did not remember having, and I can probably understand why it would impact abbreviations:

if exists("##CursorHoldI")
autocmd CursorHoldI * call feedkeys("\<C-G>u", "nt")
endif

The autocmd breaks the undo sequence if pausing for a long time in insert mode, since if I pause a long time in insert mode I'm probably making multiple changes. I imagine breaking the undo sequence also breaks the insert so that it no longer looks like I typed the whole abbreviation this insert session.

I'm sure there may be other autocmds or plugins which could cause similar problems; all they must do is break the undo sequence somehow.

Erik Christiansen

unread,
Oct 2, 2014, 6:21:37 AM10/2/14
to vim...@googlegroups.com
On 22.09.14 20:00, Christian Brabandt wrote:
> On Mo, 22 Sep 2014, Gevisz wrote:
>
> > So, it would be nice to have a short key mapping,
> > for example with <F4>, that would force Vim to
> > reconsider just typed abbreviation and expand it
> > even it was typed in the last session.
>
> Sounds like you are looking for Ctrl-] (:h i_CTRL-])

What sneaks up on me is a lazy iab which on occasion triggers when
I don't want it. Rather than train my fingers to a new (longer) iab, is
there anything to undo the immediately preceding iab expansion?

Backspacing to remove the expansion is fruitless, since the iab is
re-expanded as soon as the spacebar is whacked.

Unfortunately, CTRL-[ is not it, since that merely replicates <Esc>.
(Incidentally, I've mapped <Caps-Lock> to <Esc>, as a cross-application
fix for the distant and tiny <Esc> on laptops, so have no use for
CTRL-[ )

Erik

--
Investment in infrastructure accounts for much of China's GDP - the country is
said to have built the equivalent of Rome every two months in the past decade.
And with such a large pool of labour, it is harder to put the brakes on when
growth slows and supply outstrips demand. - http://www.bbc.com/news/magazine-19049254

Ben Fritz

unread,
Oct 2, 2014, 11:54:11 AM10/2/14
to vim...@googlegroups.com, dva...@internode.on.net
On Thursday, October 2, 2014 5:21:37 AM UTC-5, Erik Christiansen wrote:
> On 22.09.14 20:00, Christian Brabandt wrote:
>
> > Sounds like you are looking for Ctrl-] (:h i_CTRL-])
>
>
> Unfortunately, CTRL-[ is not it, since that merely replicates <Esc>.
>

The suggestion was CTRL-] NOT CTRL-[. See :help i_CTRL-]

But as I mentioned before, even this doesn't work after certain actions that CursorHoldI autocmds may take. I did post a workaround for that, re-inserting the text before triggering the expansion by deleting the WORD and using <C-R>- :

Christian Brabandt

unread,
Oct 2, 2014, 12:58:26 PM10/2/14
to vim...@googlegroups.com, vim...@googlegroups.com
On Do, 02 Okt 2014, Ben Fritz wrote:

> On Thursday, October 2, 2014 5:21:37 AM UTC-5, Erik Christiansen wrote:
> > On 22.09.14 20:00, Christian Brabandt wrote:
> >
> > > Sounds like you are looking for Ctrl-] (:h i_CTRL-])
> >
> >
> > Unfortunately, CTRL-[ is not it, since that merely replicates <Esc>.
> >
>
> The suggestion was CTRL-] NOT CTRL-[. See :help i_CTRL-]

But Erik asked for something like to undo the abbreviation. To the best
of my knowledge there is not (and there is even a todo list entry :h
todo and search for abbreviation).

Perhaps it would be enough to simply drop an undo point, before
expanding an abbreviation in insert mode? Something like this patch
perhaps?

diff --git a/src/getchar.c b/src/getchar.c
--- a/src/getchar.c
+++ b/src/getchar.c
@@ -4574,6 +4574,11 @@ check_abbr(c, ptr, col, mincol)
/* insert the last typed char */
(void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
}
+ if (State & INSERT)
+ {
+ u_sync(TRUE);
+ u_save(0, curbuf->b_ml.ml_line_count+1);
+ }
#ifdef FEAT_EVAL
if (mp->m_expr)
s = eval_map_expr(mp->m_str, c);


Best,
Christian
--
KNORR - Fressen mit Frust und Hiebe!

Ben Fritz

unread,
Oct 2, 2014, 2:57:47 PM10/2/14
to vim...@googlegroups.com, vim...@googlegroups.com
On Thursday, October 2, 2014 11:58:26 AM UTC-5, Christian Brabandt wrote:
>
>
> But Erik asked for something like to undo the abbreviation. To the best
>
> of my knowledge there is not (and there is even a todo list entry :h
>
> todo and search for abbreviation).
>
>
>
> Perhaps it would be enough to simply drop an undo point, before
>
> expanding an abbreviation in insert mode? Something like this patch
>
> perhaps?
>

Doesn't that prevent repeat with '.' from working properly?

Ben Fritz

unread,
Oct 2, 2014, 3:00:09 PM10/2/14
to vim...@googlegroups.com, dva...@internode.on.net
On Thursday, October 2, 2014 5:21:37 AM UTC-5, Erik Christiansen wrote:
>
>
> What sneaks up on me is a lazy iab which on occasion triggers when
>
> I don't want it. Rather than train my fingers to a new (longer) iab, is
>
> there anything to undo the immediately preceding iab expansion?
>
>
>
> Backspacing to remove the expansion is fruitless, since the iab is
>
> re-expanded as soon as the spacebar is whacked.
>
>
>
> Unfortunately, CTRL-[ is not it, since that merely replicates <Esc>.
>
> (Incidentally, I've mapped <Caps-Lock> to <Esc>, as a cross-application
>
> fix for the distant and tiny <Esc> on laptops, so have no use for
>
> CTRL-[ )
>
>

Oooooh I get it. This is a NEW question.

To avoid expanding an abbreviation, simply type <C-V> before the space. Or hit <C-C> to leave insert mode without expanding the abbreviation. Or use <C-G>u to drop an undo point and prevent the abbreviation from expanding when you continue typing.

Erik Christiansen

unread,
Oct 3, 2014, 5:46:45 AM10/3/14
to vim...@googlegroups.com
On 02.10.14 12:00, Ben Fritz wrote:
> To avoid expanding an abbreviation, simply type <C-V> before the
> space.

That does it! Nothing more needed. :-))

> Or hit <C-C> to leave insert mode without expanding the abbreviation.

Hey, that works too. The alternative syntax for that approach: <Esc>a is
a bit more finger-friendly than <C-C>a , my hands tell me.

I'll try Christian's patch later, but should grab the latest source
first - I'm only up to patch 281 on Vim 7.4.

Erik

--
"You have seven to eight million people entering the workforce in China every
single year, so you have to give them something to do in order to retain the
legitimacy of the government," says Doran. "Maybe 10 or 15 years ago they were
doing things that made sense - roads, rail, power stations etc - but now it's
investment for investment's sake." - http://www.bbc.com/news/magazine-19049254

Ben Fritz

unread,
Oct 3, 2014, 9:34:58 AM10/3/14
to vim...@googlegroups.com, dva...@internode.on.net
On Friday, October 3, 2014 4:46:45 AM UTC-5, Erik Christiansen wrote:
> > Or hit <C-C> to leave insert mode without expanding the abbreviation.
>
> Hey, that works too. The alternative syntax for that approach: <Esc>a is
> a bit more finger-friendly than <C-C>a , my hands tell me.
>

Yeah, but <Esc> actually expands abbreviations as it leaves insert mode.

Erik Christiansen

unread,
Oct 3, 2014, 9:58:38 AM10/3/14
to vim...@googlegroups.com
Rats, it does too. Funny ... I was sure I tried it and it worked. Can't
have of course. But one really good fix, plus a spare, is ample.
I'm a happy little Vegemite.

Erik

--
You can't have everything. Where would you put it?
- Steven Wright

Erik Christiansen

unread,
Oct 4, 2014, 5:26:26 AM10/4/14
to vim...@googlegroups.com, vim...@googlegroups.com
On 02.10.14 18:58, Christian Brabandt wrote:
> Perhaps it would be enough to simply drop an undo point, before
> expanding an abbreviation in insert mode? Something like this patch
> perhaps?
...

Thanks Christian. That does work.
Given Ben's neat ^V fix, I wouldn't strongly advocate the patch on the
basis of my use case, though. (Dunno if it has broader applicability.)

On 02.10.14 11:57, Ben Fritz wrote:
> Doesn't that prevent repeat with '.' from working properly?

Well, it isn't consistent with manual input behaviour. Chaining several
instances of subsequently-undone expanded abbreviations leaves a space
separated sequence of the abbreviations. Dot, however, results in
placing an expanded abbreviation, and undo undoes the _whole_ expansion,
leaving nothing.

Sorry Christian, I think I'll rebuild without the patch, now. But it did
push me to update to the current patch level. (So while I remain an old
Vimmer, I'm no longer an Old-Vimmer.)

Erik

--
The eagle may soar, but the weasel never gets sucked into a jet engine.
Reply all
Reply to author
Forward
0 new messages