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

Visual Studio-Code

Visual Studio-Code Problem Overview


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.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

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

Solution 2 - Visual Studio-Code

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.

Solution 3 - Visual Studio-Code

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

Solution 4 - Visual Studio-Code

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

Solution 5 - Visual Studio-Code

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.

Solution 6 - Visual Studio-Code

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.

Solution 7 - Visual Studio-Code

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.

Solution 8 - Visual Studio-Code

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2475983View Question on Stackoverflow
Solution 1 - Visual Studio-CodeChandan NayakView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeCornelCView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeAlex TView Answer on Stackoverflow
Solution 4 - Visual Studio-CodecodigubeView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeChagai FriedlanderView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeCozyAzureView Answer on Stackoverflow
Solution 7 - Visual Studio-CodeAnnette VView Answer on Stackoverflow
Solution 8 - Visual Studio-CodeSupun SandaruwanView Answer on Stackoverflow