18

I don't have a huge amount of experience using VI. I am running it on Mac OSX.

I've copied and pasted text before in the editor using (when I say gui in the following I mean the Mac OSX gui)

  1. Cursor to highlight and copy i using command C or the gui or the yy command in VI.
  2. Entering insert mode where I want to paste the text and then pasting using command V or the gui

My problem is that a very long line that is split over multiple lines in the terminal becomes multiple lines as shown on the terminal when copied and pasted by any of the methods.

How do I get it to copy and paste excatly as is?

denfromufa
  • 5,610
  • 13
  • 81
  • 138
Codey McCodeface
  • 2,988
  • 6
  • 30
  • 55

6 Answers6

21
  1. Move the cursor to the line from where you want to copy and paste contents at another place.
  2. Hold the key v in press mode and press upper or lower arrow key according to requirements or up to lines that will be copied. you can press key V to select whole lines.
  3. Press d to cut or y to copy.
  4. Move the cursor to the place where you want to paste.
  5. Press p to paste contents after the cursor or P to paste before the cursor.
PRAFUL ANAND
  • 385
  • 3
  • 7
12

You have

 :set paste

Put Vim in Paste mode. This is useful if you want to cut or copy some text from one window and paste it in Vim. This will avoid unexpected effects. Setting this option is useful when using Vim in a terminal, where Vim cannot distinguish between typed text and pasted text.

zzapper
  • 4,743
  • 5
  • 48
  • 45
8

Assuming your vi is actually vim, before pasting, do:

:set paste

That disables word wrapping and auto-indent and all similar things that modify typed text. After pasting, turn it off again with

:set nopaste

The reason is that while gvim can tell pasting from typing (so you don't need this when using gvim), the terminal version can't, because it's the terminal doing copy and paste and vim simply sees the text as typed. And therefore applies the transformation like it does for any other text.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
3

Someone showed me a neat trick. In the vi editor, set to insert mode ("i"). Then middle-button click at the location where you would like to insert.

Philippe
  • 4,088
  • 4
  • 44
  • 49
1

Well, if it is actually one long line the easiest way to do this is with a 'Y' in command mode. Just move to the line and do Y and then move to where you want to put the line and do a p (for paste).

1

After trying everything suggested here and in other answers including trying to format the file afterwards with vi(m) commands and seds, I got smart and just heredoc'd what I wanted to paste with redirection to the file. i.e.

cat << EOF > yourfile.txt
paste what you are trying to paste
another line of pasted text
yet another
foo > bar?
foo = bar??
end of the file(yay)
EOF

everything between the first line and the last line will be pasted to your file without those pesky newlines interpreted as many spaces/tabs. Just beware that what you are pasting might have its own heredoc(like mine did coincidently LOL). In that case would need to manually paste those lines in your editor which shouldn't be an issue.