Saturday, December 01, 2012

[link] Wrap a visual selection in an HTML tag

Wrap a visual selection in an HTML tag.

Pretty useful function. I have only slightly modified it to take the tag as parameter and insert the tag on the line before/after selection. And hooked it on a keyboard shortcut.

[ The '^M' below should be converted there into real ^M (typed as ^V^M). ]

" Wrap visual selection in an HTML tag.
vmap <C-q> <Esc>:call VisualHTMLTagWrap('cite')<CR>
vmap <C-T> <Esc>:call VisualHTMLTagWrap('title')<CR>
function! VisualHTMLTagWrap(tag)
 normal `>
 if &selection == 'exclusive'
  exe "normal i^M</".a:tag.">"
  exe "normal a^M</".a:tag.">"
 normal `<
 exe "normal i<".a:tag.">^M"
 normal `>
 normal j


Nikola Valentinov Petrov said...

You can try the suround plugin from tpope it will actually do what you want and a lot more.

The only downside is that it is far more code and is not installed on every machine. Oh well...

Best, Nikola

Ihar Filipau said...

There is a plug-in for everything.

But the beauty of the VIM is that very often one can code what's needed in much shorter time than: searching for the plugin, selecting from all the options best suited one, installing it, learning how to use it, use it.

When possible, I prefer to keep things simple. (Plugins are complicated by definition IMO.) And without trying it with own hands, one would never really know what simple and what's not.

Nikola Valentinov Petrov said...

I totally agree. The thing is that if you are doing some html/xml and I am almost every day - you should consider investing in learning the plugins for that. This one is totally recommended - I am using it for other things like wiki syntax and what not.

Otherwise you are totally right - I prefer to not have plugins that are > 100 lines with some exceptions of course. I find them scary...

Best, Nikola