'Use Ctrl+Click to Select Word in VS Code

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?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

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.

Solution 5:[5]

I would like to share my solution.

Dockerfile

FROM python:alpine3.7

RUN apk update && \
    apk add python3-dev libc-dev libffi-dev gcc &&\
    pip install --upgrade pip

RUN adduser -D user
USER user
WORKDIR /home/user

COPY --chown=user:user requirements.txt requirements.txt
COPY --chown=user:user script.pyx script.pyx
COPY --chown=user:user invoke invoke
COPY --chown=user:user setup.py setup.py

ENV PATH="/home/user/.local/bin:${PATH}"

RUN pip install --user -r requirements.txt

RUN cython script.pyx --embed &&\ 
    gcc -Os -I /usr/local/include/python3.7m -o script script.c -lpython3.7m -lpthread -lm -lutil -ldl &&\
    rm -f script.pyx &&\
    rm -f script.c &&\
    rm -f requirements.txt

ENV arg1=''
ENV arg2=''
ENV arg3=''
ENV arg4='''

ENTRYPOINT ["/bin/sh", "invoke"]

Shell script "invoke"

#!/bin/bash
./script

Thanks to @David Maze for suggestions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4 Benjamin Vincent
Solution 5 MarcinO