user2475983
user2475983

Reputation: 2112

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.

Upvotes: 165

Views: 104615

Answers (8)

Supun Sandaruwan
Supun Sandaruwan

Reputation: 2418

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.

Upvotes: 2

CozyAzure
CozyAzure

Reputation: 8468

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.

Upvotes: 4

codigube
codigube

Reputation: 1246

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

Upvotes: 21

Chandan Nayak
Chandan Nayak

Reputation: 10877

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

Upvotes: 265

Chagai Friedlander
Chagai Friedlander

Reputation: 320

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.

Upvotes: 12

Alex T
Alex T

Reputation: 1366

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

Upvotes: 19

CornelC
CornelC

Reputation: 5254

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.

Upvotes: 68

Annette V
Annette V

Reputation: 51

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.

Upvotes: 2

Related Questions