format internet:

…please wait (48% completed)…

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!!!


10 Responses to “Pasting code into vi”

  1. 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.

    • 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.

  2. Manuel said

    Totally right. It doesn’t work through a ssh session. Good to know!

  3. Hi Javier,

    Nice trick! I always had the same problem, but I had never thought there would be a solution for it. Doh!


  4. Cool. Very handy. I didn’t know about this vi command.

  5. Authentic words, some true words man. You rocked my day!

  6. opinador said

    En mis tiempos…:

    set noai
    set ai

  7. mjdfgcss said AllSubmitter – Программа для раскрутки сайта. Максимально качественная, быстрая и контролируемая регистрации в любых информационных ресурсах Интернета.

  8. Sherry said

    Hi webmaster do you need unlimited content for your page ?

    What if you could copy article from other sources, make
    it pass copyscape test and publish on your page – i know the right tool for you,
    just type in google:
    kisamtai’s article tool

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: