Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrapping sections with HTML tags in vim

Tags:

html

vim

plugins

I want to be able to quickly wrap entire sections of my HTML with other tags. I run into this a lot when I realize I need an outer div around my other divs, like so:

Original HTML:

<div id='a'>
   <img src='a.png'>
</div>
<div id='b'>
   <img src='b.png'>
</div>

Modified HTML:

<div id='Main'>
   <div id='a'>
      <img src='a.png'>
   </div>
   <div id='b'>
      <img src='b.png'>
   </div>
</div>

I could use matchit.vim and surround.vim but I don't believe surround.vim surrounds with words (ie. <div>), just single characters (ie. <) and it also does not indent.

The closest thing I can think of right now takes 15-20 button presses.

like image 644
puk Avatar asked Dec 07 '22 11:12

puk


2 Answers

surround.vim can surround with html tags, but it does not indent indents only when using S from visual mode, not ys and not s in visual, thanks to @RandyMorris. It also putts same-indented div’s on the next and previous lines (without indenting) if the motion to ys or visual selection used was linewise. Does not do even this for yss, so you have to use ysg@<div>j>> if you really want to avoid visual mode.

Update: there are g:surround_indent and b:surround_indent options. If you set any of them surround.vim will use = to indent surrounded text with its surroundings and the above mess with ys not indenting will be false, as well as S (S will use = as well). Requires filetype indent on and proper indenting settings.

If you don’t set these options you will see behavior described in the first paragraph: S indents unconditionally.

like image 80
ZyX Avatar answered Dec 22 '22 15:12

ZyX


Yes, surround is indeed the way to go. Supposing your cursor is on the first div:

V%j%S<div id="Main"<CR>

or

V5jS<div id="Main"<CR>

do the trick.

Depending on the context, the whole thing could even be shortened to:

Vat<div id="Main"<CR>

The <div id="Main" part seems hard to skip.

There are other ways, of course.

ZenCoding, for example lets you use CSS syntax like that:

V5j<C-y>,div#Main<CR>

I am not aware of any faster way to get what you want.

TextMate's ControlShiftw was nice, that's for sure, but it defaulted to <p> so you'd have to type div id="Main" anyway.

like image 20
romainl Avatar answered Dec 22 '22 14:12

romainl