从剪贴板粘贴并自动切换“:设置粘贴”

发布于 2024-08-28 00:33:14 字数 314 浏览 6 评论 0原文

当我从剪贴板粘贴内容时,它们通常(总是)是多行的,在这些情况下(并且仅在这些情况下),我希望触发 :set Paste ,因为否则制表符会随着每一行的增加而增加(你们都已经看到了!)。

虽然 :set Paste 的问题是它与 set smartindent 的表现不佳,导致光标跳到新行的开头而不是正确的位置缩进。所以我只想在这个实例中启用它。

我使用 Mac,使用 Vim sshing 到 Debian 机器,然后使用 cmd + v 在插入模式下粘贴。

When I paste things from the clipboard, they're normally (always) multilined, and in those cases (and those cases only), I'd like :set paste to be triggered, since otherwise the tabbing will increase with each line (you've all seen it!).

Though the problem with :set paste is that it doesn't behave well with set smartindent, causing the cursor to jump to the beginning of a new line instead of at the correct indent. So I'd like to enable it for this instance only.

I'm using Mac, sshing to a Debian machine with Vim, and thus pasting in Insert mode using cmd + v.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝天 2024-09-04 00:33:14

我不使用 Mac,但我相信我这里有前缀: 应该表示 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 (控制-v 控制-r)。插入模式下的 Control-O 允许您执行一个命令然后返回插入模式;在这里您可以使用它从剪贴板寄存器中输入。

当我测试它们映射到不同的键时,这对我有用,所以你应该已经准备好了。

不处于插入模式时无需映射任何内容;您只需使用"+p即可。

I don't use a mac, but I believe I have the prefix right here: <D-v> should mean cmd-v. For insert mode:

:imap <D-v> ^O:set paste<Enter>^R+^O:set nopaste<Enter>

or really, just do this:

:imap <D-V> ^O"+p

The ^O and ^R are literal control-O and control-R, which you can type with ^V^O (control-v control-o) and ^V^R (control-v control-r). Control-O in insert mode allows you to execute one command then return to insert mode; here you can use it to put from the clipboard register.

This worked for me when I tested them mapped to a different key, so you should be all set.

There's no need to map anything when not in insert mode; you can just use "+p.

少跟Wǒ拽 2024-09-04 00:33:14

我的 .vimrc 中有以下内容:

inoremap <S-Insert> <ESC>:setl paste<CR>gi<C-R>+<ESC>:setl nopaste<CR>gi

gi 是在当前缓冲区中上次停止插入模式的相同位置启动插入模式。

更新:

Jefromi 发布了更好的解决方案。我对它做了一些修改,

inoremap <S-Insert> <ESC>"+p`]a

它插入剪贴板文本并将光标放在它后面。

I have the following in my .vimrc:

inoremap <S-Insert> <ESC>:setl paste<CR>gi<C-R>+<ESC>:setl nopaste<CR>gi

gi is to start insert mode in the same position as where insert mode was stopped last time in the current buffer.

Update:

Jefromi posted a better solution. I have tinkered it a bit

inoremap <S-Insert> <ESC>"+p`]a

It inserts clipboard text and places the cursor right after it.

一杆小烟枪 2024-09-04 00:33:14

你是对的,你应该只在需要时启用 'paste' 。它不仅仅影响缩进。您可以在其文档中阅读它影响的所有内容。一个对于简化 'paste' 使用非常有用的相关选项是 “粘贴切换”

如果您使用的是 X-forwarding 和可以正确传达鼠标操作的终端,您还可以利用 '鼠标'选项。使用 :set mouse=a ,Vim 可以知道鼠标在做什么,因此当它通过鼠标中键单击接收到多行粘贴时,不会执行自动缩进。

即使没有鼠标功能,X 转发也能有所帮助,因为从剪贴板或选择寄存器手动粘贴时(分别为 "+"*),Vim 也会做同样的事情。

You're right in that you should only enable 'paste' when you need it. It does more than just affect indenting. You can read everything that it affects in its documentation. A related option that is very useful to ease the use of 'paste' is 'pastetoggle'.

If you were using X-forwarding and a terminal that can properly communicate mouse actions, you could also take advantage of the 'mouse' option. With :set mouse=a, Vim is made aware of what the mouse is doing and therefore won't perform automatic indentation when it receives a multi-line paste via a middle-button mouse click.

Even without the mouse capability, X-forwarding could help because Vim will do the same thing when manually pasting from the clipboard or selection registers ("+ and "* respectively).

做个少女永远怀春 2024-09-04 00:33:14

这应该可以用 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

虽然有弱点,但它适用于大多数情况。

This ought to be solvable with a Vim script. (I hate Vim scripting, so it would have to be a much more serious infliction to cause me to solve it myself.) Even with iTerm2's "paste slowly" mode, the default is to break the data to be pasted into 16 byte chunks and send one every 0.125 seconds. Therefore, you should be able to programmatically detect a 16 byte chunk of "keystrokes" and do something about it.

In pseudocode that would look like:

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

There are weaknesses to that, but it would work for most cases.

只等公子 2024-09-04 00:33:14

我们可以粘贴(插入模式),而不会弄乱缩进,使用

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.

We can paste (insert mode) without messing up indentation using

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文