(removed review, move some comments to discussion. Corrected 2+ to be 1 +) |
(Change <tt> to <code>, perhaps also minor tweak.) |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 3: | Line 3: | ||
|previous=1491 |
|previous=1491 |
||
|next=1498 |
|next=1498 |
||
− | |created= |
+ | |created=2007 |
|complexity=intermediate |
|complexity=intermediate |
||
|author=Max Cantor |
|author=Max Cantor |
||
− | |version= |
+ | |version=6.0 |
− | |rating= |
+ | |rating= |
|category1=Automated Text Insertion |
|category1=Automated Text Insertion |
||
|category2= |
|category2= |
||
}} |
}} |
||
− | 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. |
+ | 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: |
− | |||
− | |||
− | |||
− | Here's the function, and a mapping to go with it: |
||
<pre> |
<pre> |
||
− | " |
+ | " Wrap visual selection in an HTML tag. |
− | vmap |
+ | vmap <Leader>w <Esc>:call VisualHTMLTagWrap()<CR> |
function! VisualHTMLTagWrap() |
function! VisualHTMLTagWrap() |
||
− | let |
+ | let tag = input("Tag to wrap block: ") |
+ | if len(tag) > 0 |
||
− | let a:jumpright = 1 + len( a:tag ) " corrected from 2 to 1 DG12 10/08/08 |
||
− | normal ` |
+ | normal `> |
+ | if &selection == 'exclusive' |
||
− | let a:init_line = line( "." ) |
||
− | exe "normal i<". |
+ | exe "normal i</".tag.">" |
− | + | else |
|
⚫ | |||
− | let a:end_line = line( "." ) |
||
+ | endif |
||
− | " Don't jump if we're on a new line |
||
⚫ | |||
− | if( a:init_line == a:end_line ) |
||
⚫ | |||
− | " Jump right to compensate for the characters we've added |
||
− | + | normal `< |
|
endif |
endif |
||
⚫ | |||
endfunction |
endfunction |
||
</pre> |
</pre> |
||
+ | Put it in <code>~/.vim/scripts/wrapwithtag.vim</code> and enable in <code>~/.vimrc</code>: |
||
⚫ | |||
− | Why don't you do it the opposite way: |
||
− | |||
<pre> |
<pre> |
||
+ | au Filetype html,xml source ~/.vim/scripts/wrapwithtag.vim |
||
− | normal `> |
||
− | exe "normal a</".a:tag.">" |
||
⚫ | |||
⚫ | |||
</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 <code>:let mapleader =</code> command {{help|mapleader}}). |
||
− | Here, you don't have to care about shifting your selected area. |
||
+ | |||
⚫ | |||
+ | 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'''. |
||
---- |
---- |
||
+ | Why not use ftplugin directory instead of a Filetype autocmd? |
Latest revision as of 06:27, 13 July 2012
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).
Comments[]
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?