Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
What about trying for yourself? Or reading the documentation? – romainl Jun 16 '12 at 19:14
See also "Bracketed Paste Mode" in my answer to "Pasting code into terminal window into vim" 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 '12 at 7:14
@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 at 19:24
up vote 5 down vote accepted

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.

share|improve this answer
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 Oct 22 '15 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 '15 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. – Lee Daniel Crocker Oct 22 '15 at 17:17

This post has my favorite answer,

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 ""

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

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

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

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

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

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

inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
share|improve this answer
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 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 at 21:29

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

share|improve this answer

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'
share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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