Bug#504244: Vim script to turn on and use xterm bracketed paste mode

Josh Triplett josh at joshtriplett.org
Wed Dec 23 10:27:01 UTC 2009

tags 504244 + patch

Well, most of the patch, anyway.

The following, added somewhere vim runs at startup, will use xterm's
bracketed paste mode to make pasting automatically enable paste mode
(and insert mode).  Also works fine in ~/.vimrc .

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

Note that this will only work in terminals which implement bracketed
paste mode.  This includes xterm, but not necessarily every terminal
with TERM=xterm; however, it shouldn't do any harm if bracketed paste
mode doesn't exist, assuming the terminal at least knows to ignore
enable/disable commands for modes it doesn't know about.

Due to limitations of paste mode, I couldn't make this work if already
*in* paste mode (because vim doesn't interpret mappings), nor can I
avoid overwriting pastetoggle (because I don't have a place to restore
it from after the paste finishes).  However, note that I don't overwrite
pastetoggle unless the bracketed paste mode works.  Native support for
bracketed paste modes in vim could potentially avoid both of those problems.

- Josh Triplett

More information about the pkg-vim-maintainers mailing list