Using Vim as an HTML editor

28,554

Solution 1

I do all of my HTML editing in vim. The three plugins I find most helpful for editing HTML and XML in vim are matchit, surround, and allml.

Matchit will allow you to jump to the start/end tag with '%'. Surround allows you to easily add, delete, and change the surrounding tags. Allml provides you with a great set of mappings for editing (X)HTML and XML.

Solution 2

wrap selected text with tags:

    function! VisualTagsWrap()
        if !exists('g:tags_to_wrap')
            let g:tags_to_wrap=[]
        endif
        let g:tags_to_wrap=split(input('space separated tags to wrap block: ', join(g:tags_to_wrap, ' ')), '\s\+')
        if len(g:tags_to_wrap)>0
            execute 'normal! `>a</'.join(reverse(g:tags_to_wrap), '></').'>'
            execute 'normal! `<i<'.join(reverse(g:tags_to_wrap), '><').'>'
        endif
    endfunction


vnoremap <silent>,w <ESC>:call VisualTagsWrap()<CR>

highlight closing bracket for tags:

set matchpairs+=<:>

dummy text (type "lorem" in insert mode):

inoreabbrev lorem Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

Solution 3

To match tags:

Have a look at matchit plugin. Vim 6 onwards matchit.vim is packaged with standard distribution. To install matchit, read :help matchit-install.

Make sure filetype plugin on is in vimrc.

Once installed, use % to match the start/end tag. :help matchit-intro for more help.

Solution 4

In Fedora 15 matchit.vim comes in the vim-common package. Then add this to your ~/.vimrc to get it working.

source /usr/share/vim/vim73/macros/matchit.vim

Then just press Shift+5 (aka % ) to jump to the matching tag.

When editing an html file without the .html suffix, use the command:

:set filetype=html

to activate the matchit macro.

Share:
28,554
Admin
Author by

Admin

Updated on January 23, 2022

Comments

  • Admin
    Admin over 2 years

    You know how Notepad++ has this feature that when you click on a tag (say ) it automatically highlights the ending tag () as well? What's it called? And how do you tweak Vim to have this feature as well?

    And any more ways you can turn Vim into a powerful and efficient HTML editor?

  • Admin
    Admin over 14 years
    Hi so where do these code go? Sorry but I'm a noob VIM-er so you have to put it really clearly! :(
  • Admin
    Admin over 14 years
    Really? It's not inside, I'm running 7.2, I gotta install it seperately.
  • shoban
    shoban over 14 years
    Yes. At least it was the case with vim 7.2 which came with Cygwin and Ubuntu Jaunty.
  • Admin
    Admin over 14 years
    put that code into your .vimrc. use ,w in select mode to wrap text, use % to switch between tag's brackets
  • asfallows
    asfallows almost 12 years
    Just added this to my vimrc today. I like it. Now all I need is something like vi< that visual selects in between a pair of tags.
  • zhon
    zhon over 11 years
    allml has been renamed to ragtag
  • Jay
    Jay over 8 years
    @asfallows use cit for change in tag, for example haha (vit also works)