将文本粘贴到 vim 时关闭自动缩进

发布于 2024-08-26 11:05:50 字数 396 浏览 6 评论 0 原文

我正在努力学习Vim。

当我将代码从剪贴板粘贴到文档中时,我在每个新行的开头都会出现额外的空格:

line
  line
    line

我知道您可以关闭自动缩进,但我无法让它工作,因为我有一些其他设置冲突或其他设置(这在我的 .vimrc 中看起来非常明显,但当我把它们拿出来时似乎并不重要)。

如何在粘贴代码时关闭自动缩进,但在编写代码时仍然有 vim 自动缩进?这是我的 .vimrc 文件:

set expandtab  
set tabstop=2  
set shiftwidth=2  
set autoindent  
set smartindent  
set bg=dark  
set nowrap  

I am making the effort to learn Vim.

When I paste code into my document from the clipboard, I get extra spaces at the start of each new line:

line
  line
    line

I know you can turn off auto indent but I can't get it to work because I have some other settings conflicting or something (which look pretty obvious in my .vimrc but don't seem to matter when I take them out).

How do I turn off auto indenting when I paste code but still have vim auto indent when I am writing code? Here is my .vimrc file:

set expandtab  
set tabstop=2  
set shiftwidth=2  
set autoindent  
set smartindent  
set bg=dark  
set nowrap  

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

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

发布评论

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

评论(27

同展鸳鸯锦 2024-09-02 11:05:51

Mac 用户可以通过直接从粘贴板读取来避免自动格式化:

:r !pbpaste

Mac users can avoid auto formatting by reading directly from the pasteboard with:

:r !pbpaste
嘿看小鸭子会跑 2024-09-02 11:05:51

我是一名 Python 用户,有时会复制并粘贴到 Vim 中。 (我从 Mac 切换到 Windows WSL),这是困扰我的故障之一。

如果您触摸 script.py,然后触摸 vi script.py,Vi 将检测到它是一个 Python 脚本,并尝试提供帮助、自动缩进、使用额外缩进进行粘贴等如果你不告诉它是一个 Python 脚本,这种情况就不会发生。

但是,如果这种情况已经发生在您身上,那么当您粘贴已经完全缩进的代码时,默认的自动缩进可能会是一场噩梦(请参阅下面的倾斜梯子形状)。

我尝试了三个选项,结果如下

set paste        # works perfect 
set noai         # still introduced extra whitespace
set noautoindent # still introduced extra whitespace

在此处输入图像描述
输入图像描述这里

I am a Python user who sometimes copy and paste into Vim. (I switched from Mac to Windows WSL) and this was one of the glitches that bothered me.

If you touch a script.py and then vi script.py, Vi will detect it is a Python script and tried to be helpful, autoindent, paste with extra indents, etc. This won't happen if you don't tell it is a Python script.

However, if that is already happening to you, the default autoindent could be a nightmare when you paste already fully indented code (see the tilted ladder shape below).

I tried three options and here are the results

set paste        # works perfect 
set noai         # still introduced extra whitespace
set noautoindent # still introduced extra whitespace

enter image description here
enter image description here

热情消退 2024-09-02 11:05:51

这是一篇帖子,作者是某人,他弄清楚了如何重新映射粘贴事件以自动打开粘贴模式,然后退后。在 MacOSX 上的 tmux/iTerm 中为我工作。

Here is a post by someone who figured out how to remap the paste event to automatically turn paste mode on and then back off. Works for me in tmux/iTerm on MacOSX.

じее 2024-09-02 11:05:51

我只是将 set Clipboard=unnamed 放入我的 .vimrc 中。这使得默认的粘贴缓冲区映射到 X 的剪贴板。

因此,如果我在终端中标记一些文本,我只需按 p 即可将其粘贴到 vim 中。同样,我可以在 vim 中拉出内容(例如 YY 将当前行拉入缓冲区),然后在任何窗口中单击鼠标中键来粘贴它。

我不知道。我觉得超级方便。

I just put set clipboard=unnamed in my .vimrc. That makes the default paste buffer map to X's clipboard.

So, if I mark a bit of text in a terminal, I can simply press p to paste it in vim. Similarly, I can yank things in vim (e.g. YY to yank the current line into the buffer) and middle click in any window to paste it.

I don't know. I find it super convenient.

流心雨 2024-09-02 11:05:51

将其添加到您的 ~/.vimrc 中,您只需在粘贴之前和之后按 F2 即可:

set pastetoggle=<F2>

Add this to your ~/.vimrc and you will only have to press F2 before and after pasting:

set pastetoggle=<F2>
勿忘初心 2024-09-02 11:05:51

另一个我直到现在才看到的答案:

:se paste noai

Another answer I did not see until now:

:se paste noai
狼性发作 2024-09-02 11:05:51

在终端内工作时, vim-bracketed-paste vim 插件将自动处理粘贴,无需粘贴之前或之后的任何击键。

它的工作原理是检测 括号粘贴模式,这是由“现代”x-term 兼容发送的转义序列iTerm2、gnome-terminal 等终端以及其他使用 libvte 的终端。作为一个额外的好处,它也适用于 tmux 会话。我在连接到 Linux 服务器并使用 tmux 的 Mac 上成功地将它与 iTerm2 一起使用。

When working inside a terminal the vim-bracketed-paste vim plugin will automatically handle pastes without needing any keystrokes before or after the paste.

It works by detecting bracketed paste mode which is an escape sequence sent by "modern" x-term compatible terminals like iTerm2, gnome-terminal, and other terminals using libvte. 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.

荒人说梦 2024-09-02 11:05:51

将其粘贴到您的 ~/.vimrc 中并感到高兴:

" enables :Paste to just do what you want
command Paste execute 'set noai | insert | set ai'

编辑:根据反射, :r !cat 是一种更好的方法,因为它简短、语义化,并且不需要自定义 vimrc。用它来代替!

Stick this in your ~/.vimrc and be happy:

" enables :Paste to just do what you want
command Paste execute 'set noai | insert | set ai'

Edit: on reflection, :r !cat is a far better approach since it's short, semantic, and requires no custom vimrc. Use that instead!

誰認得朕 2024-09-02 11:05:51

另一种粘贴方法是在插入模式下通过 并删除寄存器(此处为全局寄存器)的内容。请参阅::hi_ctrl-rh i_CTRL-R_CTRL-O

来自vim帮助文档:

按字面插入寄存器的内容并且不自动缩进。与用鼠标粘贴的效果相同。不替换字符!这 '。' register(最后插入的文本)仍然按键入的方式插入。{Vi 中没有}

因此,要将内容粘贴到 vim 中而不自动缩进,请在大多数 UNIX 系统中使用 *

您可以在 vimrc inoremap 中添加映射,因此您可以使用 * 正常粘贴 * 寄存器的内容,而无需自动缩进。

注意:只有当 vim 使用 clipboard 编译时,这才有效。

Another way to paste is via <C-r> in insert mode and dropping the contents of the register (here the global register). See: :h i_ctrl-r and h i_CTRL-R_CTRL-O.

From the vim help documentation:

Insert the contents of a register literally and don't auto-indent. Does the same as pasting with the mouse. Does not replace characters! The '.' register (last inserted text) is still inserted as typed.{not in Vi}

So to paste contents into vim without auto indent, use <C-r><C-o>* in most unix systems.

You can add a mapping in the your vimrc inoremap <C-r> <C-r><C-o> so you can paste the contents of the * register normally without the auto indent by using <C-r>*.

Note: this only works if vim is compiled with clipboard.

浅暮の光 2024-09-02 11:05:51

尽管 :pastetoggle:paste:nopaste 应该可以正常工作(如果实现 - 它们并不总是像我们从讨论中看到的那样)我强烈建议使用直接方法 "+p"*p 粘贴并使用 "+r"*r< /code>:

Vim 可以访问十种类型的寄存器(:help registers),提问者对部分中的 quotestarquoteplus 感兴趣

  • 选择和删除寄存器"*"+ 和 "~
  • 使用这些寄存器来存储和检索 GUI 的选定文本。
    请参阅 quotestarquoteplus。当剪贴板不可用或不可用时
    工作时,使用未命名的寄存器来代替。对于 Unix 系统,剪贴板
    仅当存在 +xterm_clipboard 功能时才可用。 {Vi 中没有}

    请注意,对于 X11 系统,仅“* 和”+ 之间有区别。

    :help x11-selection进一步阐明了*+的区别:

     quote+ quote+
    

    共有三个记录的 X 选择: PRIMARY(预计
    代表当前的视觉选择 - 就像 Vim 的视觉模式一样),SECONDARY
    (定义不明确)和 CLIPBOARD (预计用于
    剪切、复制和粘贴操作)。

    在这三个寄存器中,Vim 在读写 "* 寄存器时使用 PRIMARY
    (因此,当 X11 选择可用时,Vim 会为
    “自动选择”的“剪贴板”),以及读取和写入“+
    登记。 Vim 无法访问 SECONDARY 选择。

    示例:(假设默认选项值)

    • 在 Vim 的可视模式下选择 URL。转到您的浏览器并单击
      在 URL 文本字段中单击鼠标中键。选定的文本将是
      插入(希望如此!)。注意:在 Firefox 中您可以设置
      about:config 中的 middlemouse.contentLoadURL 首选项为 true,然后
      在窗口中的大多数地方按下鼠标中键时将使用所选的 URL。

    • 通过拖动鼠标在浏览器中选择一些文本。转到 Vim 并
      按鼠标中键:插入所选文本。

    • 在 Vim 中选择一些文本,然后执行“+y”。转到浏览器,在中选择一些文本
      通过用鼠标拖动来创建文本字段。现在使用鼠标右键并
      从弹出菜单中选择“粘贴”。所选文本将被覆盖
      来自 Vim 的文本。
      请注意,“+”寄存器中的文本在制作视觉效果时仍然可用
      选择,这使得“*”寄存器中的其他文本可用。这允许
      覆盖选定的文本。

    Although :pastetoggle or :paste and :nopaste should be working fine (if implemented - they are not always as we can see from the discussion) I highly recomment pasting using the direct approach "+p or "*p and reading with "+r or "*r:

    Vim has acess to ten types of registers (:help registers) and the questioner is interested in quotestar and quoteplus from section

    1. Selection and drop registers "*, "+ and "~

    Use these registers for storing and retrieving the selected text for the GUI.
    See quotestar and quoteplus. When the clipboard is not available or not
    working, the unnamed register is used instead. For Unix systems the clipboard
    is only available when the +xterm_clipboard feature is present. {not in Vi}

    Note that there is only a distinction between "* and "+ for X11 systems.

    :help x11-selection further clarifies the difference of * and +:

                                                      quoteplus quote+
    

    There are three documented X selections: PRIMARY (which is expected to
    represent the current visual selection - as in Vim's Visual mode), SECONDARY
    (which is ill-defined) and CLIPBOARD (which is expected to be used for
    cut, copy and paste operations).

    Of these three, Vim uses PRIMARY when reading and writing the "* register
    (hence when the X11 selections are available, Vim sets a default value for
    'clipboard' of "autoselect"), and CLIPBOARD when reading and writing the "+
    register. Vim does not access the SECONDARY selection.

    Examples: (assuming the default option values)

    • Select an URL in Visual mode in Vim. Go to your browser and click the
      middle mouse button in the URL text field. The selected text will be
      inserted (hopefully!). Note: in Firefox you can set the
      middlemouse.contentLoadURL preference to true in about:config, then the
      selected URL will be used when pressing middle mouse button in most places in the window.

    • Select some text in your browser by dragging with the mouse. Go to Vim and
      press the middle mouse button: The selected text is inserted.

    • Select some text in Vim and do "+y. Go to your browser, select some text in
      a textfield by dragging with the mouse. Now use the right mouse button and
      select "Paste" from the popup menu. The selected text is overwritten by the
      text from Vim.
      Note that the text in the "+ register remains available when making a Visual
      selection, which makes other text available in the "* register. That allows
      overwriting selected text.
    一个人的旅程 2024-09-02 11:05:51

    从 vim 中:]p

    从外部:"*]p"+]p

    From vim: ]p

    From outside: "*]p or "+]p

    计㈡愣 2024-09-02 11:05:51

    这对我有用(+寄存器的情况,我使用的就像 aps 之间的交换缓冲区):

    imap <silent> <S-Insert> <C-O>:set noai<CR><C-R>+<C-O>:set ai<CR>
    

    This works for me ( case for + register, what i use like exchange buffer between aps ):

    imap <silent> <S-Insert> <C-O>:set noai<CR><C-R>+<C-O>:set ai<CR>
    
    坏尐絯 2024-09-02 11:05:51

    这个问题已经得到解答,但我想我也可以添加我自己的解决方案:

    如果您只是想禁用自动缩进系统,对于每种文件类型(基本上,完全禁用自动缩进功能),您可以执行以下操作:以下:

    1. 备份 indent.vim 文件:
      sudo mv /usr/share/vim/vim81/indent.vim /usr/share/vim/vim81/indent.vim.orig
    2. 创建一个新的空 indent.vim 文件:
      sudo touch /usr/share/vim/vim81/indent.vim

    This issue has already been answered, but I though I could also add my own solution:

    If you simply want to disable auto-indent system wise, for every file type (basically, disable the auto-indent feature completely), you can do the following:

    1. Backup the indent.vim file:
      sudo mv /usr/share/vim/vim81/indent.vim /usr/share/vim/vim81/indent.vim.orig
    2. Create a new empty indent.vim file:
      sudo touch /usr/share/vim/vim81/indent.vim
    疯了 2024-09-02 11:05:51

    如果你使用的是 Mac,macvim 似乎可以很好地处理它,而无需切换粘贴。

    brew install macvim --override-system-vim

    If you are on a mac, macvim seems to handle it well without having to toggle paste.

    brew install macvim --override-system-vim

    玻璃人 2024-09-02 11:05:51

    原生粘贴/括号粘贴是自 vim 8(2016 年发布)以来最好、最简单的方法。它甚至可以通过 ssh 运行! (括号内的粘贴适用于 Linux 和 Mac,但不适用于 Windows Git Bash)

    1. 确保您有 vim 8+(您不需要 +clipboard+xterm_clipboard 选项)。

      vim --版本 | head -1

    2. 只需在正常模式下使用操作系统本机粘贴命令(例如ctrl+shift+Vcmd+V)即可。请勿按 i 进入插入模式。


    测试

    1. 将其输出(带有制表符缩进的 2 行)复制(ctrl+shift+Ccmd+C)到系统剪贴板:

      echo -e '\ta\n\tb'

    2. 使用自动缩进启动干净的 vim 8+:

      vim -u NONE --noplugin -c 'set autoindent'

    3. 在正常模式下从系统剪贴板粘贴(ctrl+shift+Vcmd+V)。请勿按 i 进入插入模式。 ab 应与单个制表符缩进对齐。您甚至可以在 ssh-ing 到远程计算机时执行此操作(远程计算机需要 vim 8+)。

    4. 现在尝试旧方法,它将使用额外的制表符自动缩进第二行:按 i 进入插入模式。然后使用 ctrl+shift+Vcmd+V 进行粘贴。 ab 现在未对齐。


    安装 Vim 8

    • Ubuntu 18.04 - 默认附带 Vim 8.

    • Ubuntu 16.04 - 从 PPA 安装

      sudo add-apt-repository ppa:jonathonf/vim
      须藤apt更新
      sudo apt安装vim
      
    • Mac:brew 安装 vim

    Native paste / bracketed paste is the best and simplest way since vim 8 (released in 2016). It even works over ssh! (Bracketed paste works on Linux and Mac, but not Windows Git Bash)

    1. Make sure you have vim 8+ (you don't need the +clipboard or +xterm_clipboard options).

      vim --version | head -1

    2. Simply use the OS native paste command (e.g. ctrl+shift+V or cmd+V) in Normal Mode. Do not press i for Insert Mode.


    Test

    1. Copy (ctrl+shift+C or cmd+C) the output of this (2 lines with a tab indent) to the system clipboard:

      echo -e '\ta\n\tb'

    2. Launch a clean vim 8+ with autoindent:

      vim -u NONE --noplugin -c 'set autoindent'

    3. Paste from the system clipboard (ctrl+shift+V or cmd+V) in Normal Mode. Do not press i for Insert Mode. The a and b should be aligned with a single tab indent. You can even do this while ssh-ing to a remote machine (the remote machine will need vim 8+).

    4. Now try the old way, which will autoindent the second line with an extra tab: Press i for Insert Mode. Then paste using ctrl+shift+V or cmd+V. The a and b are misaligned now.


    Installing Vim 8

    迷雾森÷林ヴ 2024-09-02 11:05:51

    请阅读这篇文章:切换代码粘贴自动缩进

    有些人喜欢通过以下 vimrc 替代方案在状态行中显示的视觉反馈:

    nnoremap <F2> :set invpaste paste?<CR>
    set pastetoggle=<F2>
    set showmode
    

    Please read this article: Toggle auto-indenting for code paste

    Some people like the visual feedback shown in the status line by the following alternative for your vimrc:

    nnoremap <F2> :set invpaste paste?<CR>
    set pastetoggle=<F2>
    set showmode
    
    茶色山野 2024-09-02 11:05:51

    我知道快速进入粘贴插入模式进行一次性粘贴的最快方法是 tpope 的 未受损,大概具有 yoyO “你打开”的助记符。它们仅记录在他的 vimdoc 中,如下所示:

    尚未为“粘贴”提供切换开关,因为典型用例
    单独插入的包装非常浪费:您切换两次,但是
    您只需粘贴一次(YOPO)。相反,按 yo 或 yO 来调用 o 或 O
    “粘贴”已经设置。离开插入模式会自动设置“nopaste”。

    The fastest way I’m aware of to quickly go to paste-insert mode for a one-shot paste is tpope’s unimpaired, which features yo and yO, presumably mnemonics for “you open”. They’re only documented in his vimdoc, as:

    A toggle has not been provided for 'paste' because the typical use case of
    wrapping of a solitary insertion is so wasteful: You toggle twice, but
    you only paste once (YOPO). Instead, press yo or yO to invoke o or O with
    'paste' already set. Leaving insert mode sets 'nopaste' automatically.

    三月梨花 2024-09-02 11:05:51

    以下 vim 插件通过其“括号粘贴”模式自动处理该问题: https://github.com/wincent/terminus

    设置“括号粘贴”模式,这意味着您可以忘记手动设置“粘贴”选项,只需在任何模式下粘贴即可。

    The following vim plugin handles that automatically through its "Bracketed Paste" mode: https://github.com/wincent/terminus

    Sets up "Bracketed Paste" mode, which means you can forget about manually setting the 'paste' option and simply go ahead and paste in any mode.

    征棹 2024-09-02 11:05:51

    VimL:

    inoremap <silent> <S-Insert> <Cmd>set paste<CR><C-r>+<Cmd>set nopaste<CR>
    

    Neovim lua:

    vim.keymap.set("i", "<S-Insert>", [[<Cmd>set paste<CR><C-r>+<Cmd>set nopaste<CR>]], { noremap = true, silent = true })
    

    VimL:

    inoremap <silent> <S-Insert> <Cmd>set paste<CR><C-r>+<Cmd>set nopaste<CR>
    

    Neovim lua:

    vim.keymap.set("i", "<S-Insert>", [[<Cmd>set paste<CR><C-r>+<Cmd>set nopaste<CR>]], { noremap = true, silent = true })
    
    各自安好 2024-09-02 11:05:51

    遗憾的是,我发现提到的 vim 插件不能与 iTerm2 3.0.15 一起使用(公平地说,我不知道这在旧版本上是否会损坏) - 但我发现了这个 hack。

    映射命令 -p 进行粘贴并使用 iTerm2 vim 键。显然这仅适用于 iTerm2。

    它是如何运作的。我使用“jk”进入转义模式,因此您还需要:

    :inoremap jk

    在您的 .vimrc 中。

    然后它只是调用 P 进入粘贴模式,“+p 从剪贴板粘贴,然后 P 禁用粘贴模式。hth.

    在此处输入图像描述

    Sadly I found the vim plugin mentioned not to be working with iTerm2 3.0.15 (to be fair I don't know if this broke on older versions) - but I found this hack instead.

    Map command-p to do the paste and using iTerm2 vim keys. Obviously this only works for iTerm2.

    How it works. I use "jk" to enter escape mode so you will also need:

    :inoremap jk

    in your .vimrc.

    Then it just invokes P to enter paste mode, "+p to paste from the clipboard and then P to disable paste mode. hth.

    enter image description here

    甜嗑 2024-09-02 11:05:51

    如果您使用 v8.2 以上版本的 vim,可以使用 :help tmux-integration 查看。

    如果您在 tmux 中运行 Vim 时遇到问题,这里有一些提示。
    如果某些内容不起作用,您可以注释掉部分(这可能取决于
    tmux 运行的终端):

    if !has('gui_running') && &term =~ '^\%(screen\|tmux\)'
        " Better mouse support, see  :help 'ttymouse'
        set ttymouse=sgr
    
        " Enable true colors, see  :help xterm-true-color
        let &termguicolors = v:true
        let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
        let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
    
        " Enable bracketed paste mode, see  :help xterm-bracketed-paste
        let &t_BE = "\<Esc>[?2004h"
        let &t_BD = "\<Esc>[?2004l"
        let &t_PS = "\<Esc>[200~"
        let &t_PE = "\<Esc>[201~"
    
        " Enable focus event tracking, see  :help xterm-focus-event
        let &t_fe = "\<Esc>[?1004h"
        let &t_fd = "\<Esc>[?1004l"
    
        " Enable modified arrow keys, see  :help xterm-modifier-keys
        execute "silent! set <xUp>=\<Esc>[@;*A"
        execute "silent! set <xDown>=\<Esc>[@;*B"
        execute "silent! set <xRight>=\<Esc>[@;*C"
        execute "silent! set <xLeft>=\<Esc>[@;*D"
    endif
    

    If you use the vim above v8.2, you can check with :help tmux-integration.

    If you experience issues when running Vim inside tmux, here are a few hints.
    You can comment-out parts if something doesn't work (it may depend on the
    terminal that tmux is running in):

    if !has('gui_running') && &term =~ '^\%(screen\|tmux\)'
        " Better mouse support, see  :help 'ttymouse'
        set ttymouse=sgr
    
        " Enable true colors, see  :help xterm-true-color
        let &termguicolors = v:true
        let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
        let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
    
        " Enable bracketed paste mode, see  :help xterm-bracketed-paste
        let &t_BE = "\<Esc>[?2004h"
        let &t_BD = "\<Esc>[?2004l"
        let &t_PS = "\<Esc>[200~"
        let &t_PE = "\<Esc>[201~"
    
        " Enable focus event tracking, see  :help xterm-focus-event
        let &t_fe = "\<Esc>[?1004h"
        let &t_fd = "\<Esc>[?1004l"
    
        " Enable modified arrow keys, see  :help xterm-modifier-keys
        execute "silent! set <xUp>=\<Esc>[@;*A"
        execute "silent! set <xDown>=\<Esc>[@;*B"
        execute "silent! set <xRight>=\<Esc>[@;*C"
        execute "silent! set <xLeft>=\<Esc>[@;*D"
    endif
    
    折戟 2024-09-02 11:05:51

    如果你想永远关闭自动缩进,你可以删除这个文件 /usr/share/vim/vim82/indent.vim 并将 set Paste 添加到你的 vimrc 文件

    If you want to turn off autoindent forever,you can remove this file /usr/share/vim/vim82/indent.vim and add set paste to your vimrc file

    痴梦一场 2024-09-02 11:05:50

    更新:这里有更好的答案:https://stackoverflow.com/a/38258720/62202

    要在粘贴代码时关闭自动缩进,有一个特殊的“粘贴”模式。

    输入

    :set paste
    

    然后粘贴您的代码。请注意,工具提示中的文本现在显示为 -- INSERT(粘贴)--

    粘贴代码后,关闭粘贴模式,以便您键入时自动缩进再次正常工作。

    :set nopaste
    

    但我总觉得这样很麻烦。这就是为什么我映射 ,以便它可以在编辑文本时在粘贴和 nopaste 模式之间切换我将其添加到 .vimrc

    set pastetoggle=<F3>
    

    Update: Better answer here: https://stackoverflow.com/a/38258720/62202

    To turn off autoindent when you paste code, there's a special "paste" mode.

    Type

    :set paste
    

    Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) --.

    After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.

    :set nopaste
    

    However, I always found that cumbersome. That's why I map <F3> such that it can switch between paste and nopaste modes while editing the text! I add this to .vimrc

    set pastetoggle=<F3>
    
    谁对谁错谁最难过 2024-09-02 11:05:50

    为了避免粘贴时出现不良效果,需要设置一个选项:

    set paste
    

    .vimrc 中的一个有用命令是 set Pastetoggle= 或其他一些按钮,以便轻松在之间切换粘贴和不粘贴。

    To avoid undesired effects while pasting, there is an option that needs to be set:

    set paste
    

    A useful command to have in your .vimrc is set pastetoggle=<F10> or some other button, to easily toggle between paste and nopaste.

    若相惜即相离 2024-09-02 11:05:50

    我通常使用 :r! cat,然后粘贴(shift + insert)内容,然后CTRL+D

    无需启用 &禁用,直接使用。

    I usually use :r! cat and then paste ( shift + insert ) the content, and CTRL+D.

    No need to enable & disable, direct usage.

    姐不稀罕 2024-09-02 11:05:50

    如果您在本地工作,可以使用以下键序列从系统剪贴板粘贴:

    "+p

    这是正确的 vim 命令,因此无需担心进入插入模式或先关闭自动缩进 当然,

    如果您正在远程工作(例如通过 SSH 进行控制台),那么这将不起作用,您应该使用 :set noai,插入模式,粘贴到控制台,离开插入模式,:set ai 路线,如其他地方所述。

    If you are working locally, you can paste from the system clipboard with the key sequence:

    "+p

    This is a proper vim command, so no need to worry about entering an insert mode or switching off autoindent first.

    Of course if you are working remotely (console over SSH, for example) then this won't work and you should go the :set noai, insert mode, paste into console, leave insertmode, :set ai route as described elsewhere.

    冷…雨湿花 2024-09-02 11:05:50

    虽然使用 paste/nopaste/pastetoggle 设置粘贴模式完全没问题,但您仍然需要在粘贴之前手动启用粘贴模式并在粘贴后禁用粘贴模式。作为一个懒惰的人,下面是我迄今为止找到的最好的解决方案,它在粘贴时自动切换粘贴模式。

    这里有一个小技巧,使用终端的括号粘贴模式来
    粘贴时自动设置/取消设置 Vim 的粘贴模式。放置以下
    在你的 .vimrc 中:

    let &t_SI .= "\[?2004h"
    让 &t_EI .= "\[?2004l"
    
    inoremap <特殊> <表达式> [200~ XTermPasteBegin()
    
    功能! XTermPasteBegin()
      设置粘贴切换=[201~
      设置粘贴
      返回 ””
    结束函数
    

    现在您可以在不显式打开/关闭粘贴模式的情况下进行粘贴 - 这是
    自动为您处理。

    来源:Coderwall

    注意:此解决方案不适用于 WSL(适用于 Linux 的 Windows 10 子系统)。如果有人有 WSL 的解决方案,请更新此答案或将其添加到评论中。

    Tmux 如果使用 tmux,则声明需要双重转义。此代码也在 编码墙

    While setting the paste mode with paste/nopaste/pastetoggle is perfectly fine, you still have to manually enable paste mode before pasting and disable paste mode after pasting. Being the lazy person that I am, below is the best solution that I've found so far, which automatically toggles the paste mode when you paste.

    Here's a little trick that uses terminal's bracketed paste mode to
    automatically set/unset Vim's paste mode when you paste. Put following
    in your .vimrc:

    let &t_SI .= "\<Esc>[?2004h"
    let &t_EI .= "\<Esc>[?2004l"
    
    inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
    
    function! XTermPasteBegin()
      set pastetoggle=<Esc>[201~
      set paste
      return ""
    endfunction
    

    Now you can paste without explicitly turning paste mode on/off - it is
    handled automatically for you.

    Source: Coderwall

    Note: This solution doesn't work in WSL (Windows 10 Subsystem for Linux). If anyone has a solution for WSL, please update this answer or add it in the comments.

    Tmux If using tmux, then the declarations need to be double escaped. The code for this is also in Coderwall

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