how to abbreviate multiple words

8 views
Skip to first unread message

Brian Golding

unread,
Jan 6, 2010, 12:03:09 PM1/6/10
to vim_use
Is it possible to abbreviate multiple words. For example to enter a
command such as

ab "by using" using

so that anytime the words "by using" are entered, they will be
replaced by just "using"?

Thanks
Brian Golding

Ben Fritz

unread,
Jan 6, 2010, 7:32:39 PM1/6/10
to vim_use

Nope.

:help abbreviations

Tim Chase

unread,
Jan 6, 2010, 8:10:36 PM1/6/10
to vim...@googlegroups.com

Though Ben has already given you the "right" answer that
abbreviations can't include spaces on the LHS, you might be able
to hack it with a horrible mapping:

:inoremap by<space>using using

This has the unfortunate side-effect that it thinks you're
entering a mapping (and doesn't show your typed text) until
you've either typed the whole thing, or you've disambiguated what
you want to type. With the above, try entering

This is done by using taxes and by usury.

you get the "This is done " portion, and then the "by usin"
flickers as you type it, until you get to the "g", at which point
it "expands" the mapping to just "using", and then when you start
typing the "by usury", you get the "by us" flickering until the
"u" disambiguates and you see the "by usu".

As stated: an ugly hack.

When I need to flag words/phrases I prefer not to use (in my
"critiqued" works, I tend to avoid forms of "to be"), I use a
:match command like

:match Error /\<\(be\|am\|is\|are\|was\|were\|...\)\>/

so they jump out at me and I can rephrase them. So in your case,
I'd use

:match Error /\<\(by using\|by \w\+ing\|...\)\>/

with any other phrases you want to revise.

-tim

Matt Wozniski

unread,
Jan 6, 2010, 8:15:32 PM1/6/10
to vim...@googlegroups.com
On Wed, Jan 6, 2010 at 8:10 PM, Tim Chase wrote:
> Brian Golding wrote:
>>
>> Is it possible to abbreviate multiple words.   For example to enter a
>> command such as
>>
>> ab "by using" using
>>
>> so that anytime the words "by using" are entered, they will be
>> replaced by just "using"?
>
> Though Ben has already given you the "right" answer that abbreviations can't
> include spaces on the LHS, you might be able to hack it with a horrible
> mapping:
>
>  :inoremap by<space>using using
>
> This has the unfortunate side-effect that it thinks you're entering a
> mapping (and doesn't show your typed text) until you've either typed the
> whole thing, or you've disambiguated what you want to type.  With the above,
> try entering

Speaking of horrible hacks, I think this one is conceptually worse,
but it doesn't have most of the drawbacks of using a map.

iabbr <expr> using (search('by\_s*using\%#', 'bcW') == 0 ? "using" :
"\<Esc>bcwusing\<Esc>ls")

(That shouild have been all one line, if it was wrapped.) There may
be problems with this that I haven't found, of course, but it seems to
work for my test cases. Not that I'd recommend it, anyway... :-)

~Matt

Tim Chase

unread,
Jan 6, 2010, 10:02:29 PM1/6/10
to vim...@googlegroups.com
Matt Wozniski wrote:
> On Wed, Jan 6, 2010 at 8:10 PM, Tim Chase wrote:
>> Brian Golding wrote:
>>> Is it possible to abbreviate multiple words. For example to enter a
>>> command such as
>>>
>>> ab "by using" using
>>>
>>> so that anytime the words "by using" are entered, they will be
>>> replaced by just "using"?
>>
>> :inoremap by<space>using using
>
> Speaking of horrible hacks, I think this one is conceptually worse,
> but it doesn't have most of the drawbacks of using a map.
>
> iabbr <expr> using (search('by\_s*using\%#', 'bcW') == 0 ? "using" :
> "\<Esc>bcwusing\<Esc>ls")

<shudders>
Like obfuscated C contests, that mapping is art, but a bit
impenetrable like modern art. :)

-tim


Andy Wokula

unread,
Jan 7, 2010, 4:46:01 AM1/7/10
to vim...@googlegroups.com

You can check out Kana's ctxabbr plugin (actually an autoload script):
http://vim.sf.net/scripts/script.php?script_id=2514

The script serves a slightly different purpose, but the following works
for me:

:call ctxabbr#define('using', "\<C-W>using", '<by')

This defines a "context sensitive" abbreviation. It works like

:inoreabbrev using <C-W>using

but expands only when "by" is found to the left.

--
Andy

Ben Fritz

unread,
Jan 7, 2010, 11:20:37 AM1/7/10
to vim_use

On Jan 7, 3:46 am, Andy Wokula <anw...@yahoo.de> wrote:
> You can check out Kana's ctxabbr plugin (actually an autoload script):http://vim.sf.net/scripts/script.php?script_id=2514
>
> The script serves a slightly different purpose, but the following works
> for me:
>
>      :call ctxabbr#define('using', "\<C-W>using", '<by')
>
> This defines a "context sensitive" abbreviation.  It works like
>
>      :inoreabbrev using <C-W>using
>
> but expands only when "by" is found to the left.
>

Very nice solution! Shouldn't there be two \<C-W> commands, though, to
also remove the word "by"?

Reply all
Reply to author
Forward
0 new messages