macos 将代码粘贴到 Mac OS X 上的 vim 终端窗口中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5585129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pasting code into terminal window into vim on Mac OS X
提问by eat_a_lemon
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...
当我将代码粘贴到我的 Mac OS X 终端窗口中时,它会缩进每一行。对于每一行,它添加一个缩进,因此文本看起来像这样......
"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?
我目前的解决方法是首先将文本粘贴到保持正确格式的 textmate 文本编辑器中。然后我保存该文件并在 vim 中打开它。然后我使用 vim yank 粘贴它。
我的 .vimrc 中是否有可以改变这种行为的设置?或者这是一个终端问题?
回答by Chris Page
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.
更新:Vim 8 包括对 Bracketed Paste Mode 的本机支持。它默认启用。请参阅 Vim 的xterm-bracketed-paste
帮助主题。用户不再需要做任何事情来配置 Vim 来支持这一点。
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.
从 Mac OS X Lion 10.7 开始,终端支持“括号粘贴模式”,这使得终端模拟器能够在用户粘贴文本时告诉连接到 tty 的程序,这样程序就不会将其解释为编辑命令。支持它的程序向终端发送一个转义序列以启用此模式,在这种模式下,终端用一对标识开始和结束的转义序列包围粘贴的文本。
To enable this in Vim, put the following code in your ~/.vimrc file:
要在 Vim 中启用此功能,请将以下代码放入 ~/.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
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.
这使得当 Vim 将终端切换到/从备用屏幕切换时?(t_ti, t_te) 它启用/禁用括号粘贴模式 (ESC [? 2004 h, ESC [? 2004 l)。当它接收到指示粘贴开始的转义序列(ESC [ 200 ~)时,它启用粘贴模式(设置粘贴)并在必要时切换到插入模式(“i”)。当它接收到匹配的粘贴结束标记 (ESC [ 201 ~) 时,它会禁用粘贴模式 (pastetoggle) 并保持插入模式。这些cmap
命令安排 Vim 命令行忽略转义序列并按原样接受粘贴的文本。
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.
请注意,这仅在 $TERM 值以“xterm...”开头时才启用括号粘贴模式;如果您将 $TERM 设置为其他内容,您可能需要修改该测试以包含您的 $TERM 值。或者,您可以完全省略测试,因为它并不是绝对必要的——它只是试图小心不要做一些可能与其他终端类型不兼容的事情。
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.
? 终端有一个主屏幕和一个“替代”屏幕。每个屏幕都有自己的内容和状态。备用屏幕中的文本不会向上滚动到回滚日志中。它通常由接管整个屏幕的程序使用,因此被称为“全屏”程序。例如,这包括 vim、emacs、less 和 top。
回答by William Pursell
Within vim:
在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.
将 Vim 置于粘贴模式。如果您想从一个窗口剪切或复制一些文本并将其粘贴到 Vim 中,这将非常有用。这将避免意外的影响。
回答by dancavallaro
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).
到您的 .vimrc。现在您可以通过按 F2(或您选择的任何键)来切换粘贴模式。
回答by Aaron Jensen
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.
这将从系统剪贴板粘贴。
回答by WenbinWu
In vim
在vim中
:set paste
:设置粘贴
when you want to disable it
当你想禁用它时
:set nopaste
:set nopaste
回答by studgeek
When working inside a terminal the vim-bracketed-pastevim plugin will automatically handle pastes without needing any keystrokes before or after the paste.
在终端内工作时,vim-bracketed-pastevim 插件将自动处理粘贴,而无需在粘贴之前或之后进行任何按键操作。
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.
这适用于终端、iTerm2 和任何支持括号粘贴模式的“现代”x-term 兼容终端。作为额外的奖励,它也适用于 tmux 会话。我在连接到 linux 服务器并使用 tmux 的 Mac 上成功地将它与 iTerm2 一起使用。
The plugin is basically a packaged version of the functionality that @Chris Page listed in his answer.
该插件基本上是@Chris Page 在他的回答中列出的功能的打包版本。