Use Ctrl+Click to Select Word in VS Code
Asked Answered
P

6

44

In Visual Studio, if you hold CTRL and click on word, it selects the entire word. If you drag, it selects text word-by-word.

I find this feature of Visual Studio very useful when I'm copy pasting small bits of code, since I can just keep holding CTRL, select words, and press C, X, or V to move stuff around.

In VS Code, you can't do this. Instead, CTRL+CLICK is bound to "Go To Definition".

Is there any way to match the behavior of VS Code with Visual Studio in this context?

Polled answered 15/4, 2017 at 16:10 Comment(4)
The feature doesn't exist yet. The issue is tracked here.Arlon
You could try double clicking on the relevant wordBensen
You can mark this question as answered or close this question. This feature is now available.Constrictive
The closest I've found is to click on a word then press Ctrl + D or Cmd + D on mac.Franglais
P
9

As @phuzi said in the comments you can use double click to select the word or double click and drag to select word to word (it will snap on the last character of each word). If you triple-click on a line or click on line num, it will select the whole line (with the invisible character at last '\n').
If you press CTRL + D it will select the word where the cursor is. Also if there are multiple instance of same word you can select them all one after another using CTRL + D.

Planetstruck answered 21/8, 2020 at 18:15 Comment(2)
This answer is really legit, except for the «...and press C, X, or V to move stuff around» part, where you will have, anyway, to click CTRL to copy/cut/paste which therefore makes it almost exactly like what Zeenobit asked for... I didn't know about that and I think this is really useful... This answer should be upvoted... at the moment of writing this, it was downvoted to negative 2 for some reason where the aforementioned comment by @Bensen was upvoted...Certitude
The nice thing about this double-click + drag tip is that it works everywhere outside of VS Code as well. Thanks for this great knowledge nugget!Lexical
A
3

I am the author of the source code change mentioned in the other reply. It appears my code will not be merged since it has been over a year.

My code changes can be found here if you want to build from source, but I also uploaded compiled binaries for Windows which can be downloaded in the Release tab.

If you build your own version you will need to modify product.json to get the Extension Gallery to work as described here. You can also install the extension Visual Studio Keymap to get similar key bindings to Visual Studio.

Once in Visual Studio Code you can enable the feature or add the JSON property:

"editor.wordSelection": true

4

Armil answered 8/11, 2022 at 2:24 Comment(0)
A
2

Update for 2022. (Spoiler alert, it is still not here) but there are few catch:

Someone modded the source code

The only promising attempt I found is this attempt on github issue to modify vscode source code to support control click select.

But to really test this you need to be able to build vscode. At the time of writing, his commit is now 10 months old (10000 commits) behind latest and he did not provide any binary build, and I failed to build vscode from source myself so I cannot test this. (npm and yarn is not my domain here)

Which link to this code commit

Turning off ctrl+click for goto definition for now

Although not really a solution to what OP asked. I decided to include this in my answer as other answer did not mention it yet.

For now you can turn off this Ctrl+Click feature with workaround by setting following editor setting to 'ctrlCmd' so that it won't interfere with your copy paste action. enter image description here


Side note & Background:

  • Note that I'm using Visual Studio Keymap because I'm still using VS for main development and don't want to lose shortcut I learned.
    • This extension already bring back VS F12 = 'Go to definition'
    • And many more such as CTRL+D for line dupe.
  • Thus this free up CTRLClick from 'Go to definition' because I already has F12 for that.
  • I also heavily used CTRLClick for 'whole word' selection and Ctrl-click-drag variant of it.
Antonetteantoni answered 19/5, 2022 at 12:34 Comment(4)
I'm the author of the code change. It's unfortunate that it didn't get merged because it doesn't change anything unless you enable it. I haven't touched VS Code since then. I didn't think there was much interest in the fix but I can post a more detailed walkthrough building VS Code with the change and possibly upload the binary somewhere. Also setting up the config is required as well to get it working like I mentioned in the post, but I believe it removes MS telemetry. Any recommendation where to post? I can post on my website, might be too long to post here or github.Armil
Hi @Armil hope you are doing well. 1) For binary release I'm sure you can just host it in your forked github, just drop a compiled zip to your own release page. (github.com/manuelxmarquez/vscode/releases) 2) For setting up instruction on how to build and mod it, the quickest way I can think of is modifying README.md file in your own forked project, it will also display those text in yout main github page. github also provide visual editor, just click to edit your own README.md.Antonetteantoni
PS. For me I'm kinda make do with option I had and finished my job with vscode, too bad that this mouse selection feature is still lacking and I had a habit of using them very much elsewhere :) disabling ctrl-click is enough for me to keep my sanity with vscodeAntonetteantoni
Posting on my GitHub fork is obvious now that you mention it! I must have been working late. I actually still have my previous build, but it is quite outdated as you mentioned. I never tried the releases page, but I'll give that a shot. I would also like more detailed instructions just for myself, so I don't have to figure out how to do it every time I need to use VS Code. I'll see if I can get to it sometime this week.Armil
N
0

You can set multiples cursor by doing ALT+CLICK. You will then select multiples parts of your text/code that you could copy and paste very easily.

Narda answered 23/5, 2020 at 20:19 Comment(0)
R
0

Using a keyboard hook, you could do something like this:

// release CTRL
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_CONTROL;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));

// double click in place
POINT client;
client.x = msStruct.pt.x;
client.y = msStruct.pt.y;
ScreenToClient(hWnd, &client);
const auto mouseLParam = MAKELPARAM(client.x, client.y);                            
SendMessage(hWnd, WM_LBUTTONDOWN, 0, mouseLParam);
SendMessage(hWnd, WM_LBUTTONUP, 0, mouseLParam);
SendMessage(hWnd, WM_LBUTTONDOWN, 0, mouseLParam);
SendMessage(hWnd, WM_LBUTTONUP, 0, mouseLParam);

// press CTRL
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(input));

I have implemented said feature and provided it as a free 3rd party open-source app: https://github.com/dougbenham/CtrlClick

Repel answered 14/1, 2022 at 6:8 Comment(0)
P
0

For any AutoHotkey users, here's a script that will give you the functionality (but not the +drag variant, unfortunately).

#IfWinActive ahk_exe Code.exe
    ~^LButton:: Send {LButton up}{Ctrl up}{Click}^{Left}^+{Right}

You'll still need to follow Wappenull's instructions to change Multi Cursor Modifier to 'ctrlCmd'.

The good thing about doing it this way is that you can use the same script for multiple programs.

Poliomyelitis answered 11/8, 2022 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.