How to make vim act like it knows it should.
March 14, 2006 4:31 PM   Subscribe

In vim, is it possible to enable end-tag completion in HTML files?

If I have <a ...> text text text typed out, I would like to type </ and have vim provide the a>, finish off the ending tag for me automatically. Further, if the cursor is on <a ...>, is there a way to automatically move to the matching <a ...>?
I've looked all over the vim site, and I can't find a good answer for this. I've checked the vim cookbook, and googled around for it, and haven't found an answer.
posted by boo_radley to Computers & Internet (18 answers total)
 
Best answer: The first one is easy; in Vim-speak, that's known as an "abbreviation". Type ":help abbreviate" to see the syntax; you probably want the ":ia {lhs} {rhs}" form.

I believe this HTML plugin handles the second case. If you don't like the way the plugin works, you can at least look at the source to see how the forward searching is handled.
posted by Galvatron at 4:50 PM on March 14, 2006


Best answer: matchit matches the closing tag when you press % (I'm not sure if that's what you meant from the question).
posted by Sharcho at 5:33 PM on March 14, 2006


Best answer: See also closetag
posted by Sharcho at 5:36 PM on March 14, 2006


Someone should really sort out how people are supposed to type code, particularly HTML, on AskMeFi. I don't know if I've ever seen it come out right the first time.

Is this something that should be on Talk instead though?
posted by AmbroseChapel at 6:07 PM on March 14, 2006


Uh, there's no magic. You have to do what you would normally do in any HTML document when you want a literal < to be shown and not interpreted as a tag: use the entity < instead of actually typing <. You should also do this for > but you can get by with just doing it for the opening tag character most of the time.
posted by Rhomboid at 7:16 PM on March 14, 2006


Fair enough, if people knew that, which they obviously don't, and anyway that's only part of it.

What happens if I want to post formatted code in with the right indenting -- you can use &nbsp;s but they will disappear on preview, and possibly on save, can you remember which?

So people use <code> and <pre> tags and get extra linebreaks added which they weren't expecting. Go back through the questions which have code and you'll see.

It's really a big mess. Is there a guide somewhere to help people post code? If there is, then point me to it by all means.
posted by AmbroseChapel at 7:24 PM on March 14, 2006


Yes, I've seen the double-newline effect of using <pre> to post code. But in this case that's not really the issue.
posted by Rhomboid at 7:40 PM on March 14, 2006


Response by poster: I don't know what happened; I used &lt; all over the place, but it came out as angled brackets in my examples.
posted by boo_radley at 7:42 PM on March 14, 2006


Just to show how PRE tags add extra lines -- normal text (no indenting, looks crap):

sub something {
if(foo){
bar
}else{
baz
}
}

inside PRE tags (indenting fine, but takes up half the page):
sub something {
  if(foo){
    bar
  }else{
    baz
  }
}

posted by AmbroseChapel at 7:44 PM on March 14, 2006


I don't know what happened; I used < all over the place, but it came out as angled brackets in my examples.

Did you preview? I'm confused as to what actually happens when posting. Perhaps entities get converted to their characters on save, or on preview?

I didn't mean to diss you by the way, I could tell your intentions were good!
posted by AmbroseChapel at 7:45 PM on March 14, 2006


I recommended an abbreviation to automatically create matching closing tags. The only problem with that is that it leaves the cursor after the abbreviation, which is not the most useful location. So you might be happier with a "map" that inserts the desired characters and then moves the cursor back to the desired location:

:imap <tt> <tt></tt><Esc>hhhhi

The real reason people have a problem posting HTML is that MeFi's preview function actually changes the character entities inside the comment box. If you preview and then immediately post, you end up inserting literal characters instead of entities.
posted by Galvatron at 7:47 PM on March 14, 2006


I think you can avoid the double-newline by using <br> inside the <pre> instead of depending on the mefi server to convert NLs:
sub something {    if (foo) {        bar    } else {        baz    }}

posted by Rhomboid at 7:51 PM on March 14, 2006


Response by poster: Of course I previewed. And I think that's the issue; my entities got converted on preview.
posted by boo_radley at 8:32 PM on March 14, 2006


Response by poster: Let us continue discussing metafilter's broken preview in this handy meta thread.
posted by boo_radley at 8:36 PM on March 14, 2006


Response by poster: Oh, and Sharcho's answer about matchit matched the first part of my question. I had to enable ftplugins, and then found out that the < and > aren't considered part of the tag in matchit like I thought they would be. Closetag is very close to answering the second part of my question. Its default keystroke is CTRL+_, so I might be able to noodle around with that to make it something more natural

Galvatron's suggestion about abbreviations was good, but would only be applicable to one type of tag.

Thanks, all.
posted by boo_radley at 8:56 PM on March 14, 2006


Response by poster: I also found this, which claims to implement Intellisense for C# in vim. This isn't related at all to what I asked, but the idea of real live intellisense in vim dazzles and amuses me.
posted by boo_radley at 9:55 PM on March 14, 2006


Best answer: I think you can tweak matchit work even on < and>, take a look at the provided matchit.txt . In the default mode you have to move the cursor to the right of < to make it work.
posted by Sharcho at 2:15 AM on March 15, 2006


Response by poster: Thank you! I made the following change to closetag.vim to get the exact behavior I wanted:
inoremap <lt>/ <C-R>=GetCloseTag()<CR>
posted by boo_radley at 7:12 AM on March 15, 2006


« Older Network speed varies from PC to PC--why?   |   How do I convince my current landlord to cancel my... Newer »
This thread is closed to new comments.