Building off of the excellent answer by @KeithPinson (sorry, not enough reputation points to comment on your answer yet), this alternative will prevent the autocomplete from copying anything extra that might be inside the html tag (e.g. classes, ids, etc...) but should not be copied to the closing tag.
UPDATE I have updated my response to work with filename.html.erb
files.
I noticed my original response didn't work in files commonly used in Rails views, like some_file.html.erb
when I was using embedded ruby (e.g. <p>Year: <%= @year %><p>
). The code below will work with .html.erb
files.
inoremap ><Tab> ><Esc>?<[a-z]<CR>lyiwo</<C-r>"><Esc>O
Sample usage
Type:
<div class="foo">[Tab]
Result:
<div class="foo">
|
<div>
where |
indicates cursor position
And as an example of adding the closing tag inline instead of block style:
inoremap ><Tab> ><Esc>?<[a-z]<CR>lyiwh/[^%]><CR>la</<C-r>"><Esc>F<i
Sample usage
Type:
<div class="foo">[Tab]
Result:
<div class="foo">|<div>
where |
indicates cursor position
It's true that both of the above examples rely on >[Tab]
to signal a closing tag (meaning you would have to choose either inline or block style). Personally, I use the block-style with >[Tab]
and the inline-style with >>
.