如何在 Vim 中复制整行?
如何在 Vim 中复制整行,类似于 IntelliJ IDEA/Resharper 中的 Ctrl+D 或 Ctrl+Alt+↑/↓ 在 Eclipse 中?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 Vim 中复制整行,类似于 IntelliJ IDEA/Resharper 中的 Ctrl+D 或 Ctrl+Alt+↑/↓ 在 Eclipse 中?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(23)
我更喜欢在
.vimrc
中定义自定义键盘映射 Ctrl+D 以在正常模式下复制当前行和插入模式:I prefer to define a custom keymap Ctrl+D in
.vimrc
to duplicate the current line both in normal mode and insert mode:默认值为 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
对于不懂 vi 的人来说,上面的一些答案可能会用“粘贴... 当前行”之类的短语来误导他。
它实际上是“在光标之后/之前粘贴...”。
yy 或 Y 复制该行
或者
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
我喜欢:
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
另一种选择是使用:
为您提供保留光标位置的优势。
Another option would be to go with:
gives you the advantage of preserving the cursor position.
您也可以尝试 这将重复插入模式下的最后一行,并为您带来一个包含所有行的完成窗口。 它的工作原理几乎与 类似。
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>
我知道我参加聚会迟到了,但无论如何; 我的 .vimrc 中有这个:
:copy
命令只是将选定的行或范围(总是整行)复制到作为其参数给出的行号下方。在正常模式下,它的作用是
复制。
将此行复制到此行的正下方。在视觉模式下它变成
'<,'>; copy '>
从选择的开始到选择的结束复制到选择结束下方的行。I know I'm late to the party, but whatever; I have this in my .vimrc:
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.如果您想要另一种方式:
“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.
yyp - 用“yippee!”记住它
多行,中间有一个数字:
y7yp
yyp - remember it with "yippee!"
Multiple lines with a number in between:
y7yp
yyp - 在
yyP - 粘贴在前面
yyp - paste after
yyP - paste before
没有比这更简单的了! 从正常模式:
然后移动到要粘贴的行并
Doesn't get any simpler than this! From normal mode:
then move to the line you want to paste at and
执行此操作:
首先,yy 复制当前行,然后p 粘贴。
Do this:
First, yy to copy the current line, and then p to paste.
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 methodsYP
或Yp
或yyp
。YP
orYp
oryyp
.yy 或 Y 复制该行(助记符:yank)
或者
dd 到 delete 该行(Vim 将您删除的内容复制到类似剪贴板的“注册”,就像剪切操作)
,然后
p到paste复制或删除的文本 当前行
或者
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
普通模式:参见其他答案。
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
和 <代码>:帮助:vNormal 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 range1,.
).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 formclass xxx
right after the cursor.Reference:
:help range
,:help :t
,:help :g
,:help :m
and:help :v
我喜欢使用此映射:
因为它可以与本机
YP
命令一起使用。I like to use this mapping:
because it makes it consistent to use alongside the native
YP
command.如果您想在下面复制一行,则上面的答案是正确的。
另一种常见情况是,如果您在文本中的某个位置,并且想要复制位于缓冲区中较远、看不见的行,但由于某种原因您不想去那里。
在这种情况下:
Ctrl-X Ctrl-L
:这将弹出一个类似于自动完成列表的弹出列表(Ctrl-P 和 Ctrl-N
);Ctrl-L
和Ctrl-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:
Ctrl-X Ctrl-L
: this will bring a pop-up list similar to the autocompletion one (Ctrl-P and Ctrl-N
);Ctrl-L
andCtrl-N
until you get the line you wish;Enter
, and voilà.I find this particularly handy when you write code.
我用的是这个映射,和vscode类似。 我希望它有用!
I use this mapping, which is similar to vscode. I hope it is useful!!!.
如果您想复制一行并将其粘贴到当前的行下方,就像在 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
对于那些开始学习 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
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.
#复制当前行
用法:
按 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