Vim Tips Wiki
(Removed all comments, took the latest version from the comments and used it in the body, fixed "@b" to "--AT--b" import errors.)
m (Added a comment (and an empty rating).)
Line 1: Line 1:
  +
[[Category:Automated Text Insertion]]
 
 
{{Tip
 
{{Tip
 
|id=346
 
|id=346
Line 7: Line 7:
 
|author=fishburn--AT--sybase.com
 
|author=fishburn--AT--sybase.com
 
|version=6.0
 
|version=6.0
  +
|rating=
 
|text=
 
|text=
   
Line 80: Line 81:
 
endfunction
 
endfunction
 
}}
 
}}
  +
  +
== Comments ==
  +
  +
Could this function be expanded to replace/change tags enclosing the current region/selected text?
  +
[[User:Hansfn|Hansfn]] 11:37, 3 August 2007 (UTC)
  +
 
[[Category:Automated Text Insertion]]

Revision as of 11:37, 3 August 2007

Previous TipNext Tip

Tip: #346 - Wrap text in HTML/XML tags after prompting for the tag name

Created: October 16, 2002 8:33 Complexity: basic Author: fishburn--AT--sybase.com Version: 6.0 Karma: Imported from: Tip#346

If you have a block of text and you want to wrap it in <TAG_NAME>...</TAG_NAME> then this function will prompt you for the tag name and wrap the text.

" These mappings and TagSelection function will allow you to place 
" an XML tag around either the current word, or the current selected 
" text. 
" If the visual select is on a single line, the tag is wrapped 
" around the text <this>way</this>. If the visual select extends 
" over multiple lines, the tag is wrapped around the text 
" <this> 
" way 
" </this> 
" 
" When you are prompted for the tag name, you can enter: 
" Tag name? p class="classname" attri="bute" 
" The select is wrapped with: 
" <p class="classname" attri="bute"> 
" Your selection 
" </p> 
" Notice the attributes have been stripped from the closing tag. 
" 
" Use nmap, not nnoremap, since we do want to use an existing mapping 
nmap ,,, viw,,, 
vnoremap ,,, <Esc>:call TagSelection()<CR> 

function! TagSelection() 
 let tag = input("Tag name (include attributes)? ") 

 if strlen(tag) == 0 
 return 
 endif 

 " Save b register 
 let saveB = @b 
 " <C-R> seems to automatically reindent the line for some filetypes 
 " this will disable it until we have applied our changes 
 let saveIndent = &indentexpr 
 let curl = line(".") 
 let curc = col(".") 
 let &indentexpr =  

 " If the visual selection is over multiple lines, then place the 
 " data between the tags on newlines: 
 " <tag> 
 " data 
 " </tag> 
 let newline =  
 if getline("'>") != getline("'<") 
 let newline = "\n" 
 let curl = line("'>") 
 endif 

 " Strip off all but the first word in the tag for the end tag 
 let @b = newline . substitute( tag, '^[ \t"]*\(\<\S*\>\).*', '<\/\1>\e', "" ) 
 let curc = curc + strlen(@b) 
 exec "normal `>a\<C-R>b" 

 let @b = substitute( tag, '^[ \t"]*\(\<.*\)', '<\1>\e', "" ) . newline 
 let curc = curc + strlen(@b) 
 exec "normal `<i\<C-R>b"  

 " Now format the area 
 exec "normal `<V'>j=" 

 " Restore b register 
 let @b = saveB 
 let &indentexpr = saveIndent 

 call cursor(curl, curc) 
endfunction

Comments

Could this function be expanded to replace/change tags enclosing the current region/selected text? Hansfn 11:37, 3 August 2007 (UTC)