VI: Paste code without formatting

By Eric Downing. Filed in Editors, Software, Utilities, vi  |  
TOP del.icio.us digg

I happened to be creating a script and found myself trying to copy and past some functions between them. Well my functions
had some comments and this caused the paste operation into vi to add comments to every line after the first comment.

So this:

sub somefunc {
  # input args
  my $var1 = shift;
  my $var2 = shift;

  if ($var1 < $var2)
  {
    return $var1;
  }
  else
  {
    return $var2;
  }
}

Became this:

sub somefunc {
  # input args
  #my $var1 = shift;
  #my $var2 = shift;
  #
  #if ($var1 < $var2)
  #{
  #  return $var1;
  #}
  #else
  #{
  #  return $var2;
  #}
  #}

This made all my formatting go awry as well, and there were about 4 different functions I was about to cut and past. So I looked into how to make vi accept the text as is without adding formatting. The answer is the paste and nopaste mode.

To enter the mode to allow pasting without formatting:

:set paste

To return to regular mode:

:set nopaste

There are also ways to bind a key to more quickly enter and exit this mode if you are pasting on a regular basis.
That will be in another article.

Tags: ,

Leave a Reply