How to cut an entire line in vim and paste it?

Vim

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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.

Solution 3 - Vim

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

Solution 4 - Vim

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.

Solution 5 - Vim

Let's say that you wanted to cut the line bbb and paste it under the line ---

Before:

aaa
bbb
---

After:

aaa
---
bbb
  1. Put your cursor on the line bbb
  2. Press d+d
  3. Put your cursor on the line ---
  4. Press p

enter image description here

Solution 6 - Vim

The quickest way I found is through editing mode:

  1. Press yy to copy the line.
  2. Then dd to delete the line.
  3. Then p to paste the line.

Solution 7 - Vim

  1. press 'V' in normal mode to select the entire line
  2. then press 'y' to copy it
  3. go to the place you want to paste it and press 'p' to paste after cursor or 'P' to paste before it.

Solution 8 - Vim

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

Solution 9 - Vim

Just three steps.


  1. Go to the start of the text, and press v
  2. Go to the end of the text, and press d
  3. Go to the place that you want to paste, and press p

Solution 10 - Vim

  1. Go to the line, and first press esc, and then Shift + v.

(This would have highlighted the line)

  1. press d

(The line is now deleted)

  1. Go to the location, where you wanted to paste the line, and hit p.

In a nutshell,

Esc -> Shift + v -> d -> p

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVivekVarade123View Question on Stackoverflow
Solution 1 - VimDaedalusFallView Answer on Stackoverflow
Solution 2 - VimProgrammer123123View Answer on Stackoverflow
Solution 3 - VimKellen StuartView Answer on Stackoverflow
Solution 4 - VimdsummerslView Answer on Stackoverflow
Solution 5 - VimcustomcommanderView Answer on Stackoverflow
Solution 6 - VimVadoffView Answer on Stackoverflow
Solution 7 - Vimojas mohrilView Answer on Stackoverflow
Solution 8 - Vim4ndrewView Answer on Stackoverflow
Solution 9 - VimsumedheView Answer on Stackoverflow
Solution 10 - VimRahul PatelView Answer on Stackoverflow