In vi normal mode, place the cursor on the line to remove.
Delete the entire line in one operation.
Which command should be used?
dd deletes the complete current line and stores the deleted text in a register, so it can be pasted with p.
Detailed explanation
yyIncorrect. yy yanks the line without deleting it.
Incorrect. yy copies the line and leaves it visible.
ccIncorrect. cc changes the line and enters insert mode.
Incorrect. cc changes the line and enters insert mode rather than simply deleting it.
dwIncorrect. dw deletes a word-sized motion from the cursor.
Incorrect. dw removes a word range, not the complete line.
ddCorrect. dd deletes the current line.
Correct. dd deletes the current line and stores it in the unnamed register.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp); printf 'keep1\ndelete\nkeep2\n' >"$tmp"; vim -Nu NONE -n -es "$tmp" +'normal gg jdd' +wq; cat "$tmp"; rm -f "$tmp"Expected result
keep1
keep2Key points
- d is the delete operator
- dd operates on a whole line
- Deleted text enters a register
Notes
- Environment: Vim 9.x互換のvi操作 / 一時ファイル
- Use a temporary file and harmless edits when testing vi commands.
- Confirm the behavior on the target vi/Vim implementation.
Foundation review
Place the operation
Repeated operators such as dd act on a whole line, while an operator combined with motion selects a range.
Check the side effect
Delete a disposable line and verify that the following line moves into its place.