VS Code : create custom snippet/shortcut - visual-studio-code

I would like to have the following feature on VSCode and I don't know if it is possible. For example, if I have this line :
I would like to emphasize this.
I would just select this, clic on a shortcut like ctrl+i and then this appears :
I would like to emphasize {i}this{/i}.
I use a lot of {i} and {/i} tags in my project so this would help me save an incredible amount of time !
I know VSCode already does something similar when you select a word and clic on "

Find your keybindings.json file and insert the following snippet:
{
"key": "ctrl+i",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "{i}$TM_SELECTED_TEXT{/i}"
},
"when": "editorTextFocus && editorHasSelection"
}
Key bindings can be found by pressing Ctrl+Shift+P > type "Keyboard shortcuts", full name being: Open Keyboard Shortcuts (JSON).

Related

VSCode how to know when editor is not split?

Until now, I used cmd+2 to split the editor on Visual Studio Code and then cmd+1 and cmd+2 to move between the split editors. This shortcut stopped working for some reason, and I can't set it on the keyboard shortcut menu either.
I checked with another keyboard, so this is not an hardware issue.
Anyhow, I tried to set new keybindings to split the screen and move between the editors, like this:
[
{
"key": "cmd+shift+]",
"command": "workbench.action.navigateRight",
"when": ""
},
{
"key": "cmd+shift+]",
"command": "workbench.action.splitEditorRight",
"when": ""
},
{
"key": "cmd+shift+[",
"command": "workbench.action.navigateLeft",
},
]
The only problem, I couldn't find what I need to write in the when option.
I want my shortcut to split the editor only if the editor is not split yet, and to move between the editors when it is.
How can I achieve that?
The multipleEditorGroups when clause context returns true when there's more than one editor group open.
You don't need to put a "when" for neither of them. Just checked vscode defaults and they don't have any.

VS Code: Single keybinding to Spit/Merge Editor?

Is there a way to detect the split/single states of the editor so I can use a single keybinding to split and merge an editor?
{
"key": "ctrl+\\",
"command": "workbench.action.splitEditorDown"
"when": "EDITOR IS IN SINGLE STATE"
},
{
"key": "ctrl+\\",
"command": "workbench.action.editorLayoutSingle",
"when": "EDITOR IS IN SPLIT STATE"
}
Or perhaps there is a different way to set this up?
Thanks.
I think this what you are looking for:
{
"key": "ctrl+\\",
"command": "workbench.action.splitEditorDown",
"when": "!multipleEditorGroups"
},
{
"key": "ctrl+\\",
"command": "workbench.action.editorLayoutSingle",
"when": "multipleEditorGroups"
}
I found that when clause by using the Developer: Inspect Context Keys command.
(1) open the Developer Tools Console: Help/Toggle Developer Tools
(2) run the above command from the Command Palette and click anywhere in an editor
(3) that will create a large object in the Developer Tools Console that has a lot of when context clauses with their current values
(4) do a find in the Console for groups and look for a promising when clause (I first searched with the term split but nothing was helpful).
There is nothing to tell you if a specific file is split into two (or more) groups though - only that there are 1 or more groups of editors.

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:

How to Create Custom Key Binded Snippets in VS Code

I am a huge Sublime Text user, and learned ways to improve my productivity using customizations in Sublime text. But as VScode is becoming popular day by day, wanted to check if there is any way which I can bind the shortcut keys to the custom actions.
For example, I select a word ABC in any file in VSCode and hit CTRL+B, and it places my own defined values around it like it should become
<b>ABC</b>
I had created the following snippet in Sublime Text, which when I wrote in Visual Studio Code - keybindings.json nothing worked.
{
"keys": [
"ctrl+b"
],
"command": "insert_snippet",
"args": {
"contents": "<b>${0:$SELECTION}</b>"
}
}
This will work in your keybindings.json:
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"when": "resourceExtname == .html", // this is optional
"args": {
"snippet": "<b>${TM_SELECTED_TEXT}</b>"
}
},
The optional when clause is if you want to limit the snippet's operation to .html files.
More general though is to use the emmet command which is built-in: Emmet: Wrap with Abbreviation in the command palette. Select your text, open the command palette, find that command and trigger it - type b or whatever your element is and it will wrap the selected text with the opening and closing elements.
[Note that there is a command workbench.action.toggleSidebarVisibility already bound to Ctrl-B, but the snippet above version seems to take precedence - meaning you lose the toggleSidebarVisibility keybinding functionality - that may be acceptable to you?]

Define Keybinding to insert special character in VSCode

I would like to define a keybinding to insert a specific unicode character in VSCode. What is the right way to achieve this ?
VSCode has a type command to handle exactly this kind of use case:
In keybindings.json:
{
"key": "ctrl+alt+1 s",
"command": "type",
"args": {
"text": "Ψ"
}
}
Type and select "Preferences: Open Keyboard Shortcuts (JSON)" in the Ctrl+Shift+P menu in VSCode.
The file is called keybindings.json but it doesn't seem to be indexed quite as such in search for some reason.