代码之家  ›  专栏  ›  技术社区  ›  Jonatan Littke Yukiup

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

  •  16
  • Jonatan Littke Yukiup  · 技术社区  · 16 年前

    :set paste 否则,tab将随着每行的增加而增加(你们都看到了!)。

    尽管 set smartindent ,导致光标跳到新行的开头,而不是正确的缩进处。所以我只想为这个实例启用它。

    我使用Mac,sshing到一个带有Vim的Debian机器,因此使用 命令 .

    4 回复  |  直到 8 年前
        1
  •  7
  •   Cascabel    16 年前

    我不使用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 .

        2
  •  3
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    我的.vimrc中有以下内容:

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

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

    更新:

    Jefromi发布了一个更好的解决方案。我把它修好了一点

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

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

        3
  •  1
  •   jamessan    16 年前

    你是对的,你应该只启用 'paste' 当你需要的时候。它不仅仅影响缩进。你可以阅读它影响到的一切 documentation . 一个相关的选项,对于简化 'pastetoggle'

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

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

        4
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    iTerm2's "paste slowly" mode ,默认值是将要粘贴的数据分成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
    

    这也有缺点,但在大多数情况下都是可行的。

        5
  •  0
  •   D. Ben Knoble iamnotsam    6 年前

    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.
    
    推荐文章