Always use :set paste, Is it a good idea?

19

8

In a terminal vim, pasting clipboard data often messes up the code indent. I just knew if I uses :set paste the indent is not broken. Though, after pasting data, should I do :set nopaste again? What if I don't, what problem comes?

Benjamin

Posted 2012-06-16T18:59:35.740

Reputation: 1 849

1@romainl I find it quite plausible that both 1) trying and waiting to see if there are negative consequences of always using set paste (+- nopaste), and 2) rtfm-ing (as a lower or intermediate level user like the OP), are less useful than asking those who have experience with the options. Asking here seems a smarter approach, and documenting this will help others. – belacqua – 2016-05-13T19:24:18.383

What about trying for yourself? Or reading the documentation?

– romainl – 2012-06-16T19:14:21.020

1

See also "Bracketed Paste Mode" in my answer to "Pasting code into terminal window into vim" http://stackoverflow.com/a/7053522/754997. If your terminal emulator supports it, you can arrange for it to tell Vim to switch to/from paste mode automatically whenever you paste text into the terminal.

– Chris Page – 2012-06-25T07:14:53.820

Answers

5

As others have written, you don't want to leave 'paste' set. I just wanted to point out that with a good terminal emulator and a properly compiled and configured vim, you shouldn't need to change 'paste'. You need a good terminal emulator such as xterm or GNOME Terminal, vim with the X11 feature included, and the 'mouse' option set to 'a'. Then vim will "know" when you're pasting with the mouse and will effectively set and unset the 'paste' option for you.

One way to get a vim with the X11 feature is to run gvim with the -v option or create an alias,

alias vim='gvim -v'

Then put

set mouse=a

in your ~/.vimrc.

garyjohn

Posted 2012-06-16T18:59:35.740

Reputation: 29 085

When I set mouse=a, I cannot copy text. "Copy" is grayed out in the right-click menu and in the app menu. – Lee Daniel Crocker – 2015-10-22T16:29:26.680

@LeeDanielCrocker: Copy will be grayed out or not visible at all unless some text has been selected. How are you selecting the text to be copied? – garyjohn – 2015-10-22T17:13:57.997

Just with mouse. FYI, "y" works fine...yanks the text and copies it to system keyboard. It's just the menus that don't work. If mouse is set to "r", then the menus work, but the mouse includes the line numbers. So all in all, I can live with mouse=a, I just have to remember to yank instead of copy. – Lee Daniel Crocker – 2015-10-22T17:17:40.727

7

This post has my favorite answer, https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode

Basically if you start in Insert mode and use Ctrl+Shift+V or right click paste with your mouse, Vim detects that this came from a terminal and automatically sets paste mode, then unsets it once done, so you don't lose remapped keys (which can't work in paste mode because its writing raw data) and you are back to a "sane" state when it is done.

For just vim (put in your .vimrc)

let &t_SI .= "\<Esc>[?2004h"
let &t_EI .= "\<Esc>[?2004l"
inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()

function! XTermPasteBegin()
  set pastetoggle=<Esc>[201~
  set paste
  return ""
endfunction

If you use vim under Tmux (still goes in .vimrc)

function! WrapForTmux(s)
  if !exists('$TMUX')
    return a:s
  endif

  let tmux_start = "\<Esc>Ptmux;"
  let tmux_end = "\<Esc>\\"

  return tmux_start . substitute(a:s, "\<Esc>", "\<Esc>\<Esc>", 'g') . tmux_end
endfunction

let &t_SI .= WrapForTmux("\<Esc>[?2004h")
let &t_EI .= WrapForTmux("\<Esc>[?2004l")

function! XTermPasteBegin()
  set pastetoggle=<Esc>[201~
  set paste
  return ""
endfunction

inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()

dragon788

Posted 2012-06-16T18:59:35.740

Reputation: 634

I assume one of the let &t_SI .= "\<Esc>[?xxxxx" lines is detecting the keyboard paste shortcut? Does this work for mac as well given the different shortcut? – doub1ejack – 2016-04-04T12:04:58.600

@doub1ejack These are actually detecting the Xterm escape sequences, not how they were called (ie 2004h is a paste begin) so as long as your terminal supports the Xterm keycodes and passes them through to Vim it should work fine on Mac. – dragon788 – 2016-04-11T21:29:28.677

omg. you are my hero. I've typed so :set nopaste so many g'd times. – maxwell – 2018-05-02T20:59:13.737

3

As romainl suggested, the documentation explains that setting the 'paste' option disables several other options, and you will certainly find that sooner rather than later it will cause problems for you. For this reason, there is the 'pastetoggle' option. See:

:help 'paste'
:help 'pastetoggle'

Heptite

Posted 2012-06-16T18:59:35.740

Reputation: 16 267

1

If something exists, it must have its meaning. You should take a good look at vim documentation which is very useful.

  :help 'paste'
  :help 'pastetoggle'

Once you read, you may want this :

" Toggle paste mode
"   (prefer this over 'pastetoggle' to echo current state)
nmap <leader>p :setlocal paste! paste?<cr>

I hope you find this post useful :)

Dzung Nguyen

Posted 2012-06-16T18:59:35.740

Reputation: 1 351

1

IIRC when you paste into vim, it essentially thinks that you typed all those characters yourself. So if auto-indent is turned on, it will indent stuff for you, but the pasted text usually contains indentation already so the indent indeed gets "messed up". Switching to paste mode turns off stuff like auto-indent.

If you like auto-indent as-you-type, you should switch it back to nopaste after you're done pasting. Try it and note how you have to do all indentation manually in paste mode.

Sumudu Fernando

Posted 2012-06-16T18:59:35.740

Reputation: 111