vim html tag 自动关闭,Auto closing an HTML tag

When typing an html document, you can automatically close html tags using omni completion. For example, you may type

example then press Ctrl-x Ctrl-o to automatically finish typing the tag so the text reads example.

With the following abbreviation in your vimrc, you can simplify this process:

:iabbrev

With this abbreviation, you can simply type then press Space to automatically complete the tag.

You may find that sometimes you want to type without invoking the abbreviation. If that happens a lot, you may prefer the following abbreviation so you have to enter / to invoke tag completion:

:iabbrev /

Also you can remap Ctrl-x Ctrl-o to Ctrl-Space using:

:imap

This autocmd and insert mode mappings will cause > to auto-complete the tag in insert mode, > to auto-complete and put your cursor on an indented new line, and > to cancel autocompleteion for those special times.

function s:CompleteTags()

inoremap > >:startinsert!?

inoremap > >

inoremap > >:startinsert!?$

endfunction

autocmd BufRead,BufNewFile *.html,*.js,*.xml call s:CompleteTags()

I suspect the abbreviation is not very helpful in practice because it won't work if you type, for example, abc (it only works if you enter insert mode to type the abbreviation, or if the abbreviation follows whitespace. Perhaps a mapping like:inoremap

might be more helpful? You would press F8 to add a closing tag while in insert mode. JohnBeckett 10:25, April 26, 2010 (UTC)

Hmm, but it would work if you end your tag on a new line by itself. I agree this is not very useful though for the reason you give. To stay more in the spirit of the original, perhaps :inoremap / would be better? This would always fire when closing a tag of course, which could be annoying to some. --Fritzophrenic 13:54, April 26, 2010 (UTC)

Use instead to avoid conflicts (learn more about Leader key in ":help leader")--Devgutt28 (talk) 15:11, August 2, 2012 (UTC)

I know I'm a bit late to the party, but I added on to Fritzophrenic's version with :inoremap / ==gi so the new tag properly indents itself. == re-indents the current line (also moving the cursor), and gi returns the cursor to its location before the re-indent and enters insert mode. (September 2, 2017)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值