Deleting to the beginning of line when line starts with whitespace characters

Vim

Vim Problem Overview


I usually use d^ to delete to the beginning of line.

But if the line starts with space or tabulations, the deletion does not go all the way to start of line.

Example: >  foo foo

The line starts with two spaces, and the cursor is between the two "foo"

d^ deletes the first foo, but not the two spaces before it.

It is obviously useful most of the time, but what if I do want to delete everything?

Vim Solutions


Solution 1 - Vim

You can use d0 to delete to the real beginning of the line.

Solution 2 - Vim

as @GWW mention and:

  • use c0 to delete to real begginning of the line and go insert mode.
  • c^ - delete to first non-blank character and go insert mode.

Solution 3 - Vim

You can also use | to goto column 0 of a line, which can be using in combination with d as d| to delete to column 0 of a line.

Source: http://hea-www.harvard.edu/~fine/Tech/vi.html

Solution 4 - Vim

If the cursor in the middle of an empty space between beginning of line and the first character of the line, then you can delete the whole spaces with diw, that means: delete inner word. In this case word is the spaces.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrcoutView Question on Stackoverflow
Solution 1 - VimGWWView Answer on Stackoverflow
Solution 2 - Vimnothing-special-hereView Answer on Stackoverflow
Solution 3 - VimGrifballView Answer on Stackoverflow
Solution 4 - VimMubin IcyerView Answer on Stackoverflow