4

I am pasting several lines of text into vim at once (from another application, so I am using the OS's copy and paste and insert mode in vim). The longest of these lines is below:

" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal

The problem with this line is that vim inserts a new line after "to" so it looks like this:

" :PluginClean      - confirms removal of unused plugins; append `!` to 
" auto-approve removal

How can I keep this line together when pasting?

Greg
  • 857
  • 1
  • 7
  • 11
  • Does this happen in other text-editors? – Melon Feb 19 '15 at 16:51
  • This is pasting into a terminal? :set paste. See also http://vi.stackexchange.com/questions/328/set-the-paste-option-but-for-one-insertion-only and http://vi.stackexchange.com/questions/730/how-can-i-copy-paste-new-text-without-auto-indentation-at-every-new-line. – derobert Feb 19 '15 at 16:57

2 Answers2

7

Before pasting, do :set paste. This changes a bunch of settings like 'textwidth' and 'autoindent' for specifically this purpose. Then enter insert mode, paste with your OS, hit <Esc> to leave insert mode, and do :set nopaste.

You can also toggle settings with a !, i.e. :set paste! to turn it on/off.

Matt Boehm
  • 2,938
  • 16
  • 14
  • 1
    I actually changed it permanently with the solution described at http://vi.stackexchange.com/a/1985/178, but this also works and is better suited for this particular problem. – Greg Feb 19 '15 at 17:06
  • 1
    There's also :set invpaste to toggle it – Collin Grady Feb 19 '15 at 17:29
3

To paste text into vim without auto-indentation, you need to 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. See: :help paste for more details.

To make a life easier, there is pastetoggle variable which specifies the key sequence that toggles the 'paste' option (:set invpaste).

In example if you'd like to use F2 to disable autoindenting if pasting into terminal in X, you may use the following code into your .vimrc file:

nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>
kenorb
  • 18,433
  • 18
  • 72
  • 134