It is even supposed to be by default in Vim 8.
Only if Vim thinks the terminal is xterm-compatible, which is the case if TERM
is xterm
or some derivative.
If TERM
is gnome
, Vim won't enable the bracketed paste mode automatically; you have to configure it.
I have vim 8, how to enable bracketed paste mode for gnome terminal?
Try this:
if &t_BE == ''
let &t_BE = "\e[?2004h"
let &t_BD = "\e[?2004l"
let &t_PS = "\e[200~"
let &t_PE = "\e[201~"
endif
t_BE
, t_BD
, t_PS
and t_PE
are all terminal options. The first two are sent automatically to the terminal whenever it enters/leaves raw mode; they must be set correctly so that they enable/disable the bracketed paste mode.
The sequences \e?2004h
and \e?2004l
are documented in xterm's documentation. Look for CSI ? Pm h
(or CSI ? Pm l
), then for Ps = 2 0 0 4
.
Ps = 2 0 0 4 ⇒ Set bracketed paste mode, xterm.
...
Ps = 2 0 0 4 ⇒ Reset bracketed paste mode, xterm.
t_PS
and t_PE
tell Vim which sequences the terminal will use to surround the pasted text. The values are documented here.
To test the code, start Vim like this:
vim +'let @+ = "clipboard\e:echom \"malicious code injected\"\ri"' +startinsert
Then press C-S-v
to insert the contents of the clipboard in the Vim buffer.
If the only text which is inserted is clipboard
and :mess
has logged malicious code injected
, then it doesn't work.
Note that the bracketed paste mode can be bypassed if the pasted text contains \e[201~
; the latter sequence may end the bracketed mode prematurely. See the second example on this page. It doesn't work on all terminals though; see this answer for more info.
Also, the bracketed paste mode does not prevent Vim from auto-indenting the text when inserting the contents of a register with C-r
.
$ vim -Nu NONE +'set ai' +':let @a = "Level 1\n Level 2\n Level 2\nLevel 1"'
" press: i C-r a
If 'paste'
is reset, you'll get:
Level 1
Level 2
Level 2
Level 1
If 'paste'
is set, you'll get:
Level 1
Level 2
Level 2
Level 1
You can avoid this issue by pasting from normal mode ("ap
), or inserting with C-r C-o
or C-r C-p
.
FWIW, for this reason, I use these mappings:
ino <expr> <c-r> getregtype(v:register) =~# '<c-v>' ? '<c-r>' : '<c-r><c-o>'
ino <c-r><c-p> <c-r><c-p>
ino <c-r><c-o> <c-r><c-o>
ino <c-r><c-r> <c-r><c-r>
If you use tmux, be aware that the latter supports the bracketed paste mode since version 1.7, but it doesn't use it by default. In the key bindings table, ]
is bound to the paste-buffer
command:
bind-key -T prefix ] paste-buffer
If you want paste bracket control codes to be inserted around the buffer, you need to pass the -p
flag to paste-buffer
. FWIW, I use it in these 3 key bindings:
bind C-p paste-buffer -p
bind p choose-buffer -Z -F "#{buffer_sample}" "paste-buffer -p -b '%%'"
bind -T copy-mode-vi p send -X copy-selection-and-cancel \; paste-buffer -p \; delete-buffer
Update: This was changed in 3f6af41.
For more info, see :h xterm-bracketed-paste
.
export TERM=gnome-256color
in your shell before you start Vim, does that make bracketed mode work? Please edit the question to include your original value of$TERM
before you change it with this suggestion...set paste
a bit more automatically (particularly disabling it right after pasting and exiting insert mode.)~/.inputrc
withset enable-bracketed-paste on
)TERM=xterm-256color
, so configure your GNOME Terminal to do that (it might actually be the default.)export TERM=xterm-256color
to.bashrc
solves the issue.