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?


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.

  • 1
    I actually changed it permanently with the solution described at 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 Feb 19 '15 at 17:29

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>

Not the answer you're looking for? Browse other questions tagged or ask your own question.