Abbreviations, but only within a certain file type

302 views
Skip to first unread message

Kevin Steinhardt

unread,
May 12, 2011, 9:05:23 AM5/12/11
to vim_use
Forgive my ignorance once more, but is there a way to tell vim that
I'd only like it to expand an abbreviation keyword if the file type
is .htm, .html or .mkd? I'd like to have some abbreviations for left
and right single 'book quotes' and I though that

iab ''l ‘
iab ''r ’

would speed up my writing. I've chosen to use two single quotes
followed by a position (l for left-side, r for right-side) because I
might want to use single quotes followed by either the letter 'l' or
the letter 'r' elsewhere in the same document. But still: how would
one go about telling vim "look; these may be in my .vimrc file but
only expand these when I'm in an HTML or Markdown document". Since
mistyping the above two abbreviations would be difficult (if not
impossible), it's more "I don't want HTML characters in TeX files"
than anything else. Because I write quite a lot in LaTeX and the rest
of the time in Markdown, I might develop a habit of typing ''l instead
of ` to produce an left single book quote---which would typeset
incorrectly if included in a LaTeX file.

Any help would be much appreciated and the offer of a beer is, as
always, on... well, offer.
Kevin Steinhardt

Ben Schmidt

unread,
May 12, 2011, 9:28:47 AM5/12/11
to vim...@googlegroups.com, Kevin Steinhardt
On 12/05/11 11:05 PM, Kevin Steinhardt wrote:
> Forgive my ignorance once more, but is there a way to tell vim that
> I'd only like it to expand an abbreviation keyword if the file type
> is .htm, .html or .mkd?

:help :abbreviate-local

You can combine that with a filetype plugin, e.g. put

iab <buffer> ''l &lsquo;
iab <buffer> ''r &rsquo;

in ~/.vim/ftplugin/html.vim

or make them part of autocommands in your vimrc. I would use a function
if taking the latter approach, to avoid confusing escaping. E.g.:

function MyHTMLAbbrevs()
iab <buffer> ''l &lsquo;
iab <buffer> ''r &rsquo;
endfunction
augroup MyAbbrevs
autocmd!
autocmd BufNewFile,BufRead *.htm,*.html,*.mkd call MyHTMLAbbrevs()
augroup END

Ben.

Tim Chase

unread,
May 12, 2011, 9:36:36 AM5/12/11
to vim...@googlegroups.com, Kevin Steinhardt
On 05/12/2011 08:05 AM, Kevin Steinhardt wrote:
> Forgive my ignorance once more, but is there a way to tell vim that
> I'd only like it to expand an abbreviation keyword if the file type
> is .htm, .html or .mkd? I'd like to have some abbreviations for left
> and right single 'book quotes' and I though that
>
> iab ''l&lsquo;
> iab ''r&rsquo;

Well, the help at ":help :ab" mentions the ability to use an
expression, so something like

:iab <expr> ''l &ft=~'html\?\|mkd'?'&lsquo;':''''''
:iab <expr> ''r &ft=~'html\?\|mkd'?'&rsquo;':''''''

might do the trick. Note that you need a "complete the
abbreviation" keystroke...usually this is a non-keyword character
as defined by 'iskeyword', however you can force the expansion
without adding an extra character by using control+] (which might
be helpful when quotes are involved)

As always, if you intend to do a bunch of these :iab for a
certain file-type, it might be worth wrapping them up in a
function to make it less onerous.

> Any help would be much appreciated and the offer of a beer is, as
> always, on... well, offer.

If you know how to get a good Yuengling lager from the
distributors in the north-eastern USA down to the north-Dallas
area, I'd be as interested in your technique as the beer itself
;-) Otherwise, take the cash you'd otherwise spend on a beer and
give it to Bram's charity of choice:

:help uganda

as I'm sure both Bram and the ICCF would appreciate it. :)

-tim

Reply all
Reply to author
Forward
0 new messages