24

I want to paste something I have cut from my desktop into a file open in Vi.

But if I paste the tabs embed on top of each other across the page.

I think it is some sort of visual mode change but can't find the command.

ironfroggy
  • 7,991
  • 7
  • 33
  • 44
tristanbailey
  • 4,427
  • 1
  • 26
  • 30

3 Answers3

52

If you're using plain vi:

You probably have autoindent on. To turn it off while pasting:

<Esc> :set noai

<paste all you want>

<Esc> :set ai

I have in my .exrc the following shortcuts:

map ^P :set noai^M
map ^N :set ai^M

Note that these have to be the actual control characters - insert them using Ctrl-V Ctrl-P and so on.

If you're using vim:

Use the paste option. In addition to disabling autoindent it will also set other options such as textwidth and wrapmargin to paste-friendly defaults:

<Esc> :set paste

<paste all you want>

<Esc> :set nopaste

You can also set a key to toggle the paste mode. My .vimrc has the following line:

set pastetoggle=<C-P> " Ctrl-P toggles paste mode
Community
  • 1
  • 1
Antti Kissaniemi
  • 18,944
  • 13
  • 54
  • 47
2

If you are using VIM, you can use "*p (i.e. double quotes, asterisk, letter p).

JayG
  • 4,339
  • 3
  • 23
  • 19
0

I found that if I copy tabbed lines first into a text editor and then recopy them from there to vim, then the tabs are correct.

Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047