Select the current word in VSCode when dots or hyphens are present? - visual-studio-code

How do I select the following words in VSCode:
button-color or button.color ?
Ctrl-D doesn't work, it just selects the portion before/after the . or -. Ctrl-W also doesn't work even after I changed its defautl setting of closing the editor/open tab.

You can also change your Editor: Word Separators setting. Remove the . and - characters and it will work as you want. Just delete those characters from the list shown in the setting.

You can use the extension Select By.
Edit you keybindings.json
You can define or redefine the Ctrl+D with
{
"key": "ctrl+shift+d", // or any other combo
"when": "editorTextFocus",
"command": "selectby.regex",
"args": {
"surround": "[-.a-zA-Z0-9]+"
}
}

Related

How to make ctrl word end or start recognize underscore

In vscode, ctrl + arrow will not stop at an underscore. Is there any way to change this behaviour, or is there a shortcut to select characters between two underscores?
(I have searched through available shortcuts and extensions but could not find any)
Thanks!
If you add the underscore to your wordSeparators in the setting Editor: Word Separators, then
Ctrl+rightArrow : move to the next word separator,
followed by another rightArrow to move after that underscore, then
Ctrl+Shift+rightArrow will select all word characters up to the next word separator, which might or might not be the next underscore - depends on your code.
You can use the extension Select By.
With a regular expression you can specify what you recognize as a word separator. Use the moveby.regex command. And then redefine the key binding for Ctrl+ArrowRight
To select some text based on regular expressions use command selectby.regex:
Add to your settings.json
"selectby.regexes": {
"selectUnderscores": {
"surround": "_[^_]*_"
}
}
And define a keybinding:
{
"key": "ctrl+f10", // or any other key combo
"when": "editorTextFocus",
"command": "selectby.regex",
"args": ["selectUnderscores"]
}

Setting character $ to wrap text as parentheses in vscode

Suppose following code exists.
sample text
When user double click text, then press { or (, it just wraps the text while keeping it.
sample {text}
sample (text)
But I don't know how to apply this rule to $ in VS Code Settings.
What I expect is
sample $text$
Which setting in VS Code is related to this feature?
Edit> Auto Surround
is the setting in vscode. But it only applies to quotes and brackets like (), {}, <> and [] (and possibly some other language-defined cases). You cannot change that setting to include another character like $ unfortunately.
Here is a keybinding you might try (in keybindings.json):
{
"key": "alt+4", // or whatever keybinding you wish
"command": "editor.action.insertSnippet",
"args": {
// "snippet": "\\$$TM_SELECTED_TEXT\\$"
// to have the text still selected after the $'s are inserted, use this
"snippet": "\\$${1:$TM_SELECTED_TEXT}\\$"
},
"when": "editorTextFocus && editorHasSelection"
},
So that any selected text will be wrapped by a $ when you select it and alt+4 (where the $ is on an English keyboard). If you do that operation a lot it might be worth it.
If you use this line instead in the snippet above:
"snippet": "$1$TM_SELECTED_TEXT$1" // or
"snippet": "$1${2:$TM_SELECTED_TEXT}$1"
then more generically select text to surround, trigger that keybinding and type whichever and how many characters you want to wrap the selection.

Keyboard shortcut in VSCode for Markdown links?

from other text editors I'm used to adding Markdown links by
selecting the word I want to be linked,
pressing cmd-K on my Mac's / iPad Pro's keyboard, which puts square brackets around the marked word, appends a pair of normal parenthesis () and places the cursor right in beetween those two parenthesis so that I can
just paste the URL I have in my clipboard into the right place by pressing cmd-V.
So, select -> cmd-K -> cmd-V is a nice and short sequence for adding links in a Markdown document and cmd-K has become some kind of pseudo standard for adding links in several writing apps.
However, in VSCode that's not possible. But I'd love to make it possible. Any ideas? cmd-K is (hard-wired?) bound to listen for a next key press.
But it doesn't have to be cmd-K. I can learn another keystroke. But I need to be able to put additional text (square brackets and parenthesis) into the text and move the cursor to the right position. How's that done?
Thanks so much!
This extension Markdown All In One looks like it does what you want in one step.
Paste link on selected text
Just select your link and hit Ctrl+V and it creates the link and inserts the clipboard link.
If for some reason you don't want to use this extension, it would be pretty easy to create a snippet to do what you want.
Adding another answer that doesn't use the extension Markdown All In One I mentioned in the other answer and because a couple of commenters requested a different way. #MarcoLackovic
First Method: keybinding in keybindings.json, then manually paste
{
"key": "alt+w", // use whatever keybinding you wish
"command": "editor.action.insertSnippet",
"args": {
"snippet": "[${TM_SELECTED_TEXT}]($0)"
},
"when": "editorHasSelection && editorLangId == markdown "
}
Select the link text and, trigger your keybinding - the cursor will be placed where you want it and paste.
Second Method: use a macro to insert the snippet and paste in one step
You will need a macro extension like multi-command to run multiple commands in series. Then this keybinding:
{
"key": "alt+w",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "[${TM_SELECTED_TEXT}]($0)"
}
},
"editor.action.clipboardPasteAction"
]
},
"when": "editorHasSelection && editorLangId == markdown "
}
Demo of second method:

Disable closing bracket swallowing? [duplicate]

This question already has answers here:
How to make vscode stop overriding closing parentheses on insertion
(5 answers)
Closed 3 years ago.
When you have the cursor in front of a ], ) or } and you type that character, instead of inserting it vscode just moves past that character, producing ]*cursor here* instead of ]*cursor here*]. Becacuse of this, every time I actually need to insert a closing bracket I need to move to the end of the )))) to type it, instead of just being able to type it directly. So is there a way to disable this behavior(without disabling bracket auto-completion)?
Here is the same question, but for sublime text, and this guy mentions it as a side effect of auto-closing brackets.
I received a solution from github of vscode project.
It works for me. Edit your keybindings.json add the text below:
{
"key": "]",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "]"
}
},
{
"key": "Shift+]",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "}"
}
},
{
"key": "Shift+0",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": ")"
}
}
Notice: "Shift+0" for en keyboard (, edit it for your keyboard layout.
It is indeed a side effect of the autoClosingBrackets setting of the editor.
If you go to File > Preferences > Settings to open the settings JSON file, you can search for "editor" or "autoClosing" and copy the entry to your user settings if you whish to change/disable it (it's enabled by default), or just copy this to disable it:
// Controls if the editor should automatically close brackets after opening them
"editor.autoClosingBrackets": false,
More on VS Code settings, as well as a list of the default settings can be found here: https://code.visualstudio.com/docs/getstarted/settings
If you disable this setting:
Typing a bracket or quote won't automatically add a matching, closing bracket or quote.
Typing a (closing) bracket before an existing one won't cause it to be "absorbed".
You'll have to type each closing bracket or quote yourself.
You won't be able to automatically enclose selected text with brackets or quotes by selecting it and typing just one bracket/quote. With this option disabled, the selected text will be replace with whatever you type.
TL;DR: currently it is not possible to disable this annoying feature.
I'v asked the same question here.
There is an open issue in their repo.

Visual Studio Code Surround With

I can't find any way to surround a selection with something in VS Code.
For example doing something like that : text => "text" just by selecting the word text and typing key "
Another example with the following text :
mon
tue
wed
thu
fri
sat
sun
By selecting all of theses words :
mon|
tue|
wed|
thu|
fri|
sat|
sun|
and typing " I would like to perform something like this :
"mon"
"tue"
"wed"
"thu"
"fri"
"sat"
"sun"
Selecting some text and pressing " already works in VSCode to surround a single item, and works for multi-line selections as well.
NOTE: this is language dependent. The language syntax must define opening and closing braces, e.g. quotes, braces, etc. So this will not work in a "plaintext" file, for example. Change your language mode with CTRL+SHIFT+P and type Change Language Mode ENTER and select something like JavaScript where this is supported.
What you are after though is not really that efficient like that. Your best bet is to use multi-cursors.
Place the cursor at the start of the first line, press CTRL+ALT+DOWN to add another cursor below on the next line. Keep doing that until you have a cursor in front of all your words.
Then just type " then END then " and all your lines are surrounded by quotes.
NB: To check if you have a key bound, and what it is, you can always press CTRL+SHIFT+P and type Add Cursor Below and if there's a keybinding it will show to the right of that text.
In VS Code hold Command + Shift + P
then write:
"> Preferences: Open Keyboard Shortcuts (JSON)"
In this area that you are allowed to modify, paste this inside the brackets:
{
"key": "ctrl+p",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\""
}
}
** note that in this example the key is set to Ctrl + p, you can change the key to whatever you prefer
Maybe you can try this extension, you can write your own custom wrappers:
https://marketplace.visualstudio.com/items?itemName=yatki.vscode-surround
A simple yet powerful extension to add wrapper templates around your code blocks.
Features
Supports multi selections
Fully customizable
Custom wrapper functions
You can assign shortcuts for each wrapper function separately
Nicely formated
Demo 1: Choosing wrapper function from quick pick menu
Demo 2: Wrapping multi selections
Using Yuri Aps' suggestion, I added the following JSON to keybindings.json. This provides the functionality Ronan Lamour requested for any file type, and without requiring an extension. It works for either single or multiple selections when using either single or double quotes. Coming from Sublime, this is helpful since it reproduces functionality Sublime provides natively.
{
"key": "'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "'${TM_SELECTED_TEXT}'"
}
},
{
"key": "shift+'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\""
}
},
I was coming from (neo)vim switching to VS Code, and was using Tim Pope's wonderful "vim-surround" plugin for vim before. I found a port of that plugin for VS Code. It's very useful, and incredibly efficient once you learn the shortcuts, in my opinion!
Links:
Original plugin by Tim Pope for vim
Port of plugin to VS Code
If you use vim or vim bindings in VS Code, please enjoy!
Edit: the VSCodeVim plugin includes the surround functionality automatically, so if you have that plugin installed, you don't really need the vscode-surround plugin.
Update 15-02-2022:
VS Code has introduced Surround with snippets for JS/TS natively.
It may not be totally related with the question but it may help someone who landed in this question with the intent of "surround with" in vs code.
This extension also exists if you want custom surround with text.
https://marketplace.visualstudio.com/items?itemName=sifue.surrounding.
I just installed it and got it working perfectly
Select the word you want to surround it with and enter Ctrl + Alt + T. Then just key in whatever key you want to surround it with.
A more generic solution: in keybindings.json:
{
"key": "alt+m",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "$1${TM_SELECTED_TEXT}$1$0"
}
}
Whatever you type after triggering the keybinding will be added to both ends of all selections.
Just tab to the end of the word(s) when you are done and, if you had multiple cursors Esc to remove extra cursors leaving just one.
Since GitHub supports math in Markdown now, I need to wrap my formulas with dollar signs:
$E = mc^2$
When I select a formula and press dollar sign $ on my keyboard I get my formula wrapped automatically. Here is one way to achieve it:
Open Keyboard Shortcuts menu:
Press on Open Keyboards Shortcuts (JSON) button:
In shortucts.json file, which opens, paste this snippet:
{
"key": "shift+4",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "$${TM_SELECTED_TEXT}$"
}
}