Automate All the Things!

Doug Ireton's blog about Chef, Git, Ruby, Vim, and Infrastructure Automation

Flip Your Unicorn!

| Comments

Let’s say you had a unicorn in your code. Maybe something like this:

                                           ________
                                        .##@@&&&@@##.
     \                               ,##@&::%&&%%::&@##.
     ^\^                            #@&:%%000000000%%:&@#
     /.(((                        #@&:%00'         '00%:&@#
    (,/"(((__,--.                #@&:%0'             '0%:&@#
        \  ) _( /{              #@&:%0                 0%:&@#
        !|| " :||              #@&:%0                   0%:&@#
        !||   :||              #@&:%0                   0%:&@#
        '''   '''              "" ' "                   " ' ""

But it’s so wrong!

Clearly, something is wrong with the unicorn. It’s not facing the rainbow. This week, we’ll learn how to flip the unicorn (or any other text) with an awesome Vim Visual mode mapping courtesy of the inimitable Dr. Chip

Show me the map!

Here it is, just don’t ask me to explain it.

1
vno  <silent> <Leader>fR   c<C-O>:set ri lz<cr><C-R>"<esc>:norm! dd`<<cr>:set ri! lz!<cr>

And, here are the results*

            /       \               
          ^/^       ^\^          
        ))).\       /.(((        
 .--,__)))"\,)     (,/"(((__,--. 
}\ )_ (  /             \  ) _( /{
 ||: " ||!             !|| " :|| 
 ||:   ||!             !||   :|| 
 '''   '''             '''   ''' 

Well, not exactly. The mapping doesn’t exactly flip text. It won’t turn a ) into a (, or a } into a {. It just exchanges or swaps the places of the characters. Still it gets pretty close, much easier than trying to do it by hand. You can then run substitute commands to flip the ), \, and }.

The Actual Results

            \ 
          ^\^ 
        (((./ 
 .--,__((("/,(
{/ (_ )  \    
 ||: " ||!    
 ||:   ||!    
 '''   '''    

Uhhh, ok, why would you want to do this?

I have no idea, unless you had a unicorn you wanted to flip on a deadline. Thanks to Google and Dr. Chip for this (marginally useful) Vim tip! Until next week, cheers!

For the rest of the mappings which will flip text upside down as well, see Dr. Chip’s maps section on his awesomely 1995 website.

                                         ________
                                      .##@@&&&@@##.
                 /                 ,##@&::%&&%%::&@##.                   \                       
               ^/^                #@&:%%000000000%%:&@#                  ^\^          
             ))).\              #@&:%00'         '00%:&@#                /.(((        
      .--,__)))"\,)            #@&:%0'             '0%:&@#              (,/"(((__,--. 
     }\ )_ (  /               #@&:%0                 0%:&@#                 \  ) _( /{
      ||: " ||!              #@&:%0                   0%:&@#                !|| " :|| 
      ||:   ||!              #@&:%0                   0%:&@#                !||   :|| 
      '''   '''              "" ' "                   " ' ""                '''   ''' 

Comments