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 formatt开发者_StackOverflowing. 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?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.
As noted by @DenilsonSáMaia this answer has been packaged into a plugin; although, it is obsolete starting in Vim 8: https://github.com/ConradIrwin/vim-bracketed-paste
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.
Another way to do this, assuming you have your system clipboard set up properly is to do
"+p
This will paste from the system clipboard.
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).
In vim
:set paste
when you want to disable it
:set nopaste
When working inside a terminal the vim-bracketed-paste vim plugin will automatically handle pastes without needing any keystrokes before or after the paste.
This works in Terminal, iTerm2, and any "modern" x-term compatible terminals that support bracketed paste mode. As an added bonus it works also for tmux sessions. I am using it successfully with iTerm2 on a Mac connecting to a linux server and using tmux.
The plugin is basically a packaged version of the functionality that @Chris Page listed in his answer.
精彩评论