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?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityAs 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.
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.
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.)
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.
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.