如何用vim粘贴新行?

发布于 2024-08-03 09:39:19 字数 146 浏览 4 评论 0原文

我经常需要在 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 技术交流群。

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

发布评论

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

评论(14

一世旳自豪 2024-08-10 09:39:19

:help p 之后不久,它说:

:[line]pu[t] [x]    Put the text [from register x] after [line] (default
                    current line).  This always works |linewise|, thus
                    this command can be used to put a yanked block as
                    new lines.

:[line]pu[t]! [x]   Put the text [from register x] before [line]
                    (default current line).

不幸的是,它并不比您当前的解决方案短,除非您按照不同答案中的建议将其与某些键盘映射结合起来。例如,您可以将其映射到任何键(甚至p):

:nmap p :pu<CR>

Shortly after :help p it says:

:[line]pu[t] [x]    Put the text [from register x] after [line] (default
                    current line).  This always works |linewise|, thus
                    this command can be used to put a yanked block as
                    new lines.

:[line]pu[t]! [x]   Put the text [from register x] before [line]
                    (default current line).

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):

:nmap p :pu<CR>
彡翼 2024-08-10 09:39:19

选项:

1) 使用yy 拉出整行(包括行尾字符)。然后,p 会将这一行粘贴到当前行之后的新行上,并且 P (Shift-P) 将粘贴到当前行上方。

2) 进行映射:那么它只有一两个键:

:nmap ,p o<ESC>p
:nmap <F4> o<ESC>p

3) 映射的函数版本(实际上不必要,但只是为了完整性):

:nmap <F4> :call append(line('.'), @")<CR>

" This one may be a little better (strip the ending new-line before pasting)
:nmap <F4> :call append(line('.'), substitute(@", '\n
, '', ''))<CR>

:help let-register
:help :call
:help append()
:help line()
:help nmap

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 and P (Shift-P) will paste above the current line.

2) Make a mapping: then it's only one or two keys:

:nmap ,p o<ESC>p
:nmap <F4> o<ESC>p

3) The function version of the mapping (unnecessary really, but just for completeness):

:nmap <F4> :call append(line('.'), @")<CR>

" This one may be a little better (strip the ending new-line before pasting)
:nmap <F4> :call append(line('.'), substitute(@", '\n
, '', ''))<CR>

:help let-register
:help :call
:help append()
:help line()
:help nmap
说不完的你爱 2024-08-10 09:39:19

您可以使用 在插入模式下粘贴缓冲区,后跟要粘贴的缓冲区的名称。默认缓冲区是 ",所以您会这样做

o<C-R>"

我发现我经常使用 " 并将其绑定到 在我的 vimrc 中:

inoremap <C-F> <C-R>"

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 do

o<C-R>"

I found that I use <C-R>" very often and bound that to <C-F> in my vimrc:

inoremap <C-F> <C-R>"
忱杏 2024-08-10 09:39:19

这仍然使用三个击键,但我发现它比 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.

永不分离 2024-08-10 09:39:19

使用此插件: https://github.com/tpope/vim-unimpaired

] p 粘贴到下面

[p 粘贴到上面

优点:

  • 适用于所有拉出的文本(单词、行、字符等)
  • 缩进粘贴的文本以匹配文本的缩进
    周围是
  • “更容易”
  • 2 次击键,而不是 3 次,而且击键速度

Using this plugin: https://github.com/tpope/vim-unimpaired

]p pastes on the line below

[p pastes on the line above

advantages:

  • works on all yanked text (word, line, character, etc)
  • indents the pasted text to match the indentation of the text
    around it
  • 2 keystrokes instead of 3 and much "easier" strokes
  • fast
禾厶谷欠 2024-08-10 09:39:19

就我个人而言,我已经像这样映射了 Enter (CR)

nmap <CR> o<Esc>k

...基于 这篇 Vim Wikia 文章。

这样我可以直接从正常模式创建换行符,并将其与想要粘贴到下面的换行符结合起来:

<CR>jp

您也可以在上面的 nmap 中跳过 k ,具体取决于您喜欢的功能来自Enter,所以它只是p

我还将 jj 映射到 Esc,这在这种情况下也有帮助。 Esc 离主行太远了,不知道它在 vim 中的重要性。

不比其他解决方案短,但我确实认为它比其中一些解决方案感觉不那么笨重,而且它还有其他用途。

Personally I've nmapped Enter (CR) like this:

nmap <CR> o<Esc>k

...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:

<CR>jp

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.

七颜 2024-08-10 09:39:19

如果您想保持插入模式,可以执行 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 line
  • ctrl+o – run a single command
    like in normal mode
  • p – paste

It'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.

白日梦 2024-08-10 09:39:19

如果您要复制整行然后粘贴整行,请首先使用 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, and p to paste. You can also use V, which is visual line mode, in contrast with plain v for visual mode.

街角迷惘 2024-08-10 09:39:19

我有映射 inoremap jj。因此,使用 ojjOjj 然后使用 p 插入新行很容易。

所以ojjp粘贴新的换行符。它比op多了一笔,但是ojjp对我来说很容易。

I have mapping inoremap jj <ESC>. So it is easy to insert new line with ojj and Ojj and then p.

so ojjp paste new a newline. it have one more stroke then o<esc>p but ojjp is easy for me.

有木有妳兜一样 2024-08-10 09:39:19

我找到了一个优雅的解决方案。如果您将复制寄存器放入操作系统的剪贴板中(无论如何这都很棒),那么

set clipboard+=unnamed

您可以使用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

set clipboard+=unnamed

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.

孤独难免 2024-08-10 09:39:19

我在 Neovim 配置中使用以下映射:

nnoremap <leader>p m`o<ESC>p``
nnoremap <leader>P m`O<ESC>p``

一点解释:

  • m`:在当前光标位置设置一个标记。
  • op:在下面创建一个新行,并将文本粘贴到该行中
  • OP:在上面创建一个新行,并将文本粘贴到该行中
  • ``:将光标置于原始位置

有关 Vim 中标记的更多信息,请参阅 :h mark

I use the following mapping in my Neovim config:

nnoremap <leader>p m`o<ESC>p``
nnoremap <leader>P m`O<ESC>p``

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 line
  • O<Esc>P: create a new line above and paste the text in this line
  • ``: put the cursor in the original position

See :h mark for more information about marks in Vim.

一个人的旅程 2024-08-10 09:39:19

如果您想粘贴新行并仍保持缩进,请创建以下映射:

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/461834

Still three keystrokes, but no escape, and you save a keystroke if you want to end in insert anyway.

话少心凉 2024-08-10 09:39:19

此解决方案似乎仅适用于复制的文本块在新行上开始(而不是抓取行内某处的文本片段),但您始终可以从要抓取的最后一个字符开始复制,然后导航到所需复制块开始之前的行尾的最后一个字符。然后,当您想要粘贴它时,将光标放在要粘贴文本的行的末尾,然后按 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.

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