I use VS Code with the Lean language, which supports Unicode characters. The Unicode characters are entered through sequences of ASCII characters. For example, to enter a universal quantifier you enter the five-character sequence \ a l l TAB. Is there any way in VS Code to bind that sequence to a function key so that I can say hit F1 and this is interpreted as entering the ASCII sequence?
I needed to bind a key in VSCode to a sequence of characters. Here's how to do that in VS Code 1.67.2:
Press ctrl+alt+P / cmd+alt+P to open the command palette.
Use the command "Preferences: Open Keyboard Shortcuts (JSON)"
Press the "Define Keybinding" button in the bottom right, or press cmd+K cmd+K / ctrl+K ctrl+K, or just type it in.
The following works for your case:
{
"key": "F1",
"command": "type",
"args": { "text": "\\all\t" },
"when": "editorTextFocus && !editorReadOnly"
},
and for my case
{
"key": "cmd+.",
"command": "type",
"args": { "text": "->" },
"when": "editorTextFocus && !editorReadOnly"
},
The binding will work as soon as you save the file.
Related
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
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 <-.
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 do I search for the selected text in the current file without having to copy / ctrl-f / paste?
For clarification: Ultraedit has this behaviour. When pressed F3 and there's no selected text it performs the last search, if there is a selected text then it searches for the selected text in the current file.
Enable the editor.find.seedSearchStringFromSelection setting as seen below. This will cause highlighted texted to automatically be searched when you press ctrl+f.
Ultraedit-way search is my favorite and it's really convenient: single 'F3' key can handle all.
The drawback of Ctrl+D is: it cannot wrap around the search.
To be clear, the definition of Ultraedit-way search is:
When pressed F3 and there's no selected text it performs the last search, if there is a selected text then it searches for the selected text in the current file.
Here is the absolutely 100%-compatible solution to Ultraedit-way search:
When text is selected, do nextSelectionMatchFindAction
When no text is selected, do nextMatchFindAction
Single F3 does both above.
Shift+F3 keeps its original: Find Previous
Therefore keybindings.json can add the following lines to disable original F3 and Ctrl+F3 functions, and add two new F3 functions when text is selected and not selected.
{
"key": "f3",
"command": "editor.action.nextSelectionMatchFindAction",
"when": "editorFocus && editorHasSelection"
},
{
"key": "ctrl+f3",
"command": "-editor.action.nextSelectionMatchFindAction",
"when": "editorFocus"
},
{
"key": "f3",
"command": "editor.action.nextMatchFindAction",
"when": "editorFocus && !editorHasSelection"
},
{
"key": "f3",
"command": "-editor.action.nextMatchFindAction",
"when": "editorFocus"
}
And one more thing to be addressed:
When F3 is pressed, the search dialog is appeared and every matched text is highlighted, you can press ESC to dismiss search dialog when search is done.
Update #2021/1/25
If anyone wants Shift+F3 to works as smart as F3, add the following line into keybindings.json:
{
"key": "shift+f3",
"command": "editor.action.previousSelectionMatchFindAction",
"when": "editorFocus && editorHasSelection"
},
{
"key": "shift+f3",
"command": "editor.action.previousMatchFindAction",
"when": "editorFocus && !editorHasSelection"
},
{
"key": "shift+f3",
"command": "-editor.action.previousMatchFindAction",
"when": "editorFocus"
},
I have found what I was looking for.
Ctrl+D triggers the action Add Selection To Next Find Match which does exactly what I wanted: perform an immediate search for the selected text, just like F3 in Ultraedit.
Does anyone know how "Replace with next value" (ctr + shift + dot) works ?
I am unable to get this shortcut to do anything on my vscode.
I tried googling examples of this shortcut and asking on vscode gitter/reddit but couldn't get any answers.
It would be greatly appreciated if someone could provide the steps of using this shortcut.
If I press the shortcut after highlighting an integer or float literal (16 or 5.2 for example) it subtracts 1 from it.
Strangely, Replace with Previous Value adds 1 to the value while Replace with Next Value subtracts 1.
I don't know if it has any other purpose.
Add to the accepted answer:
If you use it on a boolean, it toggles between true and false.
No need to "highlight" the number, putting the cursor on it is enough.
As noted by #FĂ©lix Caron, this is how to swap the two keys.
keybindings.json
{ "key": "ctrl+shift+oem_period", "command": "editor.action.inPlaceReplace.up", "when": "editorTextFocus && !editorReadonly" },
{ "key": "ctrl+shift+oem_comma", "command": "editor.action.inPlaceReplace.down", "when": "editorTextFocus && !editorReadonly" },
Moreover, in my case as I have breadcrumbs enabled "breadcrumbs.enabled": true,, the replaceUp key triggered breadcrumbs instead, so I had to unbind it.
{ "key": "ctrl+shift+oem_period", "command": "-breadcrumbs.toggleToOn", "when": "!config.breadcrumbs.enabled" },
{ "key": "ctrl+shift+oem_period", "command": "-breadcrumbs.focusAndSelect", "when": "breadcrumbsPossible" },