18

Is there a faster way to erase the text on a line than

  • pressing 0 to jump to the start, then
  • pressing d$ to delete until the end of the line?

Also, what if I wanted to delete everything except the indentation? Is there a way to do that?

34

As you said, there are multiple ways, and you can also take into account on what your next action will be after you clear or delete the current line.

  1. dd will delete the current line (not clear)
  2. 0D will clear the current line including whitespace
  3. ^D will clear the current line not including whitespace (^dg_ saves trailing space)
  4. 0C will clear the current line including whitespace and put you into insert mode
  5. ^C will clear the current line not including whitespace and put you into insert mode (^cg_ saves trailing space)

cc or S will function as either 4. or 5. if 'autoindent' is off or on respectively.

So use whatever fits the action at hand. I believe that 0D is the fastest to clear the line, but you can map that to something if you wish, to make it even faster.

9

Yep. If there's one thing I'm liking about Vim, it's that the answer to "Can I do that?" is almost always an emphatic yes.

0D Jumps to the beginning, then deletes through the end of the line.

^D (not Ctrl+D) will jump to the first non-whitespace character, then delete through the end.

(P.S. I figured this out just now and wanted to share with any other noobs. Please let me know if there's a better way.)

6

Clear the line with one (shifted) key: S.

This also keeps the indent.

You go into insert mode at the first position after the indent, or at start of line - just what you need to substitute a line - therefore the name S - like substitute.

:help S

["x]S   Delete [count] lines [into register x] and start
        insert.  Synonym for "cc" |linewise|.

["x]cc  Delete [count] lines [into register x] and start
        insert |linewise|.  If 'autoindent' is on, preserve
        the indent of the first line.
  • S is a great key. – domi91c Apr 20 '17 at 18:29
3

With Kana's "vim-textobj-line" plugin, you can use operations on indented line with il (e.g. dil), or entire line without the newline, with al (e.g. yal). The advantage of this approach is subtle: instead of having to think of both motions (start, end) for the desired operation, you specify the text-object directly: in line, a line.

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.