I know how to use the v
command in vim, but I need something which will delete an entire line and it should allow me to paste the same line somewhere else.
-
1You should update your accepted answer. It doesn't tell you how to paste...– Kellen StuartNov 23, 2020 at 18:30
-
To move a line say from line 10 to line 20 use the following :10m 20– peterretiefMar 6, 2021 at 8:21
10 Answers
dd
in command mode (after pressing escape) will cut the line, p
in command mode will paste.
Update:
For a bonus, d
and then a movement will cut the equivalent of that movement, so dw
will cut a word, d<down-arrow>
will cut this line and the line below, d50w
will cut 50 words.
yy
is copy line, and works like dd
.
D
cuts from cursor to end of line.
If you've used v
(visual mode), you should try V
(visual line mode) and <ctrl>v
(visual block mode).
-
15Good answer. I would add
P
to paste the line before the cursor in command mode. And<Ctrl-R>"
to paste the line in insert mode.– BireiJan 7, 2012 at 21:44 -
14And
]p
to paste with correct indenting. But we should be careful not to post the entire vim manual here :). Jan 7, 2012 at 21:51
Pressing Shift+v would select that entire line and pressing d would delete it.
You can also use dd, which is does not require you to enter visual mode.
-
40Or you could press vv, or dd, which are equivalent without the annoying visual mode activating. Dec 16, 2013 at 10:55
-
16
-
1what about undo... Im trying on my docs and not able to undo them. Oct 8, 2018 at 12:34
-
30
-
7@becko
p
Paste after cusrorShift
+P
Paste before cursor. Accepted answer is terrible. Shouldn't have to answer in comments. Nov 23, 2020 at 18:34
Delete current line and copy to clipboard:
d + d
Paste After The Cursor
p
Paste Before The Cursor
Shift + p
Select Whole Line (I use this ALL the time)
Shift + v
Then j or k to move down and up respectively
Essentially d + d is the equivalent of Shift + v then d
-
1Does SHIFT + P insert before or after the current line, where the cursor is located? Nov 10, 2020 at 16:09
-
@BernhardDöbler I've updated my answer to answer your question.
Shift
+p
will paste before the cursor. I find myself using just plainp
more often these days. Nov 10, 2020 at 17:12
There are several ways to cut a line, all controlled by the d
key in normal mode. If you are using visual mode (the v
key) you can just hit the d
key once you have highlighted the region you want to cut. Move to the location you would like to paste and hit the p
key to paste.
It's also worth mentioning that you can copy/cut/paste from registers. Suppose you aren't sure when or where you want to paste the text. You could save the text to up to 24 registers identified by an alphabetical letter. Just prepend your command with '
(single quote) and the register letter (a thru z). For instance you could use the visual mode (v
key) to select some text and then type 'ad
to cut the text and store it in register 'a'. Once you navigate to the location where you want to paste the text you would type 'ap
to paste the contents of register a.
Let's say that you wanted to cut the line bbb
and paste it under the line ---
Before:
aaa
bbb
---
After:
aaa
---
bbb
- Put your cursor on the line
bbb
- Press d+d
- Put your cursor on the line
---
- Press p
-
-
1@KolobCanyon It is an animated GIF created with LICEcap - cockos.com/licecap Nov 10, 2020 at 17:20
The quickest way I found is through editing mode:
- Press
yy
to copy the line. - Then
dd
to delete the line. - Then
p
to paste the line.
-
Does p insert before or after the current line, where the cursor is located? Nov 10, 2020 at 16:09
-
- press 'V' in normal mode to select the entire line
- then press 'y' to copy it
- go to the place you want to paste it and press 'p' to paste after cursor or 'P' to paste before it.
Yep, use dd in command line. Also I recommend to print useful image with ViM hotkeys available at http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
Just three steps.
- Go to the start of the text, and press v
- Go to the end of the text, and press d
- Go to the place that you want to paste, and press p
- Go to the line, and first press
esc
, and thenShift + v
.
(This would have highlighted the line)
- press
d
(The line is now deleted)
- Go to the location, where you wanted to paste the line, and hit
p
.
In a nutshell,
Esc
-> Shift + v
-> d
-> p