31

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?

33

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 ^
  • 4
    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 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. – 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
20

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.

  • 2
    If you include the Shift it's the same and possibly easier. – Holloway Apr 29 '15 at 12:02
  • 4
    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
14

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
    I was just thinking of adding this as an answer and then saw yours – sbell Apr 29 '15 at 15:27
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.

  • 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 – FliiFe 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" :-)

  • 2
    ^D will leave the whitespace alone. – EvergreenTree Apr 28 '15 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. – Kyle Strand Apr 29 '15 at 22:57
  • This will behave differently on the last line of the file. – tommcdo Apr 30 '15 at 2:41
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, privacy policy and cookie policy

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