Replace "< -" keystroke combination for a custom shortcut in VS Code - visual-studio-code

I was wondering how could I set a shortcut, for example CRTL+F for the keystrokes <- for a better experience programming with R language. Or even when I type - it prints out on my coding scripts the full <-.
I could set it in RStudio but couldn't find a way to do that in VS code. The shortcut options in VS Code are always linked to IDE commands, didn't help me at all.
Any ideas?

Or use this handy keyboard shortcut example with arguments from the doc pages
{
"key": "ctrl+f",
"command": "type",
"args": { "text": "<-" },
"when": "editorTextFocus && editorLangId == r"
},

You can also define a user snippet. For example, put this in your snippets\r.json file:
"assign": {
"prefix": "=",
"body": " <- ",
"description": "left arrow assignment"
}
Now, typing =Tab will insert <-.

Got it just by adding the below code to keybinding.json file:
// Place your key bindings in this file to override the defaults
[
{
"key": "oem_minus oem_minus",
"command": "type",
"args": {
"text": "<-"
},
"when": "editorTextFocus"
}
]
Worked perfectly. When I type -- it prints out <-.

Related

VSCode Terminal command names for Up/Down Arrow hotkeys? [duplicate]

My current custom keybindings
{
"key": "alt+b",
"command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus",
"args": {
"text": "\u0017"
}
},
{
"key": "alt+j",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u001bb" }
},
{
"key": "alt+l",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u001bf" }
},
{
"key": "alt+n",
"command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus",
"args": {
"text": "\u001bd"
}
These work well with 1.45 update (integrated terminal update)
Reference - but I need more Information
I want to use 'ijkl' keys like 'wasd' gaming standard like my custom project intuiter
So My Idea is
Can we send sequence to terminal to show Previous Command/Next command (like when we click Up/Down Arrow)
moveToLineStart/moveToLineEnd sequence is removed with this update(sorry I can't find or make sequence) can we get this effect by sendSequence?
"Can we sendSequence to terminal to show Previous Command/Next Command (like when we click Up/Down Arrow)"
{
"key": "alt+x", // or whatever you choose
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "\u001b[A\n"
}
// 1 up arrow or previous command, works in cmd, git bash and powershell
// the \n at the end makes it execute the previous command immediately,
// leave it off if you don't want to do that
// \u000d, a carriage return, is equivalent to \n if you prefer to use that, so
// \u001b[A\u000d does the same thing as \u001b[A\n
> /** Carriage Return (Caret = ^M, C = \r) */
> export const CR = '\x0d';
{
"key": "alt+y", // or whatever you choose
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "\u001b[B\n"
}
// 1 down arrow
// see comment above about immediate command execution
For these arrow commands, see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
Functions using CSI , ordered by the final character(s)
CSI Ps A Cursor Up Ps Times (default = 1) (CUU).
CSI Ps B Cursor Down Ps Times (default = 1) (CUD).
This part \u001b[ is the escape sequence or CSI referred to in the document. Follow that with an A for cursur up (up arrow) so \u001b[A or follow the escape sequence with a B for a down arrow, so \u001b[B.
[Theoretically, you should be able to do \u001b[2A for 2 (the Ps part referred to above) up arrows at once but that never seems to work in vscode for me in vscode.]
{
"key": "ctrl+e",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u0005" }, // move cursor to end of line, bash at least
"when": "terminalFocus"
},
{
"key": "ctrl+a",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u0001" }, // move cursor to start of line, bash at least
"when": "terminalFocus"
},
I used the usual keybindings - at least in bash - for go to the start or end of the command line. See, e.g., http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/
From this document https://github.com/xtermjs/xterm.js/blob/0e45909c7e79c83452493d2cd46d99c0a0bb585f/src/common/data/EscapeSequences.ts
we see that Ctrl+A is :
/** Start of Heading (Caret = ^A) */ [that's Ctrl+A]
export const SOH = '\x01';
so I used \u0001 the unicode sequence to replace that listed \x01 which won't work with the sendSequence command.
Likewise, to send a Ctrl+E to the terminal for the go to the end of the command line, we see that Ctrl+E is :
/** Enquiry (Caret = ^E) */ [that's Ctrl+E]
export const ENQ = '\x05';
or unicode \u0005.
Now, your terminal shell might use something other than Ctrl+A and Ctrl+E for go to start/end of the command line. If my keybindings don't work for you, find out what your shell uses for go to start/end and see if they are in https://github.com/xtermjs/xterm.js/blob/0e45909c7e79c83452493d2cd46d99c0a0bb585f/src/common/data/EscapeSequences.ts

How to create shortcut for one line code in vs?

I want to create a shortcut key for one line code in visual code . For example I want to use the key "F4" to paste "<p class='example _class'>" this code in the code editor or the key "F6" to paste "<a href= ''>" . Basically I want to create shortcut for some line of code, so that I don't need to copy these line again and again to repeat this line in my project.
After some research I got my answer,
On the keybindings.json file paste the below code:
{
"key": "F4",
"command": "type",
"args": {
"text": "<p class=\"example _class\">"
},
"when": "editorTextFocus"
},
I think the json code does not need any explanation. It is pretty much self explaining.
If you insert a snippet with the keybinding you don't have to select and replace the class name but make it a placeholder, use TAB to move from tab stop to tab stop
{
"key": "F4",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "<p class=\"${1:example _class}\">$0</p>"
}
}

Bind a command to go to a specific symbol in VS Code

My issue:
I would like to bind a few keyboard shortcuts to navigate to specific symbols in my currently open file within VS Code.
Example:
I want to navigate to the symbol <template> by pressing cmd+1.
I want to navigate to the symbol <script> by pressing cmd+2.
I want to navigate to the symbol <style> by pressing cmd+3.
By default, you can do that by pressing cmd+p and then in the palette typing #template which will jump to the template tag within the open file.
So a solution might look something like this in my keybinding.json:
{
"key": "cmd+1",
"command": "workbench.action.gotoSymbol",
"args": ["#template"],
}
However, the args part of this does not work. I'm wondering if anyone knows a good solution for setting up key bindings like this so I can navigate around my files with shortcuts.
Thanks in advance!
Try this:
{
"key": "cmd+1",
"command": "workbench.action.quickOpen",
"args": "#<template>",
},
The command workbench.action.gotoSymbol won't take an argument, but workbench.action.quickOpen will. I don't know why there is that difference, but as you know if you start the Quick Open Panel with # you get object references. And the rest of your text <template> for example will automatically be entered into the input box.
It will filter for those symbols but will not jump to the first one. If you want that additional functionality, you would have to consider a macro which would select the first entry.
There is an alternative that allows you to navigate to anything by using a matching regex. See the extension Select By which allows to jump to any string. For example (in settings.json):
"selectby.regexes": {
"goTo<template>": {
"moveby": "<template>",
}
}
and your keybinding (keybindings.json):
{
"key": "ctrl+1", // <template>`
"command": "moveby.regex",
"args": ["go<template>", "moveby", "next", "start", "wrap"],
// "when": "editorTextFocus && editorLangId == javascriptreact"
}
You can set it up to jump to the previous instance as well:
{
"key": "ctrl+alt+1", // previous <template>
"command": "moveby.regex",
"args": ["goTo<template>", "moveby", "prev", "start", "wrap"],
// "when": "editorTextFocus && editorLangId == javascriptreact"
}
either way it should now "wrap", it just depends on how many <templates> you have in the file and which direction you want to go to first.
I found the solution to my problem, which is pretty funny because it's an existing extension for VS Code that does exactly what I am looking for:
https://marketplace.visualstudio.com/items?itemName=andersonmfjr.vue-jumptotag
To bind the keyboard shortcuts in keybindings.json
{
"key": "cmd+1",
"command": "extension.jumptotemplate",
},
{
"key": "cmd+2",
"command": "extension.jumptoscript",
},
{
"key": "cmd+3",
"command": "extension.jumptostyle",
},
That does exactly what I was looking for.

vscode custom key binding length issue

I am binding custom keys with keybindings.json file like below,
{
"key": "s a",
"command": "workbench.action.files.saveAll",
"when": "vim.mode == 'Normal'"
}, {
"key": "q u",
"command": "workbench.action.closeActiveEditor",
"when": "vim.mode == 'Normal'"
}
It seems that vscode supports only custom keys length to 2.
Is there any way to binding keys (length) more than that?
for example
{
"key": "s a v e",
"command": "workbench.action.files.saveAll",
"when": "vim.mode == 'Normal'"
}, {
"key": "q u i t",
"command": "workbench.action.closeActiveEditor",
"when": "vim.mode == 'Normal'"
}
thank you.
I ran into the same problem but could not find the fix to this issue despite my researches, curious about this one...
Meanwhile, I use a workaround with betterTouchTool for Mac Os and it does the job perfectly (you can assign key sequences per app or globally). For instance I set the key sequence pry to be replaced by binding.pry on the fly (with keybindings.json it was triggered too early, as soon as I hit the 'r'), but you can as well trigger any other action.
I guess it's not the only app allowing this kind of customisation but I use this one a lot and didn't search any further.
Hope this helps!

Using TM_SELECTED_TEXT in my custom snippets

With the November 2016 (version 1.8) release of VSCode Snippet Variables are now supported, specifically TM_SELECTED_TEXT.
This makes me happy as I have used these heavily in both Sublime Text and TextMate.
I can't figure out how to get it to work in VSCode. I've created the snippet they use as an example:
"in quotes": {
"prefix": "inq",
"body": "'${TM_SELECTED_TEXT:${1:type_here}}'"
}
I then enter some text, highlight it and that's where things start to break.
The idea is highlight some text, run the snippet and then ${TM_SELECTED_TEXT:${1:type_here}} is replaced with the highlighted text. The problem I'm having is that to run the snippet you need to type the prefix value (in this case inq) to run the snippet which over-writes your highlighted text which messes everything up.
In Sublime/Textmate I launched the snippet from a keyboard combination which left my text highlighted.
Is there a way, in VSCode, to either make this work as is or launch the snippet from a key combination like was available in Sublime?
Coming in 1.49 (it is in the Insiders' Build as of this edit) your example will finally work as you expected. See merged pull request.
Vscode will now "remember" your selected text if any, and when you type your snippet prefix, insert it into the TM_SELECTED_TEXT variable even though you seemingly over-typed that selected text.
As of v1.20 this has become easier as a new variable $CLIPBOARD has been added, see new snippet variables. So there is no need to assign and run a shortcut - but you must copy the selection to clipboard CTRL-C.
Your example could now be:
"in quotes": {
"prefix": "inq",
"body": "'$CLIPBOARD:${1:type_here}'"
}
Note: $CLIPBOARD works. There's no need for the extra curly brackets {$CLIPBOARD}.
With the word highlighted, press F1 and run the command "Insert Snippet", then select your snippet on the list.
Also you can edit your keybindings by going to File>Preferences>Keyboard Shortcuts and add some shortcut to the "editor.action.showSnippets" command, like this:
{
"key": "ctrl+alt+s",
"command": "editor.action.showSnippets",
"when": "editorTextFocus"
}
https://github.com/Microsoft/vscode/issues/17780
as per this thread you can assign exact snippet to keybinding by providing args.
keybinding example for bootstrap media queries
{
"key": "ctrl+alt+b",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"name": "bsup"
}
},
{
"key": "ctrl+alt+shift+b",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"name": "bsup_copy"
}
},
snippet example
"bsup": {
"prefix": "bsup",
"body": [
"#include media-breakpoint-up(md){",
"\t${TM_SELECTED_TEXT}",
"}"
],
"description": "Bootstrap media up"
},
"bsup_copy": {
"prefix": "bsup_copy",
"body": [
"${1:${TM_SELECTED_TEXT}}",
"#include media-breakpoint-up(md){",
"\t${2:${TM_SELECTED_TEXT}}",
"}"
],
"description": "Bootstrap media up + copy selected text"
},
UPD: moreover - you can define snippet directly in keybindings.json which seems to be even more convenient for me in some cases
{
"key": "cmd+shift+c",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.log('${TM_SELECTED_TEXT}', $TM_SELECTED_TEXT$1);"
}
}
using documentation from https://code.visualstudio.com/docs/editor/userdefinedsnippets i was able to customize snippets, i am using 'surround with' extension and can put my own snippet into settings.json as follows:
"html_h3-name": {
"label": "h3",
"description": "wrap by h3 with <a name=''>, top",
"snippet": "<h3><a name=\"${TM_SELECTED_TEXT/[\\s]/-/g}\"></a>$TM_SELECTED_TEXT\n\t<a class=\"small\" href=\"#top\">top</a>\n</h3>"
},
which takes highlighted code in VSCode and creates h3 header from it with a name link:
it converts 'aaa bbb ccc' to
<h3><a name="aaa-bbb-ccc"></a>aaa bbb ccc
<a class="small" href="#top">top</a>
</h3>