Make Visual Studio understand CamelCase when hitti

2019-01-07 05:35发布

问题:

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.

回答1:

Thanks for those who suggested Resharper. 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



回答2:

Anyone stumbling across this in the year 2015 or beyond (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

(Alternatively, the VSTricks extension includes Sub-word Navigation in its list of nifty features.)

The Subword Navigation project page states:

Look up Edit.Subword in keyboard bindings to set things up.

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

  1. Navigate to Tools > Options > Environment > Keyboard
  2. Under "Show commands containing", type Edit.Subword
  3. For traversing camelcases to the right
    1. Select Edit.SubwordNext
    2. Under "Use new shortcut in", select Text Editor
    3. Under "Press shortcut keys", press ALT + Right Arrow
    4. Click Assign and overwrite the existing shortcut (reassign the existing shortcut first if you don't want to lose that shortcut)
  4. For traversing camelcases to the left
    1. Select Edit.SubwordPrevious
    2. Under "Use new shortcut in", select Text Editor
    3. Under "Press shortcut keys", press ALT + Left Arrow
    4. Click Assign and overwrite the existing shortcut (reassign the existing shortcut first if you don't want to lose that shortcut)
  5. For selecting camelcases to the right
    1. Select Edit.SubwordNextExtend
    2. Under "Use new shortcut in", select Text Editor
    3. Under "Press shortcut keys", press ALT + SHIFT + Right Arrow
    4. Click Assign and overwrite the existing shortcut (reassign the existing shortcut first if you don't want to lose that shortcut)
  6. For selecting camelcases to the left
    1. Select Edit.SubwordPreviousExtend
    2. Under "Use new shortcut in", select Text Editor
    3. Under "Press shortcut keys", press ALT + SHIFT + Left Arrow
    4. Click Assign and overwrite the existing shortcut (reassign the existing shortcut first if you don't want to lose that shortcut)
  7. For deleting camelcases to the right
    1. Select Edit.SubwordDeleteNext
    2. Under "Use new shortcut in", select Text Editor
    3. Under "Press shortcut keys", press ALT + DELETE
    4. Click Assign and overwrite the existing shortcut (reassign the existing shortcut first if you don't want to lose that shortcut)
  8. For deleting camelcases to the left
    1. Select Edit.SubwordDeletePrevious
    2. Under "Use new shortcut in", select Text Editor
    3. Under "Press shortcut keys", press ALT + BACKSPACE
    4. Click Assign and overwrite the existing shortcut (reassign the existing shortcut first if you don't want to lose that shortcut)


回答3:

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".

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



回答4:

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



回答5:

Resharper supports that feature.



回答6:

Resharper does that. I suppose you could write your own addin to do the same.



回答7:

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.



回答8:

Add the macro below to the visual studio.

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



回答9:

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.