ChatGPT解决这个技术问题 Extra ChatGPT

How to select current word in Visual Studio Code (VS Code)?

How to select the current word, that is where the caret is at.

Note: I am looking for the shortcut for Visual Studio Code(VS Code), the text editor, and not Visual Studio IDE.


M
Mwiza

On Mac OS: Cmd+D On Windows & Linux: Ctrl+D

Above solved the purpose for me.

But ⌘D is defined as "editor.action.addSelectionToNextFindMatch", so if you press it more than once, it will try to search and select same word in the file which then can be used to do "multi word editing".


Thank you! Now together with drag and drop I feel whole again!
This has the unfortunate side-effect of also changing the current "find" criteria.
search Add Selection To Next Find Match in hotkey search bar
C
CornelC

You are looking for Shrink/Expand Selection.

Trigger it with Shift+ Alt+Left and Shift + Alt+Right

Update:

This is now called Smart select API. This feature uses semantic knowledge to intelligently expand selections for expressions, types, statements, classes, and imports.


This has unfortunate side-effect of also selecting the spaces around the word.
And it often selects more than just the word
Simply misleading, selects more than one word as it extends to, e.g., the surrounding parentheses.
I agree with Ctrl+D altering the "find" criteria, but I haven't found any of the caveats regarding this answer to be true. Have things changed? If not give examples please (I've found it selects the word within parentheses just fine).
Coming from Jetbrains editors (IntelliJ and PHPStorm), I find this closest to the Ctrl+W "select word" behavior, and maybe even a little better. Thanks!
A
Alex T

https://i.stack.imgur.com/xU7uU.png

https://i.stack.imgur.com/DCxy8.png

https://i.stack.imgur.com/JFi6M.png


M
Mwiza

It is Ctrl + D that works for me in latest Visual Studio Code on Windows.

Go to File -> Preferences -> Keyboard shortcuts, you will find this:

https://i.stack.imgur.com/wLKSj.png


This has the unfortunate side-effect of also changing the current "find" criteria.
C
Chagai Friedlander

Shift + Alt+Right Arrow if the word is in camelCase then you will have to click Right Arrow again to select the whole camelCase. Every time you press Right Arrow again while still holding Shift + Alt down you will select a further part of the code. so:

first the word. then if it's part of a camelCase then the camelCase. then if it is in a string the whole string. ... (many other posibilities) the whole line. everything inside the parentheses code block the whole file

at any given time you can go back to the last selection by clicking Left Arrow instead of Right Arrow

I don't know about CTRL + w in the old Visual Studio Code but in the JetBrains IDE's this is the equivalent to CTRL + w by holding down CTRL and clicking w to select more and holding down CTRL + Shift and clicking w to unselect.

Another possibility which helps to avoid selecting only one word in camelCase is CTRL + d this will just select the whole camelCase. This will however have the side-effect of also changing the current "find" criteria.

thanks Chandan Nayak for this extra shortcut.


C
CozyAzure

An unpopular opinion: you can now have Resharper keybindings, if you come from Jetbrain's camp.

The Ctrl+W expansion grow and shrinks is different from expansion selection.


A
Annette V

On "File/Preferences/Keyboard Shortcuts" I deleted the shortcut "Ctrl + W" to close the current tab action, because for this "Ctrl+F4" works for me.

Update (14 days later): Yesterday I installed VSCode 1.34.0 - I think since then the functionality is "Ctrl + D". I was very suprised.


S
Supun Sandaruwan

For any editor, you can use the below shortcuts. These shortcuts work for every text area also.

Ctrl + Shift + LeftArrow/RightArrow - this will select text word by word

Shift + UpArrow/DownArrow - this will select text line by line

Ctrl + BackSpace - this will delete text word by word

Additional

in intellijIdea Ctrl + w - use for the select current word, after giving second Ctrl + W it will select the second word also. Like that you can select the whole line.

Ctrl + d - you can duplicate current line.