Pasting code into terminal window into vim on Mac OS X

eat_a_lemon picture eat_a_lemon · Apr 7, 2011 · Viewed 24.5k times · Source

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        
   ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
        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?

Answer

Chris Page picture Chris Page · Aug 13, 2011

UPDATE: Vim 8 includes native support for Bracketed Paste Mode. It is enabled by default. See Vim’s xterm-bracketed-paste help topic. Users no longer need to do anything to configure Vim to support this.

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
    endfunction
    map <expr> <Esc>[200~ XTermPasteBegin("i")
    imap <expr> <Esc>[200~ XTermPasteBegin("")
    vmap <expr> <Esc>[200~ XTermPasteBegin("c")
    cmap <Esc>[200~ <nop>
    cmap <Esc>[201~ <nop>
endif

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.