当我从剪贴板粘贴东西时,它们通常(总是)是多行的,在这些情况下(并且仅在那些情况下),我希望:set paste
被触发,因为否则标签会随着每一行而增加(你已经看过了!)。
虽然问题:set paste
在于它与 的行为不佳set smartindent
,导致光标跳转到新行的开头而不是正确的缩进处。所以我只想为这个实例启用它。
我正在使用 Mac,使用 Vim 连接到 Debian 机器,因此使用cmd+以插入模式粘贴v。
我不使用 mac,但我相信我在这里有前缀:<D-v>
应该意味着 cmd-v。对于插入模式:
:imap <D-v> ^O:set paste<Enter>^R+^O:set nopaste<Enter>
或者真的,就这样做:
:imap <D-V> ^O"+p
^O 和 ^R 是文字 control-O 和 control-R,您可以使用 ^V^O (control-v control-o) 和 ^V^R (control-v control-r) 键入。插入模式下的 Control-O 允许您执行一个命令然后返回插入模式;在这里您可以使用它从剪贴板寄存器中放置。
当我测试它们映射到不同的键时,这对我有用,所以你应该准备好了。
不处于插入模式时无需映射任何内容;你可以使用"+p
.
我的 .vimrc 中有以下内容:
inoremap <S-Insert> <ESC>:setl paste<CR>gi<C-R>+<ESC>:setl nopaste<CR>gi
gi
是在与上次在当前缓冲区中停止插入模式的位置相同的位置启动插入模式。
更新:
Jefromi 发布了一个更好的解决方案。我稍微修改了一下
inoremap <S-Insert> <ESC>"+p`]a
它插入剪贴板文本并将光标放在它后面。
你是对的,你应该只'paste'
在需要时启用它。它不仅仅影响缩进。您可以在其文档中阅读它影响的所有内容。一个对简化使用非常有用的相关选项'paste'
是'pastetoggle'。
如果您使用 X-forwarding 和可以正确传达鼠标操作的终端,您还可以利用“鼠标”选项。使用:set mouse=a
时,Vim 会知道鼠标在做什么,因此当它通过鼠标中键单击接收到多行粘贴时,它不会执行自动缩进。
即使没有鼠标功能,X-forwarding 也会有所帮助,因为当从剪贴板或选择寄存器("+
和"*
分别)手动粘贴时,Vim 会做同样的事情。
这应该可以用 Vim 脚本解决。(我讨厌 Vim 脚本,所以它必须是一个更严重的问题才能让我自己解决它。)即使使用iTerm2 的“缓慢粘贴”模式,默认是将要粘贴的数据分成 16 字节块和每 0.125 秒发送一个。因此,您应该能够以编程方式检测 16 字节的“击键”块并对其进行处理。
在看起来像这样的伪代码中:
if too_fast_too_be_human():
set('pastemode', True)
else
set('pastemode', False)
# where either
def too_fast_too_be_human
char_threshold = 16
return len(input_buffer) > char_threshold
# or
def too_fast_too_be_human
static byte_times = []
char_threshold = 16
time_threshold = 0.125
byte_times.append(now())
while(len(byte_times) > char_threshold):
byte_times.unshift()
return (byte_times[-1] - byte_times[0]) < time_threshold
这有一些弱点,但它适用于大多数情况。
我们可以粘贴(插入模式)而不用弄乱缩进
Ctrl-r Ctrl-o Register
Ctrl-r Ctrl-o +
Ctrl-r Ctrl-o *
Ctrl-r Ctrl-o 0
CTRL-R CTRL-O {0-9a-z"%#*+/:.-=} *i_CTRL-R_CTRL-O*
Insert the contents of a register literally and don't
auto-indent. Does the same as pasting with the mouse
"MiddleMouse". When the register is linewise this will
insert the text above the current line, like with `P`.
Does not replace characters!
The '.' register (last inserted text) is still inserted as
typed.