Wikia

Vim Tips Wiki

Changes: Wrap a visual selection in an HTML tag

Edit

Back to page

(change vim.org/scripts URL to use template + minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 31: Line 31:
 
</pre>
 
</pre>
   
Put it in <tt>~/.vim/scripts/wrapwithtag.vim</tt> and enable in <tt>~/.vimrc</tt>:
+
Put it in <code>~/.vim/scripts/wrapwithtag.vim</code> and enable in <code>~/.vimrc</code>:
 
<pre>
 
<pre>
 
au Filetype html,xml source ~/.vim/scripts/wrapwithtag.vim
 
au Filetype html,xml source ~/.vim/scripts/wrapwithtag.vim
 
</pre>
 
</pre>
   
When editing HTML file, visually highlight text and press '''{Leader} w''', enter tag name (where {Leader} defaults to the '\' key, but you can override with a <tt>:let mapleader =</tt> command {{help|mapleader}}).
+
When editing HTML file, visually highlight text and press '''{Leader} w''', enter tag name (where {Leader} defaults to the '\' key, but you can override with a <code>:let mapleader =</code> command {{help|mapleader}}).
   
 
==Comments==
 
==Comments==
Another option is to use the {{script|id=1397|text=xml.vim}} plugin. Save it as <tt>~/.vim/ftplugins/html.vim</tt>. To wrap some text with tag, visually select the text, and press '''<LocalLeader>x''' (<LocalLeader> is backslash '''\''' by default). To strip the surrounding tag, press '''<LocalLeader>d'''.
+
Another option is to use the {{script|id=1397|text=xml.vim}} plugin. Save it as <code>~/.vim/ftplugins/html.vim</code>. To wrap some text with tag, visually select the text, and press '''<LocalLeader>x''' (<LocalLeader> is backslash '''\''' by default). To strip the surrounding tag, press '''<LocalLeader>d'''.
   
 
----
 
----

Latest revision as of 06:27, July 13, 2012

Tip 1495 Printable Monobook Previous Next

created 2007 · complexity intermediate · author Max Cantor · version 6.0


There are a bunch of HTML wrapper tips and tricks out there, but I couldn't find any that suited my desire to be able to quickly wrap an arbitrary visual highlight in an arbitrary HTML tag. Here's the function, and a mapping to go with it:

" Wrap visual selection in an HTML tag.
vmap <Leader>w <Esc>:call VisualHTMLTagWrap()<CR>
function! VisualHTMLTagWrap()
  let tag = input("Tag to wrap block: ")
  if len(tag) > 0
    normal `>
    if &selection == 'exclusive'
      exe "normal i</".tag.">"
    else
      exe "normal a</".tag.">"
    endif
    normal `<
    exe "normal i<".tag.">"
    normal `<
  endif
endfunction

Put it in ~/.vim/scripts/wrapwithtag.vim and enable in ~/.vimrc:

au Filetype html,xml source ~/.vim/scripts/wrapwithtag.vim

When editing HTML file, visually highlight text and press {Leader} w, enter tag name (where {Leader} defaults to the '\' key, but you can override with a :let mapleader = command :help mapleader).

CommentsEdit

Another option is to use the xml.vim plugin. Save it as ~/.vim/ftplugins/html.vim. To wrap some text with tag, visually select the text, and press <LocalLeader>x (<LocalLeader> is backslash \ by default). To strip the surrounding tag, press <LocalLeader>d.


Why not use ftplugin directory instead of a Filetype autocmd?

Around Wikia's network

Random Wiki