tags:

views:

4140

answers:

6

It's been a while since I've had to do any html-like code in vim, but recently I came across this again. Say I'm doing a simple bit of a html page:

<html><head><title>This is a title</title></head></html>

How do I write those closing tags for title, head and html down quickly? I feel like I'm missing some really simple way here that does not involve me going through writing them all down one by one. Of course I can use ^P to autocomplete the individual tag names but what gets me on my laptop keyboard is actually getting the brackets and slash right.

+7  A: 

Check this out..

http://vim.sourceforge.net/scripts/script.php?script_id=13

I use something similar.

Ian P
I use this too. It's excellent. It's also also a simple way to check that all your tags nest correctly
Rory
A: 

Here are some useful functions (including a close tags function) for working with HTML in Vim:

http://www.stripey.com/vim/html.html

Ben Hoffstein
+14  A: 

I find using the xmledit plugin pretty useful. it adds two pieces of functionality:

  1. When you open a tag (e.g. type <p>), it expands the tag as soon as you type the closing > into <p></p> and places the cursor inside the tag in insert mode.
  2. If you then immediately type another > (e.g. you type <p>>), it expands that into

    <p>
         
    </p>
    

    and places the cursor inside the tag, indented once, in insert mode.

Of course, you don't have to worry about closing tags at all if you write your HTML content in Markdown and use %! to filter your Vim buffer through the Markdown processor of your choice :)

hakamadare
+3  A: 

I find it more convinient to make vim write both opening and closing tag for me, instead of just the closing one. You can use excellent allml plugin by Tim Pope. Usage looks like this (let | mark cursor position) you type:

span|

press CTRL+x SPACE

and you get

<span>|</span>

You can also use CTRL+x ENTER instead of CTRL+x SPACE, and you get

<span>
|
</span>

Allml can do more than just it (eg. insert <%= stuff around this %> or DOCTYPE). You probably want to check out other plugins by author of allml, especially surround.

Krzysiek Goj
+6  A: 

I like minimal things,

imap ,/ </<C-X><C-O>
dysmsyd
very minimal :)
meder
+1  A: 

allml (now Ragtag ) and Omni-completion ( <C-X><C-O> ) doesn't work in a file like .py or .java.

if you want to close tag automatically in those file, you can map like this.

imap <C-j> <ESC>F<lyt>$a</^R">

( ^R is Contrl+R : you can type like this Control+v and then Control+r )

(| is cursor position ) now if you type..

<p>abcde|

and type ^j

then it close the tag like this..

<p>abcde</p>|

kimilhee