当我从剪贴板粘贴东西时,它们通常(总是)有多行,在这种情况下(也仅在这种情况下),我希望触发 :set paste
,否则制表符将随每一行增加而增加(大家都看到了!)。
但是,使用 :set paste
的问题在于它与 set smartindent
行为不兼容,导致光标跳转到新行的开头而不是正确的缩进位置。所以我只想在这种情况下启用它。
我使用 Mac,在 Vim 上通过 SSH 连接到 Debian 机器,因此使用 cmd + v 在插入模式下进行粘贴。
当我从剪贴板粘贴东西时,它们通常(总是)有多行,在这种情况下(也仅在这种情况下),我希望触发 :set paste
,否则制表符将随每一行增加而增加(大家都看到了!)。
但是,使用 :set paste
的问题在于它与 set smartindent
行为不兼容,导致光标跳转到新行的开头而不是正确的缩进位置。所以我只想在这种情况下启用它。
我使用 Mac,在 Vim 上通过 SSH 连接到 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
"+p
... 根据您的操作系统,您可能需要将其更改为 "*p
。对于我的电脑(osx),我需要使用第二个版本。 - agbodike我在我的 .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和一个可以正确通信鼠标操作的终端,你也可以利用'mouse'选项。通过: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.