将文本粘贴到 vim 时关闭自动缩进
我正在努力学习Vim。
当我将代码从剪贴板粘贴到文档中时,我在每个新行的开头都会出现额外的空格:
line
line
line
我知道您可以关闭自动缩进,但我无法让它工作,因为我有一些其他设置冲突或其他设置(这在我的 .vimrc 中看起来非常明显,但当我把它们拿出来时似乎并不重要)。
如何在粘贴代码时关闭自动缩进,但在编写代码时仍然有 vim 自动缩进?这是我的 .vimrc 文件:
set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
set bg=dark
set nowrap
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
Mac 用户可以通过直接从粘贴板读取来避免自动格式化:
Mac users can avoid auto formatting by reading directly from the pasteboard with:
我是一名 Python 用户,有时会复制并粘贴到 Vim 中。 (我从 Mac 切换到 Windows WSL),这是困扰我的故障之一。
如果您触摸
script.py
,然后触摸vi script.py
,Vi 将检测到它是一个 Python 脚本,并尝试提供帮助、自动缩进、使用额外缩进进行粘贴等如果你不告诉它是一个 Python 脚本,这种情况就不会发生。但是,如果这种情况已经发生在您身上,那么当您粘贴已经完全缩进的代码时,默认的自动缩进可能会是一场噩梦(请参阅下面的倾斜梯子形状)。
我尝试了三个选项,结果如下
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 thenvi 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
这是一篇帖子,作者是某人,他弄清楚了如何重新映射粘贴事件以自动打开粘贴模式,然后退后。在 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.
我只是将
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.
将其添加到您的
~/.vimrc
中,您只需在粘贴之前和之后按 F2 即可:Add this to your
~/.vimrc
and you will only have to press F2 before and after pasting:另一个我直到现在才看到的答案:
Another answer I did not see until now:
在终端内工作时, 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.
将其粘贴到您的 ~/.vimrc 中并感到高兴:
编辑:根据反射,
:r !cat
是一种更好的方法,因为它简短、语义化,并且不需要自定义 vimrc。用它来代替!Stick this in your ~/.vimrc and be happy:
Edit: on reflection,
:r !cat
is a far better approach since it's short, semantic, and requires no custom vimrc. Use that instead!另一种粘贴方法是在插入模式下通过
并删除寄存器(此处为全局寄存器)的内容。请参阅::hi_ctrl-r
和h i_CTRL-R_CTRL-O
。来自vim帮助文档:
因此,要将内容粘贴到 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
andh i_CTRL-R_CTRL-O
.From the vim help documentation:
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
.尽管
:pastetoggle
或:paste
和:nopaste
应该可以正常工作(如果实现 - 它们并不总是像我们从讨论中看到的那样)我强烈建议使用直接方法"+p
或"*p
粘贴并使用"+r
或"*r< /code>:
Vim 可以访问十种类型的寄存器(
:help registers
),提问者对部分中的quotestar
和quoteplus
感兴趣:help x11-selection
进一步阐明了*
和+
的区别: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 inquotestar
andquoteplus
from section:help x11-selection
further clarifies the difference of*
and+
:从 vim 中:
]p
从外部:
"*]p
或"+]p
From vim:
]p
From outside:
"*]p
or"+]p
这对我有用(+寄存器的情况,我使用的就像 aps 之间的交换缓冲区):
This works for me ( case for + register, what i use like exchange buffer between aps ):
这个问题已经得到解答,但我想我也可以添加我自己的解决方案:
如果您只是想禁用自动缩进系统,对于每种文件类型(基本上,完全禁用自动缩进功能),您可以执行以下操作:以下:
indent.vim
文件:sudo mv /usr/share/vim/vim81/indent.vim /usr/share/vim/vim81/indent.vim.orig
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:
indent.vim
file:sudo mv /usr/share/vim/vim81/indent.vim /usr/share/vim/vim81/indent.vim.orig
indent.vim
file:sudo touch /usr/share/vim/vim81/indent.vim
如果你使用的是 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
原生粘贴/括号粘贴是自
vim 8
(2016 年发布)以来最好、最简单的方法。它甚至可以通过 ssh 运行! (括号内的粘贴适用于 Linux 和 Mac,但不适用于 Windows Git Bash)确保您有 vim 8+(您不需要
+clipboard
或+xterm_clipboard
选项)。vim --版本 | head -1
只需在正常模式下使用操作系统本机粘贴命令(例如
ctrl+shift+V
或cmd+V
)即可。请勿按i
进入插入模式。测试
将其输出(带有制表符缩进的 2 行)复制(
ctrl+shift+C
或cmd+C
)到系统剪贴板:echo -e '\ta\n\tb'
使用自动缩进启动干净的 vim 8+:
vim -u NONE --noplugin -c 'set autoindent'
在正常模式下从系统剪贴板粘贴(
ctrl+shift+V
或cmd+V
)。请勿按i
进入插入模式。a
和b
应与单个制表符缩进对齐。您甚至可以在 ssh-ing 到远程计算机时执行此操作(远程计算机需要 vim 8+)。现在尝试旧方法,它将使用额外的制表符自动缩进第二行:按
i
进入插入模式。然后使用ctrl+shift+V
或cmd+V
进行粘贴。a
和b
现在未对齐。安装 Vim 8
Ubuntu 18.04 - 默认附带 Vim 8.
Ubuntu 16.04 - 从 PPA 安装。
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)Make sure you have vim 8+ (you don't need the
+clipboard
or+xterm_clipboard
options).vim --version | head -1
Simply use the OS native paste command (e.g.
ctrl+shift+V
orcmd+V
) in Normal Mode. Do not pressi
for Insert Mode.Test
Copy (
ctrl+shift+C
orcmd+C
) the output of this (2 lines with a tab indent) to the system clipboard:echo -e '\ta\n\tb'
Launch a clean vim 8+ with autoindent:
vim -u NONE --noplugin -c 'set autoindent'
Paste from the system clipboard (
ctrl+shift+V
orcmd+V
) in Normal Mode. Do not pressi
for Insert Mode. Thea
andb
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+).Now try the old way, which will autoindent the second line with an extra tab: Press
i
for Insert Mode. Then paste usingctrl+shift+V
orcmd+V
. Thea
andb
are misaligned now.Installing Vim 8
Ubuntu 18.04 - comes with Vim 8 by default.
Ubuntu 16.04 - install from a PPA.
brew install vim
请阅读这篇文章:切换代码粘贴自动缩进
Please read this article: Toggle auto-indenting for code paste
我知道快速进入粘贴插入模式进行一次性粘贴的最快方法是 tpope 的 未受损,大概具有
yo
和yO
“你打开”的助记符。它们仅记录在他的 vimdoc 中,如下所示: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
andyO
, presumably mnemonics for “you open”. They’re only documented in his vimdoc, as:以下 vim 插件通过其“括号粘贴”模式自动处理该问题: https://github.com/wincent/terminus
The following vim plugin handles that automatically through its "Bracketed Paste" mode: https://github.com/wincent/terminus
VimL:
Neovim lua:
VimL:
Neovim lua:
遗憾的是,我发现提到的 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.
如果您使用
v8.2
以上版本的 vim,可以使用:help tmux-integration
查看。If you use the vim above
v8.2
, you can check with:help tmux-integration
.如果你想永远关闭自动缩进,你可以删除这个文件
/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 addset paste
to yourvimrc
file更新:这里有更好的答案:https://stackoverflow.com/a/38258720/62202
要在粘贴代码时关闭自动缩进,有一个特殊的“粘贴”模式。
输入
然后粘贴您的代码。请注意,工具提示中的文本现在显示为
-- INSERT(粘贴)--
。粘贴代码后,关闭粘贴模式,以便您键入时自动缩进再次正常工作。
但我总觉得这样很麻烦。这就是为什么我映射
,以便它可以在编辑文本时在粘贴和 nopaste 模式之间切换!我将其添加到.vimrc
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
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.
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
为了避免粘贴时出现不良效果,需要设置一个选项:
.vimrc 中的一个有用命令是
set Pastetoggle=
或其他一些按钮,以便轻松在之间切换粘贴和不粘贴。To avoid undesired effects while pasting, there is an option that needs to be set:
A useful command to have in your .vimrc is
set pastetoggle=<F10>
or some other button, to easily toggle between paste and nopaste.我通常使用
:r! cat
,然后粘贴(shift + insert)
内容,然后CTRL+D
。无需启用 &禁用,直接使用。
I usually use
:r! cat
and then paste( shift + insert )
the content, andCTRL+D
.No need to enable & disable, direct usage.
如果您在本地工作,可以使用以下键序列从系统剪贴板粘贴:
"+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.虽然使用
paste/nopaste/pastetoggle
设置粘贴模式完全没问题,但您仍然需要在粘贴之前手动启用粘贴模式并在粘贴后禁用粘贴模式。作为一个懒惰的人,下面是我迄今为止找到的最好的解决方案,它在粘贴时自动切换粘贴模式。来源: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.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