Define Keybinding to insert special character in VSCode - visual-studio-code

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.

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.

Select the current word in VSCode when dots or hyphens are present?

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]+"
}
}

VS Code : create custom snippet/shortcut

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).

How to create VSCode bindings to input bra and ket efficiently

I'm currently using VSCode for Q# programming. This sometimes entails including simple qubit expressions in the comments for clarity. It is of course possible to just settle with using regular angle brackets (such as |00> or <00|), but it looks nicer using the appropriate Unicode characters (such as |00⟩ or ⟨00|). Copying and pasting these characters whenever needed is a bit cumbersome, so it would be nice to have key bindings in VSCode just for this purpose. Actually, I'd like to be able to configure VSCode for quick access to any selection of characters I might be interested at the moment.
VSCode customization supports a type command which does exactly that - types in its argument. In order to create an entry for a keybinding, open the command prompt (Ctrl+Shift+P or ⌘+Shift+P on Mac) and type Preferences: Open Keyboard Shortcuts (JSON) and insert entries of the form:
{
"key": "<key-binding>",
"command": "type",
"args": {
"text": "<character>"
}
}
where <key-binding> is the usual description of the keybinding and <character> is the desired character literal. So, for the bra-ket case above, my customization looks like this:
[
{
"key": "ctrl+shift+.",
"command": "type",
"args": {
"text": "⟩"
}
},
{
"key": "ctrl+shift+,",
"command": "type",
"args": {
"text": "⟨"
}
}
]

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?]