Vim Paste Shortcut

Jun 17, 2015. | By: Henok Mikre

We love Vim here at BLEN. Sure, we have a few developers who have moved on to Atom. But we politely encourage all our developers to try Vim first. Granted, the Vim learning curve is real. My initial experience with Vi/Vim is quite similar to the one described in the Slate article that recently resurfaced on Twitter, “The Oldest Rivalry in Computing”. A decade later, the state of our union is stronger than ever!

There was one quirk that I never really figured out, though. Whenever I tried to copy and paste an indented code block, the resulting text was quite ugly. It seemed as though the indentation in the original text was being multiplied by my indentation settings in vimrc. So I would paste the text into gedit and save it to later :read the file into the final text. I did that for a few years.

In May 2013, Mike and I were in Portland for DrupalCon. After attending a full day of sessions, we headed to a coffee shop in the city to work on the new ED.gov. Mike noticed that I was jumping through hoops to paste a text in Vim. He then said, “you know you can just type :set paste, right?” I responded with, “what?” (fighting back tears). I could not believe there was such a simple solution to something that had been bugging me for years. I would like to think my productivity has gone way up eversince.

And, yesterday, I decided I would rather use a toggle than having to type :set paste and :set nopaste. I wanted to consult the Vim docs first, though. Here is what :help paste returned:

help doc on paste

I prefer to use F5 and F6 keys instead since I am on a Mac. Otherwise, I would have to use the fn key.

If this suits you, add the following in your .vimrc file:

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Paste
" :help paste recommends F10 and F11, but this is hard on Mac keyboard.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
map <F5> :set paste<CR>
map <F6> :set nopaste<CR>
imap <F5> <C-O>:set paste<CR>
imap <F6> <nop>
set pastetoggle=<F6>

Resources:

Subscribe

Subscribe to this blog via RSS.

Recent Posts

About

BLEN Corp is a small, minority, and veteran-owned information technology firm located in Washington D.C. Since 2004, we have been ahead of the curve in early adaption and implementation of cutting edge technologies including web and mobile development, service-oriented architecture, and other innovative web based solutions. Look at some of our projects.

Social Links

Our Bunker

WeWork Wonder Bread Factory
641 S St. NW
3rd Floor
Washington, DC 20001
United States.