Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Remove html character entities)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{deprecated|This tip is deprecated by the surround plugin.}}
 
{{review}}
 
{{review}}
 
{{TipImported
 
{{TipImported
Line 9: Line 10:
 
|version=6.0
 
|version=6.0
 
|rating=3/7
 
|rating=3/7
  +
|category1=
  +
|category2=
 
}}
 
}}
 
Inspired by [[VimTip987]] I wrote a small function to surround selected text in visual-mode with text.
 
Inspired by [[VimTip987]] I wrote a small function to surround selected text in visual-mode with text.
Line 16: Line 19:
 
<pre>
 
<pre>
 
before: bla bla Selected Text bla bla
 
before: bla bla Selected Text bla bla
:'&lt;,'&gt;call Surround('"', '"')&lt;CR&gt;
+
:'<,'>call Surround('"', '"')<CR>
 
after : bla bla "Selected Text" bla bla
 
after : bla bla "Selected Text" bla bla
 
</pre>
 
</pre>
Line 22: Line 25:
 
<pre>
 
<pre>
 
fun! Surround(s1, s2) range
 
fun! Surround(s1, s2) range
exe "normal vgvmboma\&lt;ESC&gt;"
+
exe "normal vgvmboma\<Esc>"
 
normal `a
 
normal `a
 
let lineA = line(".")
 
let lineA = line(".")
Line 30: Line 33:
 
let columnB = col(".")
 
let columnB = col(".")
 
" exchange marks
 
" exchange marks
if lineA &gt; lineB || lineA &lt;= lineB &amp;&amp; columnA &gt; columnB
+
if lineA > lineB || lineA <= lineB && columnA > columnB
 
" save b in c
 
" save b in c
 
normal mc
 
normal mc
Line 38: Line 41:
 
normal `cma
 
normal `cma
 
endif
 
endif
exe "normal `ba" . a:s2 . "\&lt;ESC&gt;`ai" . a:s1 . "\&lt;ESC&gt;"
+
exe "normal `ba" . a:s2 . "\<Esc>`ai" . a:s1 . "\<Esc>"
 
endfun
 
endfun
 
</pre>
 
</pre>
Line 47: Line 50:
 
before: bla bla Selec
 
before: bla bla Selec
 
ted Text bla bla
 
ted Text bla bla
:'&lt;,'&gt;call Surround('"', '"')&lt;CR&gt;
+
:'<,'>call Surround('"', '"')<CR>
 
after : bla bla "Selec
 
after : bla bla "Selec
 
ted Text" bla bla
 
ted Text" bla bla
Line 55: Line 58:
   
 
<pre>
 
<pre>
vnoremap _" :call Surround('"', '"')&lt;CR&gt;
+
vnoremap _" :call Surround('"', '"')<CR>
vnoremap _( :call Surround('(', ')')&lt;CR&gt;
+
vnoremap _( :call Surround('(', ')')<CR>
vnoremap _[ :call Surround('[', ']')&lt;CR&gt;
+
vnoremap _[ :call Surround('[', ']')<CR>
vnoremap _{ :call Surround('{', '}')&lt;CR&gt;
+
vnoremap _{ :call Surround('{', '}')<CR>
 
</pre>
 
</pre>
   
Line 64: Line 67:
   
 
<pre>
 
<pre>
command! -range -nargs=* Sur call Surround(&lt;f-args&gt;)
+
command! -range -nargs=* Sur call Surround(<f-args>)
 
</pre>
 
</pre>
   
 
<pre>
 
<pre>
 
before: bla bla Selected Text bla bla
 
before: bla bla Selected Text bla bla
:'&lt;,'&gt;Sur (&lt;\ - -\ &gt;)
+
:'<,'>Sur (<\ - -\ >)
after : bla bla (&lt; -Selected Text- &gt;) bla bla
+
after : bla bla (< -Selected Text- >) bla bla
 
</pre>
 
</pre>
   
Line 76: Line 79:
 
<pre>
 
<pre>
 
"wrap highlighted text in doublequotes
 
"wrap highlighted text in doublequotes
:vmap [q "zdi"&lt;C-R&gt;z"
+
:vmap [q "zdi"<C-R>z"
"these wrap ansii color character commands around the visualmode selected text - good for adding
+
"these wrap ansi color character commands around the visualmode selected text - good for adding
 
"color to stdout [b=blue, [r=red, etc
 
"color to stdout [b=blue, [r=red, etc
:vmap [b "zdi&lt;C-V&gt;&lt;ESC&gt;[1;34m&lt;C-R&gt;z&lt;C-V&gt;&lt;ESC&gt;[0m&lt;ESC&gt;
+
:vmap [b "zdi<C-V><Esc>[1;34m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [r "zdi&lt;C-V&gt;&lt;ESC&gt;[1;31m&lt;C-R&gt;z&lt;C-V&gt;&lt;ESC&gt;[0m&lt;ESC&gt;
+
:vmap [r "zdi<C-V><Esc>[1;31m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [m "zdi&lt;C-V&gt;&lt;ESC&gt;[1;35m&lt;C-R&gt;z&lt;C-V&gt;&lt;ESC&gt;[0m&lt;ESC&gt;
+
:vmap [m "zdi<C-V><Esc>[1;35m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [c "zdi&lt;C-V&gt;&lt;ESC&gt;[1;36m&lt;C-R&gt;z&lt;C-V&gt;&lt;ESC&gt;[0m&lt;ESC&gt;
+
:vmap [c "zdi<C-V><Esc>[1;36m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [y "zdi&lt;C-V&gt;&lt;ESC&gt;[1;33m&lt;C-R&gt;z&lt;C-V&gt;&lt;ESC&gt;[0m&lt;ESC&gt;
+
:vmap [y "zdi<C-V><Esc>[1;33m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [g "zdi&lt;C-V&gt;&lt;ESC&gt;[1;32m&lt;C-R&gt;z&lt;C-V&gt;&lt;ESC&gt;[0m&lt;ESC&gt;
+
:vmap [g "zdi<C-V><Esc>[1;32m<C-R>z<C-V><Esc>[0m<Esc>
 
</pre>
 
</pre>
   

Latest revision as of 23:40, 29 September 2008

This tip is deprecated for the following reasons:

This tip is deprecated by the surround plugin.

Tip 988 Printable Monobook Previous Next

created September 9, 2005 · complexity basic · author Jan Christoph Ebersbach · version 6.0


Inspired by VimTip987 I wrote a small function to surround selected text in visual-mode with text.

For example, to quote a selection:

before: bla bla Selected Text bla bla
:'<,'>call Surround('"', '"')<CR>
after : bla bla "Selected Text" bla bla
fun! Surround(s1, s2) range
  exe "normal vgvmboma\<Esc>"
  normal `a
  let lineA = line(".")
  let columnA = col(".")
  normal `b
  let lineB = line(".")
  let columnB = col(".")
  " exchange marks
  if lineA > lineB || lineA <= lineB && columnA > columnB
    " save b in c
    normal mc
    " store a in b
    normal `amb
    " set a to old b
    normal `cma
  endif
  exe "normal `ba" . a:s2 . "\<Esc>`ai" . a:s1 . "\<Esc>"
endfun

Surround also works for a selection over more than one line.

before: bla bla Selec
ted Text bla bla
:'<,'>call Surround('"', '"')<CR>
after : bla bla "Selec
ted Text" bla bla

Some handy mappings:

vnoremap _" :call Surround('"', '"')<CR>
vnoremap _( :call Surround('(', ')')<CR>
vnoremap _[ :call Surround('[', ']')<CR>
vnoremap _{ :call Surround('{', '}')<CR>

I defined it as a command to perform fast on-demand-surroundings

command! -range -nargs=* Sur call Surround(<f-args>)
before: bla bla Selected Text bla bla
:'<,'>Sur (<\ - -\ >)
after : bla bla (< -Selected Text- >) bla bla

Comments[]

"wrap highlighted text in doublequotes
:vmap [q "zdi"<C-R>z"
"these wrap ansi color character commands around the visualmode selected text - good for adding
"color to stdout [b=blue, [r=red, etc
:vmap [b "zdi<C-V><Esc>[1;34m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [r "zdi<C-V><Esc>[1;31m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [m "zdi<C-V><Esc>[1;35m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [c "zdi<C-V><Esc>[1;36m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [y "zdi<C-V><Esc>[1;33m<C-R>z<C-V><Esc>[0m<Esc>
:vmap [g "zdi<C-V><Esc>[1;32m<C-R>z<C-V><Esc>[0m<Esc>