7

The first invocation of the Expand Selection (Alt-Shift-RightArrow by default) in VSCode selects camel-case sub-word under the cursor. For example, when the cursor is placed inside a camel-case identifier (such GetComponentsInChildren), it selects "Components" or "Children".

I have tested that in C# source code. I have "C# for Visual Studio Code (powered by OmniSharp)" extension installed.

Is it possible to configure Expand Selection to include the whole identifier GetComponentsInChildren? I really prefer it to be consistent with the behavior of other navigation and selection options (Ctrl-RightArrow - jump to the right, Ctrl-D - add selection to next find match)?

3 Answers 3

7

As of today (actually the June 2023 update, or 1.80), there's a setting that controls this exact behaviour, namely "editor.smartSelect.selectSubwords"; if you set it to false, the editor will select the whole word the first time you expand the selection without paying attention to subwords in camel case (or any other case for that matter).

Still, I have to say I appreciate the ingenuity of the other answers - there's a lot that can be done with macros and when clause contexts.

Sign up to request clarification or add additional context in comments.

Comments

5

Originally I thought you could simply make a macro to run 2 smartSelect.expand commands in a row (as in the commented code below). That does work - except for single words like someword with no camelCase - and then the first grow selects the entire word under the cursor and the second grow selects the line or enclosing block, not what you want.

"multiCommand.commands": [ // in settings.json { "command": "multiCommand.selectWord", "sequence": [ // "editor.action.smartSelect.expand", // "editor.action.smartSelect.expand", "cursorWordStartLeft", "cursorWordRightSelect", ] } ] 

So I looked for another way to do it and I think I found it with the macro above (which uses the multi-command extension). And this in keybindings.json:

{ "key": "shift+alt+right", // disable so you can use the same keybinding "command": "-editor.action.smartSelect.expand", "when": "editorTextFocus" }, { "key": "shift+alt+right", // trigger the macro with the same keybinding if you wish "command": "extension.multiCommand.execute", "args": { "command": "multiCommand.selectWord" }, "when": "editorTextFocus" }, 

The downside to this approach is that you lose the smartSelect.expand beyond the current word - it will not expand to the containing block for example. You decide if that is important to you. If you use a different keybinding then you don't have to disable the smartSelect grow command and you can have both options.

demo of selecting whole word


As of v1.44 the behaviour of Add Selection to Next Find Match has changed in how it calculates the word definition. Perhaps it is better for you.

The command Add Selection to Next Find Match (kb(editor.action.addSelectionToNextFindMatch)) now respects the setting editor.wordSeparators. Previously, it used to pick up the word definition as defined by the current file's language.

4 Comments

Thanks for the detailed investigation! Marking it as an answer, even though it essentially means this option cannot be configured at the moment (workaround is too limiting). :) Just FYI, the best approximation I am using for now is Ctrl/Cmd+D. It selects full word under the cursor on first invocation (and next entry of the same word on the next invocation).
> "The downside to this approach is that you lose the smartSelect.expand beyond the current word" To fix this, you can modify your "when" clause to this: "when": "editorTextFocus && !editorHasSelection"
where is this multiCommand.selectWord coming from?
@slier It is shown in the answer defined in the settings.json.
1

There was a comment about adding more to the when clause. That didn't exactly work for me, but I was able to add to it to get the result I was expecting.

my keybindings.json:

... { "key": "cmd+up", // disable so you can use the same keybinding "command": "-editor.action.smartSelect.expand", "when": "editorTextFocus" }, { "key": "cmd+up", // when the editor has a word, grow the selection (this ignore camel case) "command": "extension.multiCommand.execute", "args": { "command": "editor.action.smartSelect.grow" }, "when": "editorTextFocus && editorHasSelection" }, { "key": "cmd+up", // when the editor doesn't have a word selected, select entire word, ignoring camelcase (editor.action.smartSelect.grow won't ignore camel case on first word select) "command": "extension.multiCommand.execute", "args": { "command": "multiCommand.selectWord" }, "when": "editorTextFocus && !editorHasSelection" } ... 

my settings.json (unchanged from original answer):

... "multiCommand.commands": [ { "command": "multiCommand.selectWord", "sequence": [ "cursorWordStartLeft", "cursorWordRightSelect", ] } ] ... 

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Note that this requires installing the multi-command extension to work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.