Skip to content

Instantly share code, notes, and snippets.

@kosei27
Forked from hokaccha/endtagcomment.vim
Created December 9, 2010 07:35
Show Gist options
  • Select an option

  • Save kosei27/734448 to your computer and use it in GitHub Desktop.

Select an option

Save kosei27/734448 to your computer and use it in GitHub Desktop.
" こういうHTMLがあったときに
" <div id="hoge" class="fuga">
" ...
" </div>
"
" 実行するとこうなる
" <div id="hoge" class="fuga">
" ...
" <!-- /div#hoge.fuga --></div>
function! Endtagcomment()
let reg_save = @@
try
silent normal vaty
catch
execute "normal \<Esc>"
echo 'no match html tags'
return
endtry
let html = @@
let start_tag = matchstr(html, '\v(\<.{-}\>)')
let tag_name = matchstr(start_tag, '\v([a-zA-Z]+)')
let id = ''
let id_match = matchlist(start_tag, '\vid\=["'']([^"'']+)["'']')
if exists('id_match[1]')
let id = '#' . id_m[1]
endif
let class = ''
let class_match = matchlist(start_tag, '\vclass\=["'']([^"'']+)["'']')
if exists('class_match[1]')
let class = '.' . join(split(class_m[1], '\v\s+'), '.')
endif
execute "normal `]va<\<Esc>`<"
let @@ = printf('<!-- /%s%s%s -->', tag_name, id, class)
normal ""P
let @@ = reg_save
endfunction
nnoremap ,t :<C-u>call Endtagcomment()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment