Select by camel case - the greatest ReSharper setting you never knew
Posted on Monday, 6th June 2016
One of ReSharper’s most powerful features is the sheer number of additional shortcuts it adds to Visual Studio, and out of the arsenal of shortcuts available my most used shortcuts have to be the ones that enable me to modify and manipulate code in as fewer wasted keystrokes as possible. Ultimately, this boils down to the following two shortcuts:
1: Expand/Shrink Selection (CTRL + ALT + Right to expand, CTRL + ALT + Left to shrink)
This shortcut enables you to expand a selection by scope, meaning pressing CTRL + ALT + Right to expand your selection will start by highlighting the code your cursor is over, then the line then function scope, class and namespace. Check out the following gif to see an example. Be sure to watch the selected area!
Expand/Shrink Selection:
2: Increase selection by word (CTRL + Shift + Right to expand, CTRL + Shift + Left to shrink)
A few of you will probably notice that this shortcut isn’t really a ReSharper shortcut - and you’d be right. But none the less, once harnessed, increase/decrease selection by word is extremely powerful when it comes to renaming variables, methods, classes etc. and will serve you well if mastered.
Where am I going with this?
Whilst the aforementioned shortcuts are excellent tools to add to your shortcut toolbox, one thing I always wished they would do was the expand selection by camel case, allowing me to highlight words with more precision and save me the additional key presses when it comes to highlighting the latter part of a variable in order to rename it. For instance, instead of highlighting the whole word in one go (say, the word ProductService, for example), it would first highlight the word Product, followed by Service after the second key press.
Having wanted to do this for sometime now, I was pleasantly surprised when I stumbled across a ReSharper setting to enable just this. This setting can be enabled by going to ReSharper > Options > Environment > Editor > Editor Behaviour and selecting the Use CamelHumps checkbox.
The problem I’ve found when enabling this is the setting overwrites the default behaviour of CTRL + ALT + Left / Right. Whilst this may be fine for some, I would rather the ability to choose when to highlight by word and when to highlight by camel case. Luckily you can do just this via the _ReSharperHumpPrev and _ReSharper_HumpNext_ commands that are available for binding in Visual Studio.
To do this do the following:
- Open the Visual Studio Options window from Tools > Options
- Expand Environment and scroll down to Keyboard
- Map the two commands _ReSharper_HumpNext_ and _ReSharper_HumpPrev_ to the key mappings you wish (E.g. ALT+Right Arrow and ALT+Left Arrow) by selecting the command from the list and entering the key mapping in the Press shortcut keys text box, then click Assign.
Now, with UseCamelHumps enabled and my shortcut keys customised, I can choose between the default selection by string, or extend selection by camel case - giving me even more code-editing precision!
Enjoy this post? Don't be a stranger!
Follow me on Twitter at @_josephwoodward and say Hi! I love to learn in the open, meet others in the community and talk Go, software engineering and distributed systems related topics.