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

8

157

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.

Trinee answered 27/5, 2016 at 9:35 Comment(0)
K
246

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

Kerguelen answered 28/5, 2016 at 9:13 Comment(4)
Thank you! Now together with drag and drop I feel whole again!Tedmund
This has the unfortunate side-effect of also changing the current "find" criteria.Solicitous
search Add Selection To Next Find Match in hotkey search barWheezy
this also does not select a whole sentece or stringOutwit
P
66

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.

Prepositive answered 6/9, 2017 at 9:3 Comment(8)
This has unfortunate side-effect of also selecting the spaces around the word.Solicitous
And it often selects more than just the wordSolicitous
Simply misleading, selects more than one word as it extends to, e.g., the surrounding parentheses.Equidistant
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).Detruncate
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!Pedestrianize
I think this is what most people refer to when looking at this question. Simply selecting the word is not what I use Cmd+W in IntelliJ, it's the ability to expand the selection as needed. It's not as smart as IntelliJ (goes from selecting a full line to selecting the whole file instead of the method/function), but it gets me 90% of the way there.Uncommunicative
this only select the word, but not select the word combine with _, like word1_word2, only select word1Wheezy
this works if you want to select a whole sentence or stringOutwit
G
21

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:

enter image description here

Girhiny answered 19/3, 2017 at 21:15 Comment(1)
This has the unfortunate side-effect of also changing the current "find" criteria.Solicitous
K
19

If you want to ctrl+w to behave the same as in Idea just go keyboard settings enter image description here

Search for Expand selection. Set new shortcut cmd+w or ctrl+w depending on your OS. enter image description here Also re-bind other commands that use ctrl+w to use another shortcut that you want, for example cmd+f4

You can edit keybindings.json to avoid using UI. enter image description here

Keenakeenan answered 15/8, 2019 at 12:14 Comment(0)
A
10

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:

  1. first the word.
  2. then if it's part of a camelCase then the camelCase.
  3. then if it is in a string the whole string.
  4. ... (many other posibilities)
  5. the whole line.
  6. everything inside the parentheses code block
  7. 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.

Aswarm answered 25/6, 2020 at 8:3 Comment(0)
X
4

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.

Xenolith answered 24/3, 2021 at 8:24 Comment(0)
V
2

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.

Vinaya answered 18/5, 2019 at 10:13 Comment(0)
L
2

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.

Loach answered 2/5, 2021 at 7:17 Comment(1)
CTRL + W (CTRL + SHIFT + W) closes VSCode on WindowsEarmuff

© 2022 - 2024 — McMap. All rights reserved.