Vim Tips Wiki
Register
(Adjust previous/next navigation + minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
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 <tt>;rv</tt> – really only works with selections on one line:
+
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>
 
<pre>
 
vnoremap ;rv c<C-O>:set revins<CR><C-R>"<Esc>:set norevins<CR>
 
vnoremap ;rv c<C-O>:set revins<CR><C-R>"<Esc>:set norevins<CR>

Revision as of 05:19, 13 July 2012

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')), ''))

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