How to select whole word under the cursor in Visual Studio Code? - visual-studio-code

What hotkey or command helps to select the whole word under the cursor in VSC?
(Like CTRL or ALT + D in ST3)

To select the word under the cursor in VSCode:
ctrl + d

Note: the definition of a "word" is different when your document edited with VSCode is a markdown one.
That is why VSCode 1.52 (Nov. 2020) proposes:
Markdown inline smart select
Expand and shrink selection in Markdown documents using the following commands:
Expand: kb(editor.action.smartSelect.expand)
Shrink: kb(editor.action.smartSelect.shrink)
Selection applies to italics, bold, inline code blocks, and links.

Another simple way is to double click the mouse left button.

Related

How to disable highlight of full string in VS Code?

I have an issue with double clicking and highlighting in VS Code (v 1.71.2).
Sometimes the double-click does not select the word double-clicked.
In this screen recording you can see the issue:
Every time I used a double click.
First time the word "Condition" was not selected, but the whole string literal.
Then it was selected but the whole line was highlighted and when copying, only "Condition" would be pasted. That is correct.
The time that the word "Condition" was colored in fuchsia is the correct one but I want only that word to be highlighted instead of the entire string.
Anyone know the setting for this?
This issue was reported and fixed already, see:
Double Clicking On A Word Does Select The Entire String, Not Just The Word · Issue #125052 · microsoft/vscode · GitHub
Try to re-install the version 1.71.2 for release "August 2022 Recovery 2" and see if double-click selection works as expected.
Note that different click-counts have different outcomes:
double-click should select the word (using configured word-separators)
triple-click should select the entire string
quadruple-click should select the entire text
Configure word-separators with property editor.wordSeparators from preferences: Visual Studio Code—Customizing word separators
See also related: Double-click to select text does not always work · Issue #84319 · microsoft/vscode · GitHub.
To have finer control over selecting text you can use keyboard shortcuts like explained in Select all text between quotes in VS Code?.
I concluded that the issue was that the string was highlighted due to the cursor being placed somewhere inside the string.
To disable this, I disabled the Occurrences Highlight ("editor.occurrencesHighlight": false)
By doing so, the editor did not highlight open/closing tags though.
The (partial) solution to this was to install the extension Highlight Matching Tag

In VSCode, how can I select current element and it"s surroundings like in WebStorm?

In WebStorm you can select an element and it's surrounding when you do Alt + Arrow Up.
How can you achieve that in VSCode ?
You can try with VSCode buit in Emmet: Balance (outward) command: Ctrl+Shift+A
If the shortcut does not work, follow these steps:
Press Ctrl+Shift+P to open the Command Palette
Type Emmet
Select Emmet: Balance (outward)
This will select the tag where you have your cursor currently in, and all its contents (I understand that by its surroundings you mean the tag contents and the tag itself).
If the tag has as a content a combination of text and tags and your cursor is in the text, the command will select all the content. A new hit of the command will grow the selection to include the tag itself.

Is it possible to toggle text to strikethrough text in Visual Studio Code?

I want to keep track of a todo list in Visual Studio Code.
Is it possible to display/toggle strikethrough text:
task not yet done
̶t̶a̶s̶k̶ ̶d̶o̶n̶e̶
(used https://www.piliapp.com/cool-text/strikethrough-text/ for the above).
Looking for a solution natively or with an extension. The strike-through text needs to be displayed in the text file (like the functioning of org mode in emacs) and not in some output window (like the rendering of a HTML/Latex/MD document).
Any experts in Visual Studio Code that know if this can be done and how to do it ?
You can make strikethrough in vscode with the help of TODO Highlight extension:
"todohighlight.keywordsPattern": "(~~.+?~~)|(✔.+?\n)|(\\[x\\].+\n)",
"todohighlight.defaultStyle": {
"color": "none",
"backgroundColor": "none",
"textDecoration": "line-through"
},
Use patterns that you usually use for done todos. The above works for such:
[x] done
✔ done
~~done~~
After that you can either use plain typing or find some toggling extension or write one yourself.
Try the extension "Strike for VSCode", looks very lightweight extension.
Highlight the text you want to strikethrough and hit CTRL K + backspace.
CTRL K + backspace again to toggle.
You can use the built in function of adding "~~" before and after the text
so its like:
~~strikethrough~~
which strikes the words in between.
This can be done without extensions.

What is the property name to break long lines in VS Code?

When I have long text line I would like to show it next line.
What is the property name in VS Code?
The menu under File > Preferences or press Ctrl+, (on Mac Code > Preferences > Settings or press Command (or Cmd) ⌘+,) provides entries to configure user and workspace settings. You are provided with a list of default Settings.
Set editor.wordWrap: on in your User Settings or Workspace Settings under preference.
Select the below options to change to the desired settings.
Off - Lines will never wrap.
on - Lines will wrap at the viewport width.
wordWrapColumn - Lines will wrap at "Editor: Word Wrap Column".
bounded - Lines will wrap at the minimum of viewport and "Editor: Word Wrap Column".
You can toggle word wrap for the VS Code session with Alt+Z (macOS: Option (or Alt) ⌥+Z) or select View > Word Wrap from Menu.
For more about User and Workspace Settings or Key Bindings for Visual Studio Code
On Windows, press Alt + Z (macOS: Option ⌥ + Z) to toggle word wrapping, or, select View > Toggle Word Wrap from VSCode Menu.
For Windows, Pressing Alt+Z will break the line.
For Windows users do the following in -->
Open VScode
Go to Settings(Gear icon on the bottom-left side)
Click Settings in the menu
In the searching bar search for "word wrap"
Click the drop-down menu and change it to "on"
This setting will wrap your words according to your editor: viewport.
In case this auto wrapping isn't working you can press --> Alt + z to wrap content
In VS Code:
Install Beautify plugin. (or any Formatter)
Go to the file & select the desired part.
Right-Click on it & select Format Selection Or press Ctrl + k Ctrl + F.
Hope this helps.
HTML > Format: Wrap Line Length
set the maximum number of characters (for example - 1000)
You can press Ctrl + W / Ctrl + L to toggle word wrapping or select Main menu "Edit>Advanced>Word Wrap" .

How to indent/format a selection of code in Visual Studio Code?

I want to indent a specific section of code in Visual Studio Code.
I read How do you format code in Visual Studio Code? that gives shortcuts to indent the whole code, but it doesn't work when selecting a specific section of code.
I tried Ctrl + Shift + F after selecting some line in my code, but the whole file is indented. I'm on Windows with Visual Studio Code Insider 1.8.0. How can I do it?
I want to indent a specific section of code in Visual Studio Code:
Select the lines you want to indent.
Use Ctrl + ] to indent them.
If you want to format a section (instead of indenting it):
Select the lines you want to format.
Use Ctrl + K, Ctrl + F to format them.
You can also indent a whole section (multi-lines) by selecting it and clicking
TAB
and also indent backward using Shift+TAB
And of course for auto indentation and formatting, following the language you're using, you can see which good extensions do the good job, and which formatters to install or which parameters settings to enable or set. For each language and its available tools. Just make sure to read well the documentation of the extension, to install and set all what it needs. Exemple: prettier is the most common used formatter for JavaScript and typescript. And it's widely used by all projects and code style requirements and setup. And in CI pipelines.
Up to now the indentation problem bothers me with Python when copy pasting a block of code. If that's the case, here is how you solve that: Visual Studio Code indentation for Python
On OS X, choose "Document Format", and select all lines that you need format.
Then Option + Shift + F.
(This works at least up to version 1.74.2, checked in Jan 2023)
On macOS Visual Studio Code version 1.36.1 (2019)
To auto-format the selection, use ⌘K ⌘F (the trick is that this is to be done in sequence, ⌘K first, followed by ⌘F).
To just indent (shift right) without auto-formatting, use ⌘]
As in Keyboard Shortcuts (⌘K ⌘S, or from the menu as shown below)
This should be able to set to whatever keybindings you want for indent/outdent here:
Menu File → Preferences → Keyboard Shortcuts
editor.action.indentLines
editor.action.outdentLines
F1 → open Keyboard Shortcuts → search for 'Indent Line', and change keybinding to Tab.
Right click > "Change when expression" to editorHasSelection && editorTextFocus && !editorReadonly
It will allow you to indent line when something in that line is selected (multiple lines still work).
For German keyboard layout, the standard settings are:
Indent selection: Strg + ´
Outdent selection: Strg + ß
As you've seen there are two ways to indent the code (this for Windows).
Reindenting the entire file
Shift+Alt+F
Reindenting only selected lines
First set the shortcut for Reindent Selected Lines
Menu File → Preferences → Keyboard Shortcuts → In the Search in keybindings type in Reindent Selected Lines → Select it and press Enter → Type in your own shortcut, e.g. Shift + 5, followed by Enter
Now select your code lines in the editor and use the shortcut set above, e.g. Shift + 5, to automatically indent those lines only.
On windows its "Ctrl+[" and "Ctrl+]" for indent and unindent You can find rest of the shortcuts here
For mac, you can find the shortcuts here: https://code.visualstudio.com/docs/getstarted/keybindings
For me on windows it was Ctrl+¡ , indent line. It adds a tab at the beggining of each line.
Many of the answers were not able to solve my problem too.
Just go for fn+tab
Welcome in advance.
On linux ubuntu: select text then ctrl + shift + i
This is the way I had my code before formatting...
Then I used the command like this... (Make sure to select the code part that you need to format)
Shift+ Alt+F
And I got the formatted code like this....
For me, using a mac in 2022 it was CMD + ] to indent multiple lines after selecting the desired indented lines.
Crtl + Alt + F can also formate (windows)
Windows - 2022
Shift+Alt+F