Duplicate a whole line in Vim

2019-01-29 14:32发布

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?

19条回答
ら.Afraid
2楼-- · 2019-01-29 14:55

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 http://www.vim.org/htmldoc/help.html for more fun

查看更多
对你真心纯属浪费
3楼-- · 2019-01-29 14:57

yy or Y to copy the line
or
dd to delete (cutting) the line

then

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

查看更多
不美不萌又怎样
4楼-- · 2019-01-29 14:57

yyp - remember it with "yippee!"

Multiple lines with a number in between:

y7yp

查看更多
走好不送
5楼-- · 2019-01-29 14:57

Another option would be to go with:

nmap <C-d> mzyyp`z

gives you the advantage of preserving the cursor position.

查看更多
爷、活的狠高调
6楼-- · 2019-01-29 14:57

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

查看更多
Explosion°爆炸
7楼-- · 2019-01-29 14:57

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

查看更多
登录 后发表回答