VIM: Deleting from current position until a space

Vim

Vim Problem Overview


Often when developing I am confronted with a nested object that I'd like to delete from code in the middle of a line like this:

htmlDoc.WriteLine("<b><h3>" + this.cbAllSyncs.SelectedItem.ToString() + "</h3></b>");

The part that I'd like to delete is:

this.cbAllSyncs.SelectedItem.ToString()

I know I can count the number of words and periods and enter 7dw to delete from my current cursor position of "this". However, what I'd love to do is not have to count at all and delete to the space with one command. Is this possible?

Vim Solutions


Solution 1 - Vim

Try dtspace. In general dtx deletes from current position till just before x. Just tx moves the cursor to just before character x in current line.

To delete up to and including the space, use dfspace.

Solution 2 - Vim

You can use dW or dE as @glenn suggested if you don't want to delete the space itself.

A WORD (uppercase W) consists of a sequence of non-blank characters, separated with white space.

Give a look to the word motions.

Solution 3 - Vim

one possible solution is to use the delete with a search.

so type in d/<space> and vim will delete until it hits a space.

Solution 4 - Vim

If you want to delete from anywhere inside the WORD, and in a single command, just use daW

(you can of course use daw if you want to limit to a word)

I use it quite a lot because it spare a move to the begining of the word (in your case)

Solution 5 - Vim

  • delete for the current position to a specific character example "

    dt"

  • delete the word from the position cursor is on till the end of the word

    dw

  • delete the entire word the cursor is on irrespective of the position the cursor is on the word, this also puts you in insert mode to enable you to insert immediately.

    diw

Solution 6 - Vim

dtspace is the answer to your question, but df+ looks like it will solve your problem better.

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
QuestionBlake BlackwellView Question on Stackoverflow
Solution 1 - VimamitView Answer on Stackoverflow
Solution 2 - VimChristian C. SalvadóView Answer on Stackoverflow
Solution 3 - VimbarkmadleyView Answer on Stackoverflow
Solution 4 - VimLouisView Answer on Stackoverflow
Solution 5 - VimNaseeruddin V NView Answer on Stackoverflow
Solution 6 - VimDan GoldsteinView Answer on Stackoverflow