Make Visual Studio understand CamelCase when hitting Ctrl and cursor keys
Asked Answered
R

10

164

Is there a way that I can configure Visual Studio 2008 to understand CamelCase? Specifically, I'd like to be able to get Ctrl + right or left cursor to take me to a subsection of a variable or type name.

i.e., if my cursor was at the start of this line:

LongNamedExampleClass longNamed = new LongNamedExampleClass();

and I hit Ctrl + right, I'd like to jump to "Named" in the class name, rather than the start of the variable name. If I hit it again, I'd like to go to "Example". I hope that makes sense.

I used this navigational feature in Eclipse quite a lot, and find it difficult to unlearn something so ingrained.

Raker answered 24/11, 2008 at 13:9 Comment(2)
Resharper supports that feature.Semeiology
Resharper does that. I suppose you could write your own addin to do the same.Chiron
R
139

Thanks to those who suggested Resharper. This gives me another reason to love it even more. Just to be specific about where this lives - JetBrains call it CamelHumps (cute) and you can switch it on using the menu:

Resharper -> Options -> Environment -> Editor -> Editor Behavior -> Use CamelHumps

... in older versions of Resharper, it is;

Resharper -> Options -> Editor -> Use CamelHumps

Raker answered 25/11, 2008 at 10:26 Comment(4)
Is there an option, to select the whole word, even if CameHumps is on? At the moment I have to press right or left several times to select a word. I hope you get me.Grantham
To quickly select the whole word while CamelHumps is on, press the Extend Selection shortcut key twice (Ctrl+Alt+Right or Ctrl+W).Bolden
Not only select the whole word but jumping across the whole word too?Resnick
This is the correct solution for ctrl+[arrow]. However, there are two resharper commands - Resharper_TextControlPreviousWord and Resharper_TextControlNextWord so that you can bind different shortcuts to do the same camel case navigation. However, they don't work if the CamelHumps option is off.Gros
M
141

Visual Studio 2022 and above have this by default with the shortcut keys [Ctrl + Alt + Arrows] for traversal and [Ctrl + Alt + Shift + Arrows] for selection, but [Alt + Delete/Backspace] for deletion need to have Text Editor shortcuts created for them in "Tools > Options > Environment > Keyboard" set to the commands Edit.SubwordDeleteToEnd/Edit.SubwordDeleteToStart respectively.


Note for users of Visual Studio Code

which is a different program from Visual Studio 2022

The shortcut keys you need to manually set are cursorWordPartLeft & cursorWordPartRight for [Alt + Arrows] and cursorWordPartLeftSelect & cursorWordPartRightSelect for [Alt + Shift + Arrows], and they can be set from File > Preferences > Keyboard Shortcuts. However, it does not include anything for [Alt + Delete/Backspace]. For that, Olle Westman ported their extension over to Visual Studio Code, which you can find under the name "Subword Navigation" by ow. Those shortcut keys are under the name subwordNavigation, and I believe they come preset.


Archived Answer below for earlier versions of Visual Studio.


Anyone stumbling across this page after Nov 25, 2008 (who don't want to pay for ReSharper or CodeRush which no longer has a free version), the following free Extension adds this functionality: Subword Navigation by Olle Westman

(Alternatively, the VSTricks extension includes Sub-word Navigation in its list of nifty features. There is also a different Subword navigation extension created by Lauri Vasama not to be confused with Olle Westman's which this answer uses.)

The Subword Navigation project page states:

Look up Subword in keyboard bindings to set things up.

Here's how I set it up in Visual Studio 2019:


  1. Navigate to Tools > Options > Environment > Keyboard

  1. Under Use new shortcut in, select Text Editor

  1. Under Show commands containing, type subword. This should pop up an autocomplete field with the listed commands below.

  1. Select one of the following commands you want to set its shortcut keys for. Once selected, follow the rest of the numbered steps. Then come back to Step 4 to set another one.

    Note: When assigning these Subword Navigation commands to these new shortcut keys, preserve any already-existing shortcut-key functionality you want to keep beforehand by setting them to different keys. It should not conflict with any default-installed shortcut keys.

    Traversal

    • Edit.NextSubword
      • Traverses camelcases to the right [Alt + Right Arrow]
    • Edit.PreviousSubword
      • Traverses camelcases to the left [Alt + Left Arrow]

    Selection

    • Edit.NextSubwordExtend
      • Selects camelcases to the right [Alt + Shift + Right Arrow]
    • Edit.PreviousSubwordExtend
      • Selects camelcases to the left [Alt + Shift + Left Arrow]

    Deletion

    • Edit.SubwordDeleteToEnd
      • Deletes camelcases to the right [Alt + Delete]
    • Edit.SubwordDeleteToStart
      • Deletes camelcases to the left [Alt + Backspace]

  1. Under Press shortcut keys, press Your Shortcut Keys

  1. Click Assign and overwrite the existing shortcut
Makell answered 30/3, 2015 at 22:47 Comment(9)
Thanks for the head's-up! This works great, and is the most open of the presented solutions.Tavy
I meant the year 2015 and beyond. As mentioned later, I used this in VS 2012. I'll clarify in the post. Thanks.Makell
Particularly excellent answer in providing the setup details. I definitely appreciate the explanation on Next/PreviousExtendNador
In 2015, I had to overwrite TextEditor.* commands, rather than Global.* commands. ALT+RIGHT was already bound to something else.Iterative
Seems like this doesn't work anymore on visual studio 2019Addlepated
@yanyankelevich I just tested it in VS2019 and it works. However, I did notice in the Extensions Manager, there were two extensions called "Subword Navigation". You want the one created by Olle Westman. I've updated my answer to specify that and note it works in VS2019.Makell
I went to install this, and started thinking what I would bind to the keyboard shortcut to. "Hm, CTRL + ALT + Arrow probably. Let's see if that's already bound to something. Oh wait, it already does this sub-word navigation". I couldn't find any extensions I have installed that mention this, so it might already be there in VS 2022? (Edit: looks like an answer below denotes that this is new and included in VS2022)Junna
I am using Visual Studio 2022. It does not work! These keys have no effect sadly.Protero
@Protero Looks like VS2022 now allows you to manually set options for rightward and leftward deletion of subwords without requiring an extension. Search for Edit.SubwordDeleteToEnd & Edit.SubwordDeleteToStart in the Keyboard options, and set "Text Editor" shortcuts for them.Makell
R
139

Thanks to those who suggested Resharper. This gives me another reason to love it even more. Just to be specific about where this lives - JetBrains call it CamelHumps (cute) and you can switch it on using the menu:

Resharper -> Options -> Environment -> Editor -> Editor Behavior -> Use CamelHumps

... in older versions of Resharper, it is;

Resharper -> Options -> Editor -> Use CamelHumps

Raker answered 25/11, 2008 at 10:26 Comment(4)
Is there an option, to select the whole word, even if CameHumps is on? At the moment I have to press right or left several times to select a word. I hope you get me.Grantham
To quickly select the whole word while CamelHumps is on, press the Extend Selection shortcut key twice (Ctrl+Alt+Right or Ctrl+W).Bolden
Not only select the whole word but jumping across the whole word too?Resnick
This is the correct solution for ctrl+[arrow]. However, there are two resharper commands - Resharper_TextControlPreviousWord and Resharper_TextControlNextWord so that you can bind different shortcuts to do the same camel case navigation. However, they don't work if the CamelHumps option is off.Gros
K
22

If you have ReSharper installed you can also configure Visual Studio keyboard shortcuts "Alt-Right" and "Alt-Left" for CamelHump, without losing standard Visual Studio behaviour for "ctrl-right" and "ctrl-left".

  1. Open the Visual Studio Options window from Tools –> Options
  2. Expand Environment and scroll down to Keyboard
  3. In Show commands containing box, type "hump". This will show all commands related to CamelHumps. CamelHumps
  4. Map the commands to the key mappings you wish by selecting the command from the list and entering the key mapping in the Press shortcut keys textbox, then click Assign.

Caveats:

  • Assign the mappings for Text Editor, not Global.
  • Make sure the Use CamelHumps option from Resharper -> Options -> Editor -> Editor Behaviour is turned off.

Details here: http://davidrcook.wordpress.com/2009/11/05/working-with-camel-case-in-visual-studio/

Kiki answered 15/8, 2012 at 12:50 Comment(1)
You just made my day. The perfect solution as camel humps can be such a pin with long identifiers (as anyone who uses Eclipse will attest)Conceive
S
8

It's not native to Visual Studio, but you can get it for free using CodeRush Express. (ReSharper and CodeRush require payment).

Slouch answered 24/11, 2008 at 13:42 Comment(1)
Only the trial is free nowadaysNador
W
5

For those using Visual Studio 2022.

Ctrl +Alt + right or left will jump to sub words(camel case navigation).

Whatnot answered 8/2, 2022 at 22:18 Comment(2)
Also if you hold shift it will do selection too as you would expectJunna
Does not work with visual studio professional 2022.Protero
M
2

Visual Studio does not support this behavior. However, DevExpres's CodeRush! product supports navigation like this (using Alt-Left & Alt-Right), along with tons of other features to aid in navigation. The CodeRush! product is also bundled with their Refactor tool. They also offer a free CodeRush! & Refactor Express product, though I'm not certain if the Camel Casing navigation is part of the express product.

Meadows answered 24/11, 2008 at 13:26 Comment(0)
S
2

Add the macro below to the visual studio.

http://visualstudiomacros.blogspot.com/2008/03/getting-ctrl-right-arrow-to-respect.html

Stratopause answered 31/5, 2011 at 15:45 Comment(0)
T
1

For anyone that is looking for this in version ~1.63 The keybinding option is now called "cursorWordPart" and can easyly be set in keyboard shortcuts :)

Tyrontyrone answered 15/12, 2021 at 10:36 Comment(0)
W
0

This extension was created to do that, just like in Eclipse:

https://marketplace.visualstudio.com/items?itemName=maptz.camelcasenavigation

Woo answered 17/6, 2021 at 18:2 Comment(0)
T
-1

I don't think Visual Studio supports this feature internally, as it seems to be using the normal way Ctrl+Right and Ctrl+Left work in Windows environment. But it's probably possible with some kind of extension or plugin.

Trickish answered 24/11, 2008 at 13:13 Comment(1)
Making a guess about whether or not other people are going to be able to answer the question isn't really an answer to the question.Aluminium

© 2022 - 2024 — McMap. All rights reserved.