Vim Tips Wiki
Nummer5 (talk | contribs)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1495
 
|id=1495
  +
|previous=1491
|title=Wrap a Visual hilight in an arbitrary HTML tag
 
  +
|next=1498
|created=January 29, 2007 16:00
+
|created=January 29, 2007
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Max Cantor
 
|author=Max Cantor
 
|version=5.7
 
|version=5.7
 
|rating=12/6
 
|rating=12/6
|text=
 
 
}}
 
}}
 
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.
   
 
I haven't tested the function below thoroughly, but it works in every visual mode, even if you're doing per-character visual mode and end your selection on a different line. I like to make all of my HTML tags uppercase, but if you don't bother, you can change the line:
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 hilight in an arbitrary HTML tag.
 
 
I haven't tested the function below TOO thoroughly, but it works in every Visual mode, even if you're doing per-character Visual mode and end your selection on a different line. I like to make all of my HTML tags uppercase, but if you don't bother, you can change the line:
 
   
 
<pre>let a:tag = toupper( input( "Tag to wrap block: ") )</pre>
 
<pre>let a:tag = toupper( input( "Tag to wrap block: ") )</pre>
   
to...
+
to
 
 
<pre>let a:tag = input( "Tag to wrap block: ")</pre>
 
<pre>let a:tag = input( "Tag to wrap block: ")</pre>
   
Line 24: Line 22:
   
 
Here's the function, and a mapping to go with it:
 
Here's the function, and a mapping to go with it:
 
 
<pre>
 
<pre>
" Wraps visual selection in an HTML tag
+
" Wraps visual selection in an HTML tag
vmap ,w &lt;ESC&gt;:call VisualHTMLTagWrap()&lt;CR&gt;
+
vmap ,w &lt;ESC&gt;:call VisualHTMLTagWrap()&lt;CR&gt;
function! VisualHTMLTagWrap()
+
function! VisualHTMLTagWrap()
let a:tag = toupper( input( "Tag to wrap block: ") )
+
let a:tag = toupper( input( "Tag to wrap block: ") )
let a:jumpright = 2 + len( a:tag )
+
let a:jumpright = 2 + len( a:tag )
normal `&lt;
+
normal `&lt;
let a:init_line = line( "." )
+
let a:init_line = line( "." )
exe "normal i&lt;".a:tag."&gt;"
+
exe "normal i&lt;".a:tag."&gt;"
normal `&gt;
+
normal `&gt;
let a:end_line = line( "." )
+
let a:end_line = line( "." )
" Don't jump if we're on a new line
+
" Don't jump if we're on a new line
if( a:init_line == a:end_line )
+
if( a:init_line == a:end_line )
" Jump right to compensate for the characters we've added
+
" Jump right to compensate for the characters we've added
exe "normal ".a:jumpright."l"
+
exe "normal ".a:jumpright."l"
endif
+
endif
exe "normal a&lt;/".a:tag."&gt;"
+
exe "normal a&lt;/".a:tag."&gt;"
endfunction
+
endfunction
 
</pre>
 
</pre>
   
Line 49: Line 46:
   
 
<pre>
 
<pre>
normal `&gt;
+
normal `&gt;
exe "normal a&lt;/".a:tag."&gt;"
+
exe "normal a&lt;/".a:tag."&gt;"
normal `&lt;
+
normal `&lt;
exe "normal i&lt;".a:tag."&gt;"
+
exe "normal i&lt;".a:tag."&gt;"
 
</pre>
 
</pre>
   
Line 58: Line 55:
   
 
===Tags case===
 
===Tags case===
Since XHTML defines all tags and attributes to be in ''lower''case, I'd suggest to follow this convention in HTML as well. --[[User:Nummer5|Nummer5]] 13:58, 18 December 2007 (UTC)
+
Since XHTML defines all tags and attributes to be in ''lower''case, I'd suggest to follow this convention in HTML as well.
   
 
----
 
----
 
 
 
[[Category:Automated Text Insertion]]
 
[[Category:Automated Text Insertion]]

Revision as of 22:25, 1 January 2008

Tip 1495 Printable Monobook Previous Next

created January 29, 2007 · complexity intermediate · author Max Cantor · version 5.7


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.

I haven't tested the function below thoroughly, but it works in every visual mode, even if you're doing per-character visual mode and end your selection on a different line. I like to make all of my HTML tags uppercase, but if you don't bother, you can change the line:

let a:tag = toupper( input( "Tag to wrap block: ") )

to

let a:tag = input( "Tag to wrap block: ")

There may be a quicker way to input the arbitrary tag than with an input() call, but I kind of like this one.

Here's the function, and a mapping to go with it:

" Wraps visual selection in an HTML tag
vmap ,w <ESC>:call VisualHTMLTagWrap()<CR>
function! VisualHTMLTagWrap()
  let a:tag = toupper( input( "Tag to wrap block: ") )
  let a:jumpright = 2 + len( a:tag )
  normal `<
  let a:init_line = line( "." )
  exe "normal i<".a:tag.">"
  normal `>
  let a:end_line = line( "." )
  " 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
    exe "normal ".a:jumpright."l"
  endif
  exe "normal a</".a:tag.">"
endfunction

Comments

Why don't you do it the opposite way:

normal `>
exe "normal a</".a:tag.">"
normal `<
exe "normal i<".a:tag.">"

Here, you don't have to care about shifting your selected area.

Tags case

Since XHTML defines all tags and attributes to be in lowercase, I'd suggest to follow this convention in HTML as well.