How to remove to non-space in vim?

I am looking for a command to delete from the cursor to the first character without spaces in the same line. For a while I searched Googled and tried several features. There is no joy. Does anyone know how to do this?

+45
vim whitespace
Aug 07 2018-11-11T00:
source share
5 answers

The sequence d w deletes all characters from the cursor to the next word. This means that if you execute a command while standing in the middle of a word, it will remove the rest of that word and subsequent spaces. May or may not be what you are looking for.

+77
Aug 07 '11 at 20:13
source share

You can try d W. This will move "WORD" (see Help for WORD and words), which looks more appropriate for your question.

dw will not satisfy your needs, for example:

 array[1] = 5 

Pressing dw located at a will leave you with:

 [1] = 5 

But using dw will result in:

 = 5 
+14
Aug 7 2018-11-21T00:
source share

To delete a word, no matter what letter the cursor is on, use daw (the mnemonic "delete word") also works with other commands, for example. caw "change the word". f and t are a great team that can be used with d . For example. to move from the cursor to the first inclusion, including, for example, the letter "t", use dft . To leave t intact, use dtt .

+6
Aug 07 '11 at 21:30
source share

Many of the answers here do not directly address the question. The respondent wants to know how to remove up to the first non- whitespace character. Some of the answers will technically work, but let's see 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:




d w : foo_ bar → foob̲ar

The d elete w ord command described in other answers is great for deleting to the next character without spaces when our cursor is in front of the target.




d b : foo b̲ar → b̲ar

Naturally, we want to try inverting d w to delete back before the first character without a space before the cursor. However, as shown above, the d elete b ack-word command deletes more than we expect - it also erases the previous word. For this case, we must use:

d T <?> : foo b̲ar → foob̲ar

... where <?> is the first character without spaces before the cursor. The d elete back-un T il command erases everything before, but does not include the <?> Character, which in this case is the o at the end of "foo".




d T <?> : foo_ bar → foob̲ar

Like the previous command, we can use d elete un T il (with lowercase "t") to remove characters forward to the next character <?> ( B in the string "for this example). To achieve this result, the same result is achieved, as d w .




d i w : Foo _ bar → foob̲ar

If our cursor is in the middle of a space, we can use the d elete i nner w ord command to remove spaces from both sides.




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

If the spaces we want to remove include line breaks, we can use the d elete command above to match the <?> Pattern, where the pattern in this case is only the first character without spaces. As shown, press Enter to execute this command.

When the first character without spaces appears at the beginning of the line after the break, Vim will remove the space, but leave the target on the next line. We need to add J to the above command in J in lines (uppercase "j").

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

+3
Oct 25 '17 at 19:55
source share

d w should work.

+2
Aug 7 '11 at 20:13
source share



All Articles