Take the 2-minute tour ×

When I paste code into my Mac OS X terminal window into vim it indents each line. For each line it adds an indent so the text looks like this...

"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud        
        reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
             Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
                    deserunt mollit anim id est laborum."

My current workaround is I paste the text first into textmate text editor which keeps the correct formatting. Then I save that file and open it up in vim. Then I use vim yank to paste it.
Is there a setting in my .vimrc that could change this behavior? Or is this a terminal issue?

share|improve this question
add comment

6 Answers

up vote 27 down vote accepted

Within vim:

:set paste

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.

share|improve this answer
wow awesome, thanks! –  eat_a_lemon Apr 7 '11 at 17:48
add comment

As of Mac OS X Lion 10.7, Terminal supports “bracketed paste mode,” which enables the terminal emulator to tell the program connected to the tty when the user pastes text, so that the program won’t interpret it as editing commands. Programs that support it send the terminal an escape sequence to enable this mode, in which the terminal surrounds pasted text with a pair of escape sequences that identify the start and end.

To enable this in Vim, put the following code in your ~/.vimrc file:

if &term =~ "xterm.*"
    let &t_ti = &t_ti . "\e[?2004h"
    let &t_te = "\e[?2004l" . &t_te
    function XTermPasteBegin(ret)
        set pastetoggle=<Esc>[201~
        set paste
        return a:ret
    map <expr> <Esc>[200~ XTermPasteBegin("i")
    imap <expr> <Esc>[200~ XTermPasteBegin("")
    cmap <Esc>[200~ <nop>
    cmap <Esc>[201~ <nop>

This makes it so that when Vim switches the terminal to/from the alternate screen† (t_ti, t_te) it enables/disables bracketed paste mode (ESC [? 2004 h, ESC [? 2004 l). When it receives the escape sequence indicating the start of a paste (ESC [ 200 ~), it enables Paste mode (set paste) and switches to Insert mode if necessary ("i"). When it receives the matching end-of-paste marker (ESC [ 201 ~) it disables Paste mode (pastetoggle) and remains in Insert mode. The cmap commands arrange for the Vim command line to ignore the escape sequences and accept the pasted text as-is.

Note that this only enables bracketed paste mode when the $TERM value starts with "xterm…"; if you're setting $TERM to something else, you may want to revise that test to include your $TERM value. Or, you could omit the test altogether, since it isn’t strictly necessary—it’s just trying to be careful not to do something that might be incompatible with some other terminal type.

In Terminal, this works with all the various Paste commands, as well as drag-and-drop.

† The terminal has a main screen and an "alternate" screen. Each screen has its own contents and state. Text in the alternate screen does not scroll up into the scrollback log. It is typically used by programs that take over control of the whole screen and are therefore referred to as "full screen" programs. This includes vim, emacs, less and top, for example.

share|improve this answer
You should also add a mapping for command mode (the on you enter with a colon). Otherwise you loose the ability to paste into the command line: cmap <Esc>[200~ <nop> cmap <Esc>[201~ <nop> –  Raim Sep 9 '11 at 9:12
This is now supported by iTerm2: iterm2.googlecode.com/svn/trunk/appcasts/testing_changes.html –  Matt Sep 13 '11 at 20:56
Thanks @Raim, I've added the cmap commands. –  Chris Page Sep 25 '11 at 8:05
@ChrisPage some additions to speed up the esc mapping and make it work in tmux: github.com/aaronjensen/vimfiles/blob/master/vimrc#L449-483 –  Aaron Jensen Aug 11 '12 at 8:49
@AaronJensen do you have a reference to documentation that explains why that’s necessary and how it works? –  Chris Page Aug 15 '12 at 2:58
show 2 more comments

In vim

:set paste

when you want to disable it

:set nopaste

share|improve this answer
add comment

In addition to the other answers, if you want a quick way to toggle paste mode, add

set pastetoggle=<F2>

to your .vimrc. Now you can toggle paste mode by pressing F2 (or whatever key you choose).

share|improve this answer
add comment

Another way to do this, assuming you have your system clipboard set up properly is to do


This will paste from the system clipboard.

share|improve this answer
add comment

If you have a mouse with a middle button (often the wheel), you can use that (you have to be in insert mode).

share|improve this answer
add comment

Your Answer


