24

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?

3
  • What about trying for yourself? Or reading the documentation?
    – romainl
    Jun 16, 2012 at 19:14
  • 1
    See also "Bracketed Paste Mode" in my answer to "Pasting code into terminal window into vim" 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
    Jun 25, 2012 at 7:14
  • 3
    @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
    May 13, 2016 at 19:24

5 Answers 5

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.

3
  • When I set mouse=a, I cannot copy text. "Copy" is grayed out in the right-click menu and in the app menu. Oct 22, 2015 at 16:29
  • @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
    Oct 22, 2015 at 17:13
  • 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. Oct 22, 2015 at 17:17
9

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()
3
  • 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
    Apr 4, 2016 at 12:04
  • @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
    Apr 11, 2016 at 21:29
  • omg. you are my hero. I've typed so :set nopaste so many g'd times.
    – maxwell
    May 2, 2018 at 20:59
4

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'
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 :)

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .