How do I delete till non-whitespace in vim?

26,647

Solution 1

The sequence dw will delete all characters from the cursor until the next word. This means that if you execute the command while standing in the middle of a word, it will delete the remainder of that word and subsequent whitespaces. May or may not be what you're looking for.

Solution 2

You may want to try dW. This will move by "WORD" (see help for WORD and word), which looks more appropriate for your question.

The dw won't meet your needs in, for example:

array[1] = 5

Hitting dw while positioned in the a will leave you with:

[1] = 5

But using dW will result in:

= 5

Solution 3

Many of the answers here don't really address the question directly. The asker wants to know how to delete up to the first non-whitespace character. Some of the answers will technically work, but let's take a look at how to do this explicitly.

The following examples demonstrate how to do this in normal mode with variations that account for the starting position of the cursor. The u̲nderlined c̲haracters indicate the cursor position:


     dw:   foo_     bar   →   foob̲ar

The delete word command, described in other answers, works just fine to delete up to the next non-whitespace character when our cursor is positioned before the target.


     db:   foo      b̲ar   →   b̲ar

Naturally, we'd want to try the inverse of dw to delete backwards to the first non-whitespace character before the cursor. However, as shown above, the delete back-word command deletes more than we expect—it erases the previous word as well. For this case, we should use:

     dT<?>:   foo      b̲ar   →   foob̲ar

...where <?> is the first non-whitespace character before the cursor. The delete back-unTil command erases everything up to but not including the character <?>, which, in this case, is the character o at the end of "foo".


     dt<?>:   foo_     bar   →   foob̲ar

Similar to the previous command, we can use delete until (with a lowercase "t") to delete characters forward until the next <?> character (the b in "bar", for this example). This achieves the same result as dw for the purpose of this question.


     diw:   foo   _   bar   →   foob̲ar

If our cursor is positioned in the middle of the whitespace, we can use the delete inner word command to remove the whitespace from both sides.


     d/<?>:   foo_   \n   bar   →   foob̲ar

If the whitespace we want to remove includes line-breaks, we can use the command shown above to delete until matched pattern of <?>, where the pattern in this case is just the first non-whitespace character. As shown, press Enter to complete this command.

When the first non-whitespace character occurs at the beginning of the line after the break, Vim will remove the whitespace, but leave the target on the next line. We need to add J to the above command to Join the lines (an uppercase "j").

     d/<?>J:   foo_     \nbar   →   foob̲ar

Solution 4

To delete a word regardless on which letter the cursor is on, use daw (mnemonic "delete a word") works with other commands as well, e.g. caw "change a word". f and t are other excellent command that can be used together with d. E.g. to delete from cursor to and including first occurrence of e.g. the letter "t", use dft. To leave the "t" intact, use dtt instead.

Solution 5

dw should work.

Share:
26,647
Singlestone
Author by

Singlestone

I am an old programmer.

Updated on July 05, 2022

Comments

  • Singlestone
    Singlestone almost 2 years

    I'm looking for a command to delete from the cursor to the first non-whitespace character on the same line. I've googled for a while and tried several possibilities. No joy. Does someone out there know how to do this?

  • Singlestone
    Singlestone over 12 years
    This does it. It's a bit counterintuitive - I expected dw to mean "delete the current word, starting at the cursor". But it works, it works. Thanks.
  • richardolsson
    richardolsson over 12 years
    Per the usual scheme, you can also do d2w, d3w et c to delete all characters until the second or third word after the cursor. If this answer solved your problem, please accept it as correct.
  • orip
    orip over 12 years
    +1. To be precise, w means "move to the beginning of the next word", and d<movement> deletes from the current position to where the movement takes you.
  • Fredrik Pihl
    Fredrik Pihl over 12 years
    to delete a word regardless on which letter the cursor is on, use daw (mnemonic "delete a word") works with other commands as well, e.g. caw "change a word"
  • mbdev
    mbdev almost 8 years
    daw appears to also remove a space before the word if one exists. A better combo to use is diw which is delete in word. It also works regardless of cursor position in the word.
  • Zelphir Kaltstahl
    Zelphir Kaltstahl over 7 years
    What about more than one line? I know this is not the question, but something to go over multiple lines would be more useful.
  • Cy Rossignol
    Cy Rossignol over 6 years
    @Zelphir I added an answer that shows how to do this over multiple lines.
  • Micah Lindstrom
    Micah Lindstrom about 4 years
    Thank you for addressing the line-breaks! Your answer seems to say <?> is the pattern for matching the first non-whitespace character. However, I had to use \S for the pattern. My whole command was therefore d/\S↵. This may be what you meant in your answer, but it was not immediately clear to me.
  • Cy Rossignol
    Cy Rossignol about 4 years
    @Micah Sorry for being unclear. I used <?> in this answer as a placeholder that represents some character. I intended for a reader to substitute <?> with a particular character in the edited text rather than to enter those keystrokes literally. For that last example, your choice to enter \S in place of pattern <?> does indeed produce the best generic command to remove white space across line breaks. I prefer to type the actual character (b in the example) because it's usually easier to reach for.
  • Joel.O
    Joel.O about 2 years
    Absolutely brilliant this has saved me so much time !!!