Pasting code into vi
Posted by javier ramirez on October 5, 2009
Every time I try to paste a big chunk of code into vi, it gets all messed up because of the autoindent. Each line gets indented taking the previous one as a reference, so when I try to paste something like..
global $wp_query;
parse_str($args, $r);
if (!isset($r['current'])) $r['current'] = -1;
if (!isset($r['show_all_parents'])) $r['show_all_parents'] = 0;
if (!isset($r['show_root'])) $r['show_root'] = 0;
if (!isset($r['list_tag'])) $r['show_root'] = 1;
..what I really get is..
global $wp_query;
parse_str($args, $r);
if (!isset($r['current'])) $r['current'] = -1;
if (!isset($r['show_all_parents'])) $r['show_all_parents'] = 0;
if (!isset($r['show_root'])) $r['show_root'] = 0;
if (!isset($r['list_tag'])) $r['show_root'] = 1;
..and that’s no good. Fortunately the solution is really simple. Just enter command mode and write
:set paste
Now you can paste in all its glory. When you are done, you can get back to normal with
:set nopaste
So now you don’t have any excuses left to write original code. Get out there and copy the whole internet before I format it!!!










Manuel said
Hi Javier. Another way is to press double quote + plus key + p, in other words:
"+p.The double quotes and the plus sign will copy the clipboard content to the vim buffer so you can yank it with p.I must add that this doesn’t work with the Unix-like middle click paste, just for the common Ctrl + C, but I find it faster.
javier ramirez said
Thanks for stopping by, Manuel :)
Problem with your approach is origin and destination must be within the same machine. I usually copy from a local file to a remote one, so I cannot copy the contents of my clipboard directly to the buffer and then paste.
The “:set paste” does the trick for me, with both local and remote sessions.
Manuel said
Totally right. It doesn’t work through a ssh session. Good to know!
Ozgun Koyun said
Hi Javier,
Nice trick! I always had the same problem, but I had never thought there would be a solution for it. Doh!
Thanks,
Ozgun.
javier ramirez said
glad to know it helped :)
Javier Vidal said
Cool. Very handy. I didn’t know about this vi command.
Gymnkertcrymn said
Authentic words, some true words man. You rocked my day!