如何用vim粘贴新行?
我经常需要在 vim 中将一些内容粘贴到新行上。我通常做的是:
o<Esc>p
插入一个新行并使我进入插入模式,然后退出插入模式,最后粘贴。
三个按键。效率不太高。还有更好的想法吗?
I often have to paste some stuff on a new line in vim. What I usually do is:
o<Esc>p
Which inserts a new line and puts me in insertion mode, than quits insertion mode, and finally pastes.
Three keystrokes. Not very efficient. Any better ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
在
:help p
之后不久,它说:不幸的是,它并不比您当前的解决方案短,除非您按照不同答案中的建议将其与某些键盘映射结合起来。例如,您可以将其映射到任何键(甚至
p
):Shortly after
:help p
it says:Unfortunately it’s not shorter than your current solution unless you combined it with some keyboard map as suggested in a different answer. For instance, you can map it to any key (even
p
):选项:
1) 使用
yy
拉出整行(包括行尾字符)。然后,p
会将这一行粘贴到当前行之后的新行上,并且P
(Shift-P) 将粘贴到当前行上方。2) 进行映射:那么它只有一两个键:
3) 映射的函数版本(实际上不必要,但只是为了完整性):
Options:
1) Use
yy
to yank the whole line (including the end of line character).p
will then paste the line on a new line after the current one andP
(Shift-P) will paste above the current line.2) Make a mapping: then it's only one or two keys:
3) The function version of the mapping (unnecessary really, but just for completeness):
您可以使用
在插入模式下粘贴缓冲区,后跟要粘贴的缓冲区的名称。默认缓冲区是"
,所以您会这样做我发现我经常使用
"
并将其绑定到
在我的 vimrc 中:You can paste a buffer in insert mode using
<C-R>
followed by the name of the buffer to paste. The default buffer is"
, so you would doI found that I use
<C-R>"
very often and bound that to<C-F>
in my vimrc:这仍然使用三个击键,但我发现它比 Esc 更容易:
o
由于按 o 后处于插入模式,因此 Alt 修饰符将允许您使用命令,就好像你不是。
This still uses three keystrokes, but I find it easier than Esc:
o<Alt-p>
Since you're in insert mode after hitting o, the Alt modifier will allow you to use a command as if you weren't.
使用此插件: https://github.com/tpope/vim-unimpaired
] p
粘贴到下面行[p
粘贴到上面行优点:
周围是
Using this plugin: https://github.com/tpope/vim-unimpaired
]p
pastes on the line below[p
pastes on the line aboveadvantages:
around it
就我个人而言,我已经像这样映射了 Enter (CR):
...基于 这篇 Vim Wikia 文章。
这样我可以直接从正常模式创建换行符,并将其与想要粘贴到下面的换行符结合起来:
您也可以在上面的 nmap 中跳过 k ,具体取决于您喜欢的功能来自Enter,所以它只是
p
。我还将 jj 映射到 Esc,这在这种情况下也有帮助。 Esc 离主行太远了,不知道它在 vim 中的重要性。
不比其他解决方案短,但我确实认为它比其中一些解决方案感觉不那么笨重,而且它还有其他用途。
Personally I've nmapped Enter (CR) like this:
...based on this Vim Wikia article.
This way I can make newlines directly from normal mode, and combining this with wanting to paste to a newline below I'd do:
You could also skip k in the nmap above, depending on what functionality you prefer from Enter, so it would just be
<CR>p
.I've also imapped jj to Esc, which would also assist in this case. Esc is way too far away from the home row for how significant it is in vim.
Not shorter than the other solutions, but I do think it feels less clunky than some of them, and it has other uses too.
如果您想保持插入模式,可以执行
o ctrl+o p
o
– 插入模式并转到新行ctrl+o
– 运行单个命令就像在正常模式下一样
p
– Paste需要 3 次击键,但您仍处于插入模式,并且
o ctrl+o
速度相当快,因此我个人将其视为 2.5 次击键。If you wanted to stay in the insert mode, you can do
o ctrl+o p
o
– insert mode and go to the new linectrl+o
– run a single commandlike in normal mode
p
– pasteIt's three keystrokes but you stay in insert mode and also
o ctrl+o
is quite fast so I personally treat it as 2.5 keystrokes.如果您要复制整行然后粘贴整行,请首先使用
Y
来拉出该行或多行,包括换行符,然后使用p
进行粘贴。您还可以使用V
,它是可视行 模式,与用于可视模式的普通v
形成对比。If you're copying a whole line then pasting a whole line, use
Y
to yank the line or lines, including line break, in the first place, andp
to paste. You can also useV
, which is visual line mode, in contrast with plainv
for visual mode.我有映射
inoremap jj
。因此,使用ojj
和Ojj
然后使用p
插入新行很容易。所以
ojjp
粘贴新的换行符。它比op
多了一笔,但是ojjp
对我来说很容易。I have mapping
inoremap jj <ESC>
. So it is easy to insert new line withojj
andOjj
and thenp
.so
ojjp
paste new a newline. it have one more stroke theno<esc>p
butojjp
is easy for me.我找到了一个优雅的解决方案。如果您将复制寄存器放入操作系统的剪贴板中(无论如何这都很棒),那么
您可以使用
o
。除了减少笔画之外,这还改进了
op
和:pu
,因为它保留了缩进:其他两个选项都从新行上的字符零开始。需要注意的是,这可能取决于操作系统,也可能不取决于操作系统。我所知道的是它可以在最新版本的 OS X 上运行,但剪贴板只是在操作系统剪贴板中进行复制的多种方法之一。
I found an elegant solution to this. If you are putting the yank register in your OS's clipboard (which is great anyway), with
than you can do
o<Ctl-v>
.Besides being fewer strokes, this improves on both
o<Esc>p
and:pu
because it preserves indenting: both of the other options start you at character zero on the new line.Caveat is that this may or may not be OS dependent. All I know is that it works on recent version of OS X, but clipboard is just one of many ways to get yank in the OS clipboard.
我在 Neovim 配置中使用以下映射:
一点解释:
m`
:在当前光标位置设置一个标记。op
:在下面创建一个新行,并将文本粘贴到该行中OP
:在上面创建一个新行,并将文本粘贴到该行中``
:将光标置于原始位置有关 Vim 中标记的更多信息,请参阅
:h mark
。I use the following mapping in my Neovim config:
A little explanation:
m`
: set a mark in the current cursor position.o<Esc>p
: create a new line below and paste the text in this lineO<Esc>P
: create a new line above and paste the text in this line``
: put the cursor in the original positionSee
:h mark
for more information about marks in Vim.如果您想粘贴
新行
并仍保持缩进,请创建以下映射:nnoremapp oqp
先决条件:您已映射前导符并且您已在
.vimrc
中设置了自动缩进
。说明:使用“o”创建一个新行,键入“q”,然后按退格键(以保持缩进),“esc”将您带回最终粘贴的正常模式。
If you want to paste in a
new line
and still keep indentation, create this mapping:nnoremap <leader>p oq<BS><Esc>p
Prerequisite: you have leader mapped and you have
set autoindent
in your.vimrc
.Explanation: a new line is created with 'o', 'q' is typed and then back-spaced on (to keep indentation), and 'esc' brings you back to normal mode where you finally paste.
如果您还想以插入模式结束,则可以在插入模式下使用
CTRL-R "
进行粘贴。https://stackoverflow.com/a/2861909/461834还是三个击键,但没有转义,如果你想以插入结束,你可以保存一次击键。
If you also want to end in insert mode, it is possible to paste while in insert mode using
CTRL-R "
. https://stackoverflow.com/a/2861909/461834Still three keystrokes, but no escape, and you save a keystroke if you want to end in insert anyway.
此解决方案似乎仅适用于复制的文本块在新行上开始(而不是抓取行内某处的文本片段),但您始终可以从要抓取的最后一个字符开始复制,然后导航到所需复制块开始之前的行尾的最后一个字符。然后,当您想要粘贴它时,将光标放在要粘贴文本的行的末尾,然后按 p。如果我没有搞砸解释,这应该可以提供您正在寻找的效果。
This solution only seems to apply when the block of copied text starts on a new line (as opposed to grabbing a snippet of text somewhere within a line), but you can always start your copy on the last character you want to grab, then navigate to the last character at the end of line prior to the start of your desired copy block. Then when you want to paste it, place the cursor at the end of the line under which you want your text to be pasted and hit p. If I haven't screwed up the explanation, this should provide the effect you're looking for.