Add the % on tab when working with HAML on vim

你说的曾经没有我的故事 提交于 2019-12-11 03:24:16

问题


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.



来源:https://stackoverflow.com/questions/18264368/add-the-on-tab-when-working-with-haml-on-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!