In vi normal mode, the cursor is on the line to remove.
Delete that line including its newline.
Which key sequence is correct?
dd deletes the current line as a linewise operation. The deleted text is placed in a register and can later be pasted with p.
Detailed explanation
xIncorrect. x deletes one character at the cursor.
dwIncorrect. dw deletes from the cursor through a word motion, not necessarily the whole line.
DIncorrect. D deletes from the cursor to the end of the line.
ddCorrect. dd removes the complete current line.
Try it yourself
An example you can run in a temporary verification environment.
printf 'one\ntwo\nthree\n' > sample.txt; vim -Nu NONE -n -es sample.txt -c 'normal! 2Gdd' -c 'wq'; cat sample.txtExpected result
one
threeKey points
- dd deletes a line
- x deletes one character
- D deletes from the cursor to the line end
Notes
- Environment: Vim 9.x(vi互換操作)
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.