将代码粘贴到终端窗口到 Mac OS X 上的 vim 中
当我将代码粘贴到 Mac OS X 终端窗口到 vim 中时,它会缩进每一行。它为每一行添加一个缩进,因此文本看起来像这样...
"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."
我当前的解决方法是先将文本粘贴到 textmate 文本编辑器中,以保持正确的格式。然后我保存该文件并在 vim 中打开它。然后我使用 vim yank 来粘贴它。
我的 .vimrc 中是否有一个设置可以改变这种行为?或者这是一个终端问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新:Vim 8 包含对括号粘贴模式的本机支持。默认情况下它是启用的。请参阅 Vim 的
xterm-bracketed-paste
帮助主题。用户不再需要执行任何操作来配置 Vim 来支持此功能。从 Mac OS X Lion 10.7 开始,终端支持“括号粘贴模式”,该模式使终端仿真器能够在用户粘贴文本时告诉连接到 tty 的程序,以便程序不会将其解释为编辑命令。支持它的程序向终端发送一个转义序列以启用此模式,在该模式下,终端用一对标识开始和结束的转义序列包围粘贴的文本。
要在 Vim 中启用此功能,请将以下代码放入 ~/.vimrc 文件中:
这使得当 Vim 将终端切换到备用屏幕/从备用屏幕† (t_ti, t_te) 切换终端时,它会启用/禁用括号粘贴模式 (ESC [ ?2004 h,ESC [?2004 l)。当它收到指示粘贴开始的转义序列 (ESC [ 200 ~) 时,它会启用粘贴模式(设置粘贴)并在必要时切换到插入模式 (“i”)。当它收到匹配的粘贴结束标记 (ESC [ 201 ~) 时,它会禁用粘贴模式 (pastetoggle) 并保持插入模式。
cmap
命令安排 Vim 命令行忽略转义序列并按原样接受粘贴的文本。请注意,仅当 $TERM 值以“xterm…”开头时才启用括号粘贴模式;如果您将 $TERM 设置为其他值,您可能需要修改该测试以包含您的 $TERM 值。或者,您可以完全省略测试,因为它并不是绝对必要的 - 它只是想小心不要做一些可能与其他终端类型不兼容的事情。
在终端中,这适用于所有各种粘贴命令以及拖放。
† 终端有一个主屏幕和一个“备用”屏幕。每个屏幕都有自己的内容和状态。备用屏幕中的文本不会向上滚动到回滚日志中。它通常由接管整个屏幕控制的程序使用,因此被称为“全屏”程序。例如,这包括 vim、emacs、less 和 top。
正如 @DenilsonSáMaia 所指出的,这个答案已被打包到一个插件中;不过,从 Vim 8 开始它已经过时了:
https://github.com/ConradIrwin/vim-bracketed-paste
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:
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
在 vim 中:
Within vim:
另一种方法是,假设您已正确设置系统剪贴板,则执行
此操作将从系统剪贴板粘贴。
Another way to do this, assuming you have your system clipboard set up properly is to do
This will paste from the system clipboard.
除了其他答案之外,如果您想要一种快速切换粘贴模式的方法,请添加
到您的 .vimrc 中。现在您可以通过按 F2(或您选择的任何键)来切换粘贴模式。
In addition to the other answers, if you want a quick way to toggle paste mode, add
to your .vimrc. Now you can toggle paste mode by pressing F2 (or whatever key you choose).
在vim中
:当你想禁用它时
In vim
when you want to disable it
在终端内工作时, vim-bracketed-paste vim 插件将自动处理粘贴,而无需粘贴之前或之后需要任何击键。
这适用于终端、iTerm2 和任何支持括号粘贴模式的“现代”x-term 兼容终端。作为一个额外的好处,它也适用于 tmux 会话。我在连接到 Linux 服务器并使用 tmux 的 Mac 上成功地将它与 iTerm2 一起使用。
该插件基本上是 @Chris Page 在他的答案中列出的功能的打包版本。
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.