Add the % on tab when working with HAML on vim

2019-07-18 22:39发布

问题:

I'd like to a the ability to type h3 tab and have it output %h3 when working with haml. This would also work with other selectors such as p or section.

I've installed Tim Pope's vim-haml plugin, but it doesn't seem to provide a shortcut. Have I missed something?

回答1:

Two possible solution come to my head:

1) Hardcode everything using insert mode abbreviations (aka the easy way):

function! s:haml_settings()
    iabbrev h3      %h3
    iabbrev p       %p
    iabbrev section %section
endfunction

autocmd FileType haml call <sid>haml_settings()

Although this way would use h3<space> instead of h3<tab>.

More information: :help abbreviations, especially :help :iabbrev

2) Remap the tab key (the more advanced way):

Remap tab to first execute a certain function, that checks the word under cursor and transforms the text if it found something it can work with, or use the standard behaviour of <tab> otherwise.

More information: :help map-expression



回答2:

A slightly more powerful alternative to abbreviations is to use a snippet-expansion plugin. SnipMate and UltiSnips being the most obvious choices but there are others.

The two plugins above can use the same snippets. There are already some for haml and it's extremely easy to add yours.



标签: vim haml