Extend/Shrink Selection

Note: This is the first post from a series that we’re hoping to maintain on a regular basis. It will cover specific features provided by ReSharper with reasonable (or even redundant, for true ReSharper Jedis) details, links to other valuable web resources and probably some additional insight. You can think of it as extended online help for those users who feel not very comfortable learning or navigating through ReSharper functionality, as well as for those eager to gain additional expertise.

The Extend Selection feature allows you to successively select expanding blocks of code. You can easily select any expression in the code by clicking somewhere inside it and pressing Ctrl + W (or Ctrl+Alt+Right Arrow in the Visual Studio keyboard layout) a few times, or, alternatively, by choosing ReSharper | Code | Extend Selection.
 
To be specific, Extend Selection works the following way:
 

  1. You place the caret at an arbitrary position – in this example, at a field name in a method call.
  2. You press Ctrl + W for the first time.
     

    • If you selected Use CamelHumps in ReSharper | Options | Editor, the initial selection is made covering the current part of field name that begins with an uppercase character:

      It’s only after you’ve pressed Ctrl + W once more, the whole current word is selected.
    •  

    • If CamelHumps is disabled, ReSharper selects the complete field name straight away:
  3. Press Ctrl + W again. ReSharper extends the selection to cover the field property:
  4. The next time you hit the shortcut, the method name is added to the selection – without parentheses though:
  5. Next expansion – parentheses are added:
  6. We’re done with the method call expression. The next time you press Ctrl + W, the whole line is selected:
  7. How the feature acts further depends on the structure of your code. In our example, the method call belongs to the body of a conditional statement. That’s why the next time you hit the shortcut, the whole body is selected, excluding braces:
     

 
In this example, consequent keystrokes extend the selection to cover the following:
 

  • Body of the conditional statement including braces;
  • The conditional statement including condition;
  • Body of the method containing the conditional statement;
  • The entire containing method;
  • The class that this method belongs to;
  • The entire file.

 
Obviously enough, Shrink Selection does exactly the opposite. Press Ctrl + Shift + W (or Ctrl+Alt+Left Arrow in the Visual Studio keyboard layout) or choose ReSharper | Code | Shrink Selection a number of times, and you can eventually find your selection cut down to the initial position of the caret in strictly reverse order.
 
This feature is very intuitive but please be aware that there are several scenarios where it should work on a limited basis or not work at all.
 

  • ReSharper doesn’t process HTML and JavaScript. Because of that, Extend/Shrink Selection interprets any block of JavaScript or HTML code as an integral entity within a pair of tags and only discerns specific words within this block. In other words, when you’re working on an aspx file and you place the caret at a certain position within a JS or HTML block of code and you hit Ctrl + W, ReSharper selects the current word, and upon the next keystroke it selects the entire block of code within a pair of containing tags (for example, in case of JavaScript).
  •  

  • You should start with extending a selection using Ctrl + W in order to be able to shrink it back later. In other words, if you made a random selection with your mouse or by clicking Ctrl + A, you can’t shrink it using Ctrl + Shift + W because in this case ReSharper doesn’t know exactly what file member you want to cut the selection to.
  •  

  • Extend/Shrink Selection supports CamelHumps-based selections within both C# comments (//, ///, /**/) or VB.NET comments. However, commented code is not parsed and therefore interpreted as an integral entity (see note on JavaScript and HTML above).
This entry was posted in How-To's, Links and Opinions, ReSharper Tips&Tricks and tagged . Bookmark the permalink.

8 Responses to Extend/Shrink Selection

  1. Paulius says:

    Hi,
    could we have CamelHumps not as turn on/off feature but as a separate shortcut? Or at least it could be turned on/off with a shortcut. It’s rather useful actually (CamelHumps), but it’s like 30/70 for me, where I would use 30% for selection with CamelHumps and 70% without it, so currently it does not justify turning it on every time it is needed for a short period of time..
    thanks

  2. Jura Gorohovsky says:

    Hi Paulius,
    ReSharper provides four actions for using CamelHumps locally when the corresponding global option is switched off:
    ReSharper_HumpNext – move caret to next hump/word
    ReSharper_HumpPrev – move caret to previous hump/word
    ReSharper_HumpNextExtend – expand selection to next hump
    ReSharper_HumpPrevExtend – expand selection to previous hump
    Of course, you can assign to them whatever shortcuts you feel appropriate in VS Tools > Options > Environment> Keyboard
    Note that both HumpNextExtend and HumpPrevExtend only work in a linear fashion, without additional functionality of Extend/Shrink Selection
    Hope that helps.

  3. Joe White says:

    So if you have the CamelHumps option turned on, Ctrl+W once will select a single sub-word, but the second time will select the larger expression that the word is in? I would’ve expected the second time to move up to the word — i.e., to have the option add one more step to the beginning of the sequence, without actually removing any functionality.

  4. Jura Gorohovsky says:

    Hi Joe,
    No, selecting a subword is certainly followed by selecting the whole containing word.
    That was my fault, ambiguous wording. Thanks for taking note!

  5. Ted Jardine says:

    Argh. I’d love to use this shortcut, but it continues to wait for the second key of the chord. I have made sure that this specific shortcut has not been overwritten by restoring ReSharper keyboard shortcuts and still no go. Moreover, the VS ctrl-alt-right arrow shortcut doesn’t work and the Resharper -> Code -> Extend Selection does not have the ctrl-w shortcut description next to it while the shrink selection (ctrl-shift-w) does (and does work).

    Am I missing something completely obvious here?

  6. Ted Jardine says:

    If like me your mapping has gotten all mixed up and resetting ReSharper keyboard shortcuts in just ReSharper options does little or nothing, try resetting key mappings in VS first (of course, all your customized mapping will be lost unless you restore etc.). Tools -> Options -> Environment -> Keyboard -> Then click reset on the applicable keyboard mapping scheme. Then go to ReSharper -> Options -> Environment -> General -> then restore ReSharper keyboard shortcuts.

  7. Chris says:

    Would love if ctrl-w stopped at commas so I could use it to easily snip out things from the middle of list.

    public void Foo(Foo old, Foo new, Foo blue) ()

    would love to be able to ctrl-w to select “Foo old,” and/or “, Foo blue”

    • Jura Gorohovsky says:

      Chris, Extend/Shrink Selection doesn’t stop at commas indeed.
      However I was thinking why you would need it to stop at commas and I thought that you probably wanted to reorder parameters. If this is the case, you might want to use Ctrl+Shift+Alt+arrows for Reorder Parameters instead. Alternatively, if you’re trying to rearrange parameters in methods that have usages in your code base, consider the Change Signature refactoring.
      Please let me know if reordering members was a wrong guess.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">