4

Is there the way to apply "greedy" behavior to and keys in Visual Studio? By "greedy" I mean such behavior when all whitespace between cursor position and next word bound can be deleted using one keystroke.

9 Answers 9

7

Well, I don't think you can change the binding of the delete key or backspace key - but CTRL+DEL & CTRL+Backspace are pretty close to what you want.

3

You can use Ctrl+Shift+Arrow keys to make the selection and then just hit Delete. You may need to hit the arrow key more than once while still pressing Ctrl+Shift combination but because the fingers are in the same position is very fast. This works also for selecting words incrementally.

3

Actually, you will need to do this: Ctrl+Shift+Left+Right - this will give you only the space selected, and then you can press delete.

This is assuming that you are coming from the right, and you have to delete the space to the left.

Of course, this is still 5 keystrokes... but it beats pressing backspace again and again....

2

Just Ctrl+Backspace...

2
  • This will erase all space AND the last word too... I just tested it. Sep 18, 2008 at 13:10
  • if your cursor is in the middle of a word ctrl-backspace and then ctrl-delete will get rid of the whole word
    – OzBob
    Nov 27, 2018 at 2:36
2

Ctrl+Back Space and Ctrl+Delete are also greedy, they delete the nearest word in their respective direction.

1

You are looking for:

Edit.DeleteHorizontalWhiteSpace

I have it set to Ctrl+K, Ctrl+\ which I think is the default, but might not be

1

Sounds like something you could write a macro for and then assign to a keyboard shortcut (like SHIFT+DEL).

If you explore the EnvDTE namespaces you can do a lot to make changes to text in the active document window. I'd start by checking with something like...

    Public Sub RemoveWhiteSpace()
        DTE.ActiveDocument.Selection.WordRight(True)
        DTE.ActiveDocument.Selection.Text = " "
    End Sub

That's just a simple example, but you can extend it further pretty easily

1

As of recent, ReSharper has this as an option. It's on by default, which led to this Q&A: Visual Studio recent "hungry" or "greedy" backspace behavior update?

Perhaps this doesn't qualify as applying the behavior directly in Visual Studio, but it's good to know about.

0

OK I've got this < Ctrl > thing. And applying this knowledge I've found corresponding VS commands: Edit.WordDeleteToStart and Edit.WordDeleteToEnd.

I've successfully remapped < Delete > and < Backspace > keys using Options->Environment->Keyboard dialog. Unfortunately this commands apply not only to whitespace as I'd wish to, but still, thanks everyone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.