如何在 Vim 中复制整行?

发布于 2024-07-05 11:19:30 字数 197 浏览 10 评论 0 原文

如何在 Vim 中复制整行,类似于 IntelliJ IDEA/Resharper 中的 Ctrl+DCtrl+Alt+/Eclipse 中?

How do I duplicate a whole line in Vim in a similar way to Ctrl+D in IntelliJ IDEA/ Resharper or Ctrl+Alt+/ in Eclipse?

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

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

发布评论

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

评论(23

陌若浮生 2024-07-12 11:19:30

我更喜欢在 .vimrc 中定义自定义键盘映射 Ctrl+D 以在正常模式下复制当前行和插入模式:

" duplicate line in normal mode:
nnoremap <C-D> Yp
" duplicate line in insert mode:
inoremap <C-D> <Esc> Ypi

I prefer to define a custom keymap Ctrl+D in .vimrc to duplicate the current line both in normal mode and insert mode:

" duplicate line in normal mode:
nnoremap <C-D> Yp
" duplicate line in insert mode:
inoremap <C-D> <Esc> Ypi
合约呢 2024-07-12 11:19:30

默认值为 yyp,但我已经使用此重新绑定一年左右并且喜欢它:

" set Y复制线条,也可以在视觉模式下工作。
nnoremap Y yyp
vnoremap Y y`>pgv

Default is yyp, but I've been using this rebinding for a year or so and love it:

" set Y to duplicate lines, works in visual mode as well.
nnoremap Y yyp
vnoremap Y y`>pgv

苯莒 2024-07-12 11:19:30

对于不懂 vi 的人来说,上面的一些答案可能会用“粘贴... 当前行”之类的短语来误导他。

它实际上是“在光标之后/之前粘贴...”。

yyY 复制该行

或者

dd 删除该行

,然后

p 将复制或删除的文本粘贴到 >光标

或者

P 将复制或删除的文本粘贴到光标之前

有关更多按键绑定,您可以访问此站点:vi 完整按键绑定列表

For someone who doesn't know vi, some answers from above might mislead him with phrases like "paste ... after/before current line".

It's actually "paste ... after/before cursor".

yy or Y to copy the line

or

dd to delete the line

then

p to paste the copied or deleted text after the cursor

or

P to paste the copied or deleted text before the cursor

For more key bindings, you can visit this site: vi Complete Key Binding List

彡翼 2024-07-12 11:19:30

我喜欢:
Shift+v(立即选择整行,并让您根据需要选择其他行)、y、p

I like:
Shift+v (to select the whole line immediately and let you select other lines if you want), y, p

于我来说 2024-07-12 11:19:30

另一种选择是使用:

nmap <C-d> mzyyp`z

为您提供保留光标位置的优势。

Another option would be to go with:

nmap <C-d> mzyyp`z

gives you the advantage of preserving the cursor position.

允世 2024-07-12 11:19:30

您也可以尝试 这将重复插入模式下的最后一行,并为您带来一个包含所有行的完成窗口。 它的工作原理几乎与 类似。

You can also try <C-x><C-l> which will repeat the last line from insert mode and brings you a completion window with all of the lines. It works almost like <C-p>

智商已欠费 2024-07-12 11:19:30

我知道我参加聚会迟到了,但无论如何; 我的 .vimrc 中有这个:

nnoremap <C-d> :copy .<CR>
vnoremap <C-d> :copy '><CR>

:copy 命令只是将选定的行或范围(总是整行)复制到作为其参数给出的行号下方。

在正常模式下,它的作用是复制。将此行复制到此行的正下方

在视觉模式下它变成 '<,'>; copy '> 从选择的开始到选择的结束复制到选择结束下方的行

I know I'm late to the party, but whatever; I have this in my .vimrc:

nnoremap <C-d> :copy .<CR>
vnoremap <C-d> :copy '><CR>

the :copy command just copies the selected line or the range (always whole lines) to below the line number given as its argument.

In normal mode what this does is copy . copy this line to just below this line.

And in visual mode it turns into '<,'> copy '> copy from start of selection to end of selection to the line below end of selection.

霊感 2024-07-12 11:19:30

如果您想要另一种方式:

“ayy
这会将行存储在缓冲区a中。

“ap
这会将缓冲区 a 的内容放在光标处。

这有很多变体。

“a5yy
这会将 5 行存储在缓冲区 a 中。

请参阅“Vim 帮助文件了解更多乐趣。

If you want another way:

"ayy:
This will store the line in buffer a.

"ap:
This will put the contents of buffer a at the cursor.

There are many variations on this.

"a5yy:
This will store the 5 lines in buffer a.

See "Vim help files for more fun.

嘦怹 2024-07-12 11:19:30

yyp - 用“yippee!”记住它

多行,中间有一个数字:

y7yp

yyp - remember it with "yippee!"

Multiple lines with a number in between:

y7yp

手心的海 2024-07-12 11:19:30

yyp - 在

yyP - 粘贴在前面

yyp - paste after

yyP - paste before

知你几分 2024-07-12 11:19:30

在 vim 中复制并粘贴

没有比这更简单的了! 从正常模式:

yy

然后移动到要粘贴的行并

p

copy and paste in vim

Doesn't get any simpler than this! From normal mode:

yy

then move to the line you want to paste at and

p
请帮我爱他 2024-07-12 11:19:30

执行此操作:

首先,yy 复制当前行,然后p 粘贴。

Do this:

First, yy to copy the current line, and then p to paste.

弱骨蛰伏 2024-07-12 11:19:30

yy

将拉出当前行而不删除它

dd

将删除当前行

p< /kbd>

放置由上述任一方法抓取的行

yy

will yank the current line without deleting it

dd

will delete the current line

p

will put a line grabbed by either of the previous methods

绮筵 2024-07-12 11:19:30

YPYpyyp

YP or Yp or yyp.

断肠人 2024-07-12 11:19:30

yyY 复制该行(助记符:yank

或者

dddelete 该行(Vim 将您删除的内容复制到类似剪贴板的“注册”,就像剪切操作)

,然后

ppaste复制或删除的文本 当前行

或者

Shift + P 将复制或删除的文本p粘贴在当前行之前

yy or Y to copy the line (mnemonic: yank)

or

dd to delete the line (Vim copies what you deleted into a clipboard-like "register", like a cut operation)

then

p to paste the copied or deleted text after the current line

or

Shift + P to paste the copied or deleted text before the current line

迷迭香的记忆 2024-07-12 11:19:30

普通模式:参见其他答案。

Ex 方式:

  • :t.< /code> 将复制该行,
  • :t 7 将在第 7 行之后复制它,
  • :,+t0 将复制文件开头的当前行和下一行(< code>,+ 是范围 .,.+1) 的同义词,
  • :1,t$ 会将行从开头到光标位置复制到end(1, 是范围 1,. 的同义词)。

如果您需要移动而不是复制,请使用 :m 而不是 :t

如果将其与 :g:v 结合使用,这将非常强大:

  • :v/foo/m$ 将移动所有不匹配的行将模式“foo”添加到文件末尾。
  • :+,$g/^\s*class\s\+\i\+/t. 将复制光标后面 class xxx 形式的所有后续行。

参考: :help range:help :t:help :g:help :m 和 <代码>:帮助:v

Normal mode: see other answers.

The Ex way:

  • :t. will duplicate the line,
  • :t 7 will copy it after line 7,
  • :,+t0 will copy current and next line at the beginning of the file (,+ is a synonym for the range .,.+1),
  • :1,t$ will copy lines from beginning till cursor position to the end (1, is a synonym for the range 1,.).

If you need to move instead of copying, use :m instead of :t.

This can be really powerful if you combine it with :g or :v:

  • :v/foo/m$ will move all lines not matching the pattern “foo” to the end of the file.
  • :+,$g/^\s*class\s\+\i\+/t. will copy all subsequent lines of the form class xxx right after the cursor.

Reference: :help range, :help :t, :help :g, :help :m and :help :v

栀梦 2024-07-12 11:19:30

我喜欢使用此映射:

:nnoremap yp Yp

因为它可以与本机 YP 命令一起使用。

I like to use this mapping:

:nnoremap yp Yp

because it makes it consistent to use alongside the native YP command.

只是一片海 2024-07-12 11:19:30

如果您想在下面复制一行,则上面的答案是正确的。

另一种常见情况是,如果您在文本中的某个位置,并且想要复制位于缓冲区中较远、看不见的行,但由于某种原因您不想去那里。

在这种情况下:

  • 开始输入要复制的行开头的几个字母;
  • 点击Ctrl-X Ctrl-L:这将弹出一个类似于自动完成列表的弹出列表(Ctrl-P 和 Ctrl-N);
  • 使用 Ctrl-LCtrl-N 在此列表中上下导航,直到找到所需的行;
  • Enter,瞧。

我发现这在编写代码时特别方便。

If you want to duplicate a line just below, the answers above are correct.

Another common case would be if you are somewhere in your text, and you want to duplicate a line located far away in your buffer, out of sight, and you don't want to go there, for some reason.

In this case:

  • begin typing a few letters of the beginning of the line that you want to duplicate;
  • hit Ctrl-X Ctrl-L: this will bring a pop-up list similar to the autocompletion one (Ctrl-P and Ctrl-N);
  • navigate up and down through this list with Ctrl-L and Ctrl-N until you get the line you wish;
  • hit Enter, and voilà.

I find this particularly handy when you write code.

第七度阳光i 2024-07-12 11:19:30

我用的是这个映射,和vscode类似。 我希望它有用!

nnoremap <A-d> :t. <CR>==
inoremap <A-d> <Esc>:t. <CR>==gi
vnoremap <A-d> :t$ <CR>gv=gv

I use this mapping, which is similar to vscode. I hope it is useful!!!.

nnoremap <A-d> :t. <CR>==
inoremap <A-d> <Esc>:t. <CR>==gi
vnoremap <A-d> :t$ <CR>gv=gv
森林很绿却致人迷途 2024-07-12 11:19:30

如果您想复制一行并将其粘贴到当前的行下方,就像在 Sublime Ctrl+Shift+D 中一样,那么您可以将其添加到您的 .vimrc 文件中。

nmap ; Yp

或者,对于插入模式:

imap ; Ypa

If you would like to duplicate a line and paste it right away below the current like, just like in Sublime Ctrl+Shift+D, then you can add this to your .vimrc file.

nmap <S-C-d> <Esc>Yp

Or, for Insert mode:

imap <S-C-d> <Esc>Ypa

孤城病女 2024-07-12 11:19:30

对于那些开始学习 vi 的人来说,这里是对 vi 的一个很好的介绍,通过并排列出 vi 命令到典型的 Windows GUI 编辑器光标移动和快捷键。 它列出了所有基本命令,包括 yy(复制行)和 p(在后面粘贴)或 P(在前面粘贴)。

适用于 Windows 用户的 vi (Vim)

For those starting to learn vi, here is a good introduction to vi by listing side by side vi commands to typical Windows GUI Editor cursor movement and shortcut keys. It lists all the basic commands including yy (copy line) and p (paste after) or P(paste before).

vi (Vim) for Windows Users

紧拥背影 2024-07-12 11:19:30

1 陷阱:当您使用“p”放置该行时,它会将其放置在光标所在行的之后,因此如果您想在要拉动的行之后添加该行,请不要'在放置新行之前,不要将光标向下移动一行。

1 gotcha: when you use "p" to put the line, it puts it after the line your cursor is on, so if you want to add the line after the line you're yanking, don't move the cursor down a line before putting the new line.

海拔太高太耀眼 2024-07-12 11:19:30

#复制当前行
用法:
按 yy(复制当前行)
按 p(粘贴复制的行)

#help&more:
https://www.youtube.com/watch?v=HMpB28l1sLc

#copy current line
usage:
press yy (to copy current line)
press p (to paste copied line)

#help&more:
https://www.youtube.com/watch?v=HMpB28l1sLc

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