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?

3

7 Answers 7

53

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 ^
6
  • 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, 2015 at 13:11
  • That is true. However, you could record it as a macro and repeat that using .. Apr 29, 2015 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, 2015 at 2: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. Apr 30, 2015 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, 2015 at 12:11
27

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.

4
  • 2
    If you include the Shift it's the same and possibly easier.
    – Holloway
    Apr 29, 2015 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, 2015 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. Apr 29, 2015 at 18:23
  • 1
    See @Nobilis answer: S is documented as "Synonym for cc" Apr 30, 2015 at 17:05
16

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.

2
  • 2
    I was just thinking of adding this as an answer and then saw yours
    – sbell
    Apr 29, 2015 at 15:27
  • S is a synonym for cc.
    – Chris
    Jun 21, 2021 at 12:12
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.

1
  • 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, 2016 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" :-)

3
  • 2
    ^D will leave the whitespace alone. Apr 28, 2015 at 23:13
  • Since O leaves you in insert mode, this is equivalent to cc, except without auto-indentation....unless your line was already auto-indented. Apr 29, 2015 at 22:57
  • This will behave differently on the last line of the file.
    – tommcdo
    Apr 30, 2015 at 2:41
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.

3
  • Remark: to be comparable to the other solution it should include a trailing <Esc> to come back to Normal mode. Nov 15 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 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 ;-) Nov 15 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.