Visual Studio Code snippet as keyboard shortcut key - json

I know how to modify and create code snippets and I know how to modify shortcut keys, but how does one bring those 2 together?

Note that the line below will open a list of snippets defined for the language you are currently using (and you don't want that)
"args": { "snippet": "'$TM_SELECTED_TEXT'" }
Whereas with the below line the snippet given as argument will be executed right away
"args": { "name": "your_snippets_name" }
Here's how I defined a snippet for HTML where I wanted to select a text and when pressing CTRL+B the text to become enclosed in <strong></strong> tags:
"make_strong": {
"prefix": "strong",
"body": [
"<strong>$TM_SELECTED_TEXT${1:}</strong>"
],
"description": "Encloses selected text in <strong></strong> tags"
}
Note the ${1:} above - what this does is that it places the cursor there. This enables you to press CTRL+B at cursor and then have the cursor placed inside the <strong></strong> tags. When selecting a string and pressing CTRL+B, the string will enclosed in <strong> tags and the cursor will be placed before the closing </strong> tag. Pressing TAB at this point, will put your cursor after the closing </strong> tag.
And added in my keybindings.json the following:
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": { "name": "make_strong" }
}
UPDATE JUNE 2nd, 2021
Since this is getting lots of views, I am posting some of the snippets I use, maybe it will be useful to someone
{
"key": "ctrl+alt+u",
"command": "editor.action.transformToUppercase"
},
{
"key": "ctrl+alt+l",
"command": "editor.action.transformToLowercase"
},
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_strong" }
},
{
"key": "ctrl+i",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_italic" }
},
{
"key": "ctrl+u",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_underline" }
},
{
"key": "ctrl+alt+p",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_paragraph" }
},
{
"key": "ctrl+shift+space",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_nbsp" }
},
{
"key": "ctrl+enter",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_br" }
},

It would seem that, as of version 1.9, Visual Studio Code can do what you are looking for, no other extensions necessary.
From https://code.visualstudio.com/updates/v1_9#_insert-snippets
"You can now bind your favorite snippets to key bindings. A sample that encloses a selection with single quotes looks like this:"
Add the snippet below to keybindings.json (open Keyboard Shortcuts editor and click on the For advanced customizations open and edit keybindings.json link)
{
"key": "cmd+k",
"command": "editor.action.insertSnippet",
"args": { "snippet": "'$TM_SELECTED_TEXT'" }
}

Here are 3 steps to create a code snippet along with a shortcut.
1. Code -> Preferences -> Keyboard Shortcuts
2. Click on icon for keybindings.json file
3. Add JavaScript objects for Code Snippet/Shortcuts
For example i have created snippets for logging purposes as I mostly work with JavaScript Frameworks.
1.console.log('') with shortcut Control (or Ctrl) ⌃ + l
2.console.warn('') with Control (or Ctrl) ⌃ + w
3.console.error('') with Control (or Ctrl) ⌃ + e
Code:
{
"key": "ctrl+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.log('${TM_SELECTED_TEXT}$1')$2"
}
},
{
"key": "ctrl+w",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.warn('${TM_SELECTED_TEXT}$1')$2"
}
},
{
"key": "ctrl+e",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.error('${TM_SELECTED_TEXT}$1')$2"
}
}

Call Command Palette in View menu
Hit "shortcuts json" and OK
Then append under code blocks
{
"key": "shift+alt+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == 'js'",
"args": {
"snippet": "console.log($1);$0",
}
},
{
"key": "shift+alt+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == 'dart'",
"args": {
"snippet": "print($1);$0",
}
},
If you press Shift+Alt+L in JavaScript then
put "console.log();" to your editor,
And press Shift+Alt+L in Dart then
put "print();" to your editor,
with the same shortcut.

Related

Bracket IDE's Quick Markup equivalent for VS Code

Quick Markup (https://github.com/redmunds/brackets-quick-markup) is an extension available for Brackets IDE for HTML development. The extension can be activated with a shortcut Ctrl+Alt+M. When it is active, we can use custom shortcuts to insert custom HTML tags instead of typing them. For example, to make insert <b></b> tags around a word, we just need to select the word and hit Ctrl+B. The shortcuts and their corresponding tags can be customized.
I'm looking for a similar extension or feature like this in Visual Studio Code. I tried searching but couldn't find anything so far.
keybindings.json
{
"key": "ctrl+b",
"command": "editor.emmet.action.wrapWithAbbreviation",
"when": "editorHasSelection",
"args": {
"abbreviation": "b"
}
},
{
"key": "ctrl+shift+b",
"command": "editor.emmet.action.wrapWithAbbreviation",
"when": "editorHasSelection",
"args": {
"abbreviation": "pre>code"
}
},
You can also call it without args and just type the tags every time.
Or just typing anything:
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "<br />"
}
}

How do I create a open folder keyboard shortcut in Sublime Text?

I know to add keyboard shortcuts, I go into Keyboard Bindings - User and edit the JSON file. I already have a lot of keyboard customizations.
[
{ "keys": ["ctrl+shift+."], "command": "erb" },
{ "keys": ["alt+i"], "command": "expand_tabs" },
{ "keys": ["alt+ctrl+w"], "command": "close_all" },
{ "keys": ["ctrl+t"], "command": "show_overlay", "args": {"overlay": "goto", "show_files": true} },
// swap the keybindings for paste and paste_and_indent
{ "keys": ["ctrl+v"], "command": "paste_and_indent" },
{ "keys": ["ctrl+shift+v"], "command": "paste" },
// swap the keybindings for save and save_all
{ "keys": ["ctrl+s"], "command": "save_all" },
{ "keys": ["ctrl+shift+v"], "command": "save" }
]
I just need to know the command for open folder. I tried the obvious open_folder it didn't work.
I found the answer in the Sublime Text 2 forums. The command is prompt_open_folder. So I just added this to my keyboard bindings.
[
// Open folder shortcut
{ "keys": ["ctrl+shift+o"], "command": "prompt_open_folder"}
]

Opposite of "wrap selection with tag" in Sublime Text 2

Is there a smarter way of removing the tag (let's say using HTML) around a selection in Sublime text 2?
For example if I have a span tag around some text and want to remove both the start and end of this tag (and leave the text inside untouched)...
Or alternative - remove current tag and it's corresponding end tag?
The Emmet plugin (once called Zen Coding) can do what you want. Install it and press Ctrl+Shift+; within a tag's contents to unwrap the tag.
A bit easier way to do this is to use a macro:
unwrap.sublime-macro
[
{
"args":
{
"to": "tag"
},
"command": "expand_selection"
},
{
"args": null,
"command": "copy"
},
{
"args":
{
"to": "tag"
},
"command": "expand_selection"
},
{
"args": null,
"command": "paste_and_indent"
}
]
...with a ctrl+u for example:
{
"keys": ["ctrl+u"],
"command": "run_macro_file",
"args": {"file": "res://Packages/User/unwrap.sublime-macro"}
}
Pros: no plugins required, only default ST functionality. Cons: modifies clipboard data.

Keyboard shortcut to comment lines in Sublime Text 2

In Sublime Text 2, how do I enclose a selection in a comment?
Is there a keyboard shortcut for this action?
By default on Linux/Windows for an English keyboard the shortcut is Ctrl+Shift+/ to toggle a block comment, and Ctrl+/ to toggle a line comment.
If you go into Preferences->Key Bindings - Default, you can find all the shortcuts, below are the lines for commenting.
{ "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+/"], "command": "toggle_comment", "args": { "block": true } },
In the "Preferences->Key Bindings - User"
[
{ "keys": ["ctrl+7"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+7"], "command": "toggle_comment", "args": { "block": true } }
]
Just paste it, these are will work great !
I'd like to add, that on my mac by default block comment toggle shortcut is cmd+alt+/
For German keyboards use ctrl+shift+# to toggle a block comment and ctrl+# to toggle a line comment.
The shortcut in Preferences->Key Bindings - Default is set to Ctrl+Shift+/ and Ctrl+/, but to actually use the functions, press the keys stated above.
In a Brazilian Portuguese ABNT2 keyboard I have a similar issue to the one reported by JoshDM. In the file sublime-keymap I have:
{ "keys": ["ctrl+/"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+/"], "command": "toggle_comment", "args": { "block": true } },
But I have to use ctrl+; and ctrl+shift+;. On my keyboard, ; is on the left of /.
It seems like a bug.
you need to replace "/" with "7", it works on non english keyboard layout.
This did the trick for me coming from Brackets and being used to ctrl+/ on the numpad.
[
{ "keys": ["ctrl+keypad_divide"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+keypad_divide"], "command": "toggle_comment", "args": { "block": true } }
]
In my keyboard (Swedish) it´s the key to the right of "ä": "*".
ctrl+*
In keyboard (Spanish), SO: Win7.
Go into Preferences->Key Bindings - Default,
replace..."ctrl+/"]... by "ctrl+7"...
And don't use the numpad, it doesn't work.
Just use the numbers above the letters
On a Mac with a US keyboard, you want cmd+/.
Seems like some kind of keyboard mapping bug. I'm Portuguese, so I'm using a PT/PT keyboard. Sublime Text 3 apparently is handling / as ~.
Max OS: If you want to toggle comment multiple individual lines versus block comment an entire selection, you can do multi line edit, shift+cmd+L, then cmd+/ in that sequence.
First Open The Sublime Text 2.
And top menu bar on select the Preferences.
And than select the Key Bindings -User.
And than put this code,
[
{ "keys": ["ctrl+shift+c"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+c"], "command": "toggle_comment", "args": { "block": true } }
]
I use Ctrl+Shift+C, You also different short cut key use.
Ctrl+d and Ctrl+Shift+d....
[
{ "keys": ["ctrl+d"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+d"], "command": "toggle_comment", "args": { "block": true } },
]
On my laptop with spanish keyboard, the problem seems to be the "/" on the key binding, I changed it to ctrl+shift+c and now it works.
{ "keys": ["ctrl+shift+c"], "command": "toggle_comment", "args": { "block": true } },

In Sublime Text 2 - reopen build output

In Sublimt Text 2, when I use the build system (make) to run tests, the output is displayed in the build output pane.
However, if I press escape to close the output pane (e.g. to make a fix), I can't find a way to redisplay the output pane to see what else was borked. Have tried to create a custom keybinding to execute show_panel "output", but can't get it working.
Meep?
The menu shortcut is under Tools -> Build Results -> Show Build Results.
I wish this was under the View menu like all the rest of view options...
As you can see in Packages/Default/Main.sublime-menu the command for "Show build results" is this:
{
"command": "show_panel",
"args": {
"panel": "output.exec"
},
"caption": "Show Build Results",
"mnemonic": "S"
},
so a custom key binding could be this:
{
"keys": ["ctrl+alt+super+r"],
"command": "show_panel",
"args": {
"panel": "output.exec"
}
}
And the key binding to hide the panel:
{
"keys": ["ctrl+shift+2"],
"command": "hide_panel",
"args": {
"panel": "output.exec"
}
},
Building on akirk's answer you can make it toggle the build results panel by copying some of the syntax used for the escape shortcuts.
Adding the following lines to the user key bindings will do part of the trick. As reported by some of the previous answers the hide_panel command will hide any panel, and pressing it a second time will reveal build_results.
{
"keys": ["alt+b"], "command": "show_panel", "args": {"panel": "output.exec"},"context":
[
{ "key": "panel_visible", "operator": "equal", "operand": false }
]
},
{
"keys": ["alt+b"], "command": "hide_panel", "args": {"panel": "output.exec"},"context":
[
{ "key": "panel_visible", "operator": "equal", "operand": true }
]
},