52

A fairly common operation for me in Vim, since I am compulsive-obsessive and like to keep my files free of whitespace, is to delete the contents of an entire line, but not the line itself (i.e. not the line break).

Is there a built-in Vim way to do this? Perhaps a d<movement command> operation?

So far, the best I have is (on the line in question) 0d$. Is there a quicker way?

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
Andrew Ferrier
  • 6,729
  • 7
  • 28
  • 41

7 Answers7

55

You can use 0D this will go to the first character on the line and delete until the end of the line. Note that you can use ^D if you want to leave any preceding whitespace alone. Also, these operations cannot be repeated by using .. If you want an operation which can be repeated with the . command, refer to David Lord's answer.

Help topics:

  • :help 0
  • :help D
  • :help ^
EvergreenTree
  • 8,180
  • 2
  • 37
  • 59
  • 8
    While this is direct, to the point, and (probably) the fewest keystrokes, it's not repeatable using the . command. For that reason, I think cc<Esc> or S<Esc> is more attractive if you care about repeating. – tommcdo Apr 29 '15 at 13:11
  • That is true. However, you could record it as a macro and repeat that using .. – EvergreenTree Apr 29 '15 at 13:23
  • In general, macros cannot be repeated with .. They work as if those keystrokes were entered interactively, so individual commands are treated separately. Using . will repeat only the last command in the macro. – tommcdo Apr 30 '15 at 02:51
  • I tested it, and that's not how it works for me. If the record the macro, then use it using @{reg}, you can repeat it with . afterwards. – EvergreenTree Apr 30 '15 at 12:07
  • 1
    You're just repeating the last command of the macro, which is D. After a 0D operation, your cursor will be left in column 0. If your cursor is already on the beginning of the line (after you move to another one), it will look like the macro was properly repeated. Try moving the cursor forward in the line and typing . -- you'll see that it only repeats D. – tommcdo Apr 30 '15 at 12:11
  • Ah, my mistake. – EvergreenTree Apr 30 '15 at 15:19
28

cc <Esc> will do as you ask, but is more keystrokes than 0D. If you want to put something into that line afterwards, cc may be best.

David Lord
  • 698
  • 6
  • 12
  • 2
    If you include the Shift it's the same and possibly easier. – Holloway Apr 29 '15 at 12:02
  • 7
    While this is not the "cleanest" solution, it has one advantage: it's exactly one action, and so it's repeatable with the . command. In contrast, 0D (as suggested in the chosen answer) is two actions: the 0 motion and the D command. – tommcdo Apr 29 '15 at 13:08
  • @Trengot it probably is easier than 0D for me since I have jk mapped to <Esc> in my vimrc, so my hands wouldn't even have to leave the home row. – EvergreenTree Apr 29 '15 at 18:23
  • 1
    See @Nobilis answer: S is documented as "Synonym for cc" – Volker Siegel Apr 30 '15 at 17:05
17

S and then Ctrl+C or Esc seems to accomplish the exact same function.

It can then be repeated with . as it constitutes a single action.

Nobilis
  • 299
  • 1
  • 7
7

0D is probably what you want. It will delete from the cursor to the end of the line while leaving the line (and any characters before the cursor) in place.

SupBrah
  • 79
  • 3
  • Altho this answer is almos one year old, it is worth noting that 0D will delete from the beginning of the line, not from the cursor position – THC Jul 31 '16 at 16:05
4

I typically use ddO. dd to delete the current line, and O (that's a capital o) to add a new line.

This is also 3 keystrokes, but dd counts only as 1 and a half, and I find O easier to type than $ .

It's different from the 0D solution in that it does auto-indentation, for example with this code (where !··· is a tab, and █ the cursor:

def asd():
!···if foo:
!···!···foo()█
!···!···foobar()

0D will leave you with:

def asd():
!···if foo:
█
!···!···foobar()

And ddO will leave you with:

def asd():
!···if foo:
!···!···█
!···!···foobar()

And if you press <Esc> immediately after this, Vim should remove the auto-indentation, so you have the same as with 0D.

There is no 'correct' way, it will depend what exactly you'll want to do. I prefer ddO because it's more flexible, and because it's "in my fingers" :-)

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
1

My preferred way to do this is: A<C-u>

This puts you into insert mode at the end of the line, and then deletes all characters behind the cursor (i.e. the entire line, except the newline). I like this more than other suggestions because it has been trained into my muscle memory—it is not more efficient, more practical, or really better in any way, but it is mine.

user49544
  • 11
  • 2
  • Remark: to be comparable to the other solution it should include a trailing <Esc> to come back to Normal mode. – Vivian De Smedt Nov 15 '23 at 19:32
  • @VivianDeSmedt some of the other answers use cc or C with the explicit purpose of entering text right away. It compares with them. But your remark is valid, this will end in insert mode. – Friedrich Nov 15 '23 at 19:41
  • But the S and the cc explicitly <Esc> in their answer as far as I can tell. But feel free to keep your solution as such. It was just a comment to talk ;-) – Vivian De Smedt Nov 15 '23 at 20:13
0

And to save that one precious key stroke you can use 0C which will delete all the characters on the current line and put you into insert mode.