Extend/Shrink Selection
January 25th, 2008 by Jura GorohovskyNote: This is the first post from a series that we’re hoping to maintain on a regular basis. Tagged as “ReSharper in Detail”, 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:
- You place the caret at an arbitrary position – in this example, at a field name in a method call.

- 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:

- 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:
- Press Ctrl + W again. ReSharper extends the selection to cover the field property:

- The next time you hit the shortcut, the method name is added to the selection – without parentheses though:

- Next expansion – parentheses are added:

- We’re done with the method call expression. The next time you press Ctrl + W, the whole line is selected:

- 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).
January 26th, 2008 at 11:15 am
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
January 28th, 2008 at 1:42 pm
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.
January 29th, 2008 at 7:02 pm
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.
January 29th, 2008 at 7:36 pm
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!
February 2nd, 2008 at 2:45 am
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?
February 6th, 2008 at 10:32 pm
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.