Vim Tips Wiki
m (Reverse Selected Text moved to Reverse selected text: Page moved by JohnBot to improve title)
(Include an easier way to do a vertical mirror.)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=223
 
|id=223
  +
|previous=222
|title=Reverse Selected Text
 
  +
|next=224
|created=March 11, 2002 21:36
+
|created=2002
 
|complexity=basic
 
|complexity=basic
 
|author=scott urban
 
|author=scott urban
 
|version=5.7
 
|version=5.7
 
|rating=8/5
 
|rating=8/5
  +
|category1=
|text=
 
  +
|category2=
Suppose you want to reverse some text - I don't know why you would want to - maybe you're dyslexic. Anyway, I had a need, so this mapping will reverse visually selected text. Put the mapping in your vimrc or otherwise source it, then visually select the word or words, and hit ;rv - really only works with selections on one line:
 
 
 
 
vnoremap ;rv c<C-O>:set revins<cr><C-R>"<esc>:set norevins<cr>
 
 
 
 
 
 
 
 
}}
 
}}
 
Suppose you want to reverse some text. This mapping will reverse visually selected text. Put the mapping in your vimrc or otherwise source it, then visually select the word or words, and type <code>;rv</code> really only works with selections on one line:
  +
<pre>
 
vnoremap ;rv c<C-O>:set revins<CR><C-R>"<Esc>:set norevins<CR>
  +
</pre>
   
== Comments ==
+
==Comments==
Interesting idea, but it will only work for those whose vim has the
+
Interesting idea, but it will only work for those whose Vim has the +rightleft option. For those who don't have such a Vim, use "ma" to (mark a) and move the cursor to the last line to be affected, and:
  +
<pre>
+rightleft option. For those who don't have such a vim, use "ma" to
 
 
" \fr: reverse the order of lines (vertical mirror)
(mark a) and move the cursor to the last line to be affected, and:
 
 
nmap \fr :set lz<CR>o<Esc>mz'aO<Esc>ma:'a+1,'z-1g/^/m 'a<CR>'addma'zdd:set nolz<CR>
 
\fr: will reverse the order of lines (vertical mirror)
 
nmap \fr :set lz&lt;CR&gt;o&lt;Esc&gt;mz'aO&lt;Esc&gt;ma:'a+1,'z-1g/^/m 'a&lt;CR&gt;'addma'zdd:set nolz&lt;CR&gt;
 
 
\fR: will mirror image the lines (horizontal mirror)
 
nmap \fR :set lz&lt;CR&gt;o&lt;Esc&gt;mzkO&lt;Esc&gt;maj:s/./&amp;\r/g&lt;CR&gt;:'a+1,'z-1g/^/m 'a&lt;CR&gt;:'a+1,'z-1j!&lt;CR&gt;'add'zddk:set nolz&lt;CR&gt;
 
 
Regards,
 
Charles Campbell
 
 
 
 
 
cec--AT--NgrOyphSon.gPsfAc.nMasa.gov
 
, March 12, 2002 12:49
 
----
 
I really like cryptic commands.
 
It might be easier to get a full/fitting powered vim ?
 
   
 
" \fR: mirror image the lines (horizontal mirror)
Thomas
 
 
nmap \fR :set lz<CR>o<Esc>mzkO<Esc>maj:s/./&\r/g<CR>:'a+1,'z-1g/^/m 'a<CR>:'a+1,'z-1j!<CR>'add'zddk:set nolz<CR>
  +
</pre>
   
  +
TIMTOWTDI: Here's a simpler approach for the horizontal mirror, and a command thrown in as a bonus:
  +
<pre>
  +
nmap \fR :Mirror<CR>
  +
command! -bar -range Mirror <line1>,<line2>call setline('.', join(reverse(split(getline('.'), '\zs')), ''))
  +
</pre>
   
  +
TIMTOWTDI: Here's a simpler approach for the vertical mirror:
Thomas.Ramming--AT--gmx.de
 
  +
<pre>
, March 24, 2002 23:24
 
  +
map \fr :Reverse<CR>
  +
sunmap \fr
  +
ounmap \fr
  +
command! -bar -range=% Reverse <line1>,<line2>g/^/m<line1>-1
  +
</pre>
  +
Notice it was mapped then had specific sections unmapped since they don't make sense to have them.
 
----
 
----
  +
I use it in my work to reverse the order of 1000 groups of parameters which improves the program performance. Thanks.
<!-- parsed by vimtips.py in 0.668786 seconds-->
 

Latest revision as of 14:16, 13 March 2013

Tip 223 Printable Monobook Previous Next

created 2002 · complexity basic · author scott urban · version 5.7


Suppose you want to reverse some text. This mapping will reverse visually selected text. Put the mapping in your vimrc or otherwise source it, then visually select the word or words, and type ;rv – really only works with selections on one line:

vnoremap ;rv c<C-O>:set revins<CR><C-R>"<Esc>:set norevins<CR>

Comments[]

Interesting idea, but it will only work for those whose Vim has the +rightleft option. For those who don't have such a Vim, use "ma" to (mark a) and move the cursor to the last line to be affected, and:

" \fr: reverse the order of lines (vertical mirror)
nmap \fr :set lz<CR>o<Esc>mz'aO<Esc>ma:'a+1,'z-1g/^/m 'a<CR>'addma'zdd:set nolz<CR>

" \fR: mirror image the lines (horizontal mirror)
nmap \fR :set lz<CR>o<Esc>mzkO<Esc>maj:s/./&\r/g<CR>:'a+1,'z-1g/^/m 'a<CR>:'a+1,'z-1j!<CR>'add'zddk:set nolz<CR>

TIMTOWTDI: Here's a simpler approach for the horizontal mirror, and a command thrown in as a bonus:

nmap \fR :Mirror<CR>
command! -bar -range Mirror <line1>,<line2>call setline('.', join(reverse(split(getline('.'), '\zs')), ''))

TIMTOWTDI: Here's a simpler approach for the vertical mirror:

map \fr :Reverse<CR>
sunmap \fr
ounmap \fr
command! -bar -range=% Reverse <line1>,<line2>g/^/m<line1>-1

Notice it was mapped then had specific sections unmapped since they don't make sense to have them.


I use it in my work to reverse the order of 1000 groups of parameters which improves the program performance. Thanks.