I would like to add a key binding for inserting a line break in the editor (textInputFocus). Normally the Return key does this (VS Code calls the key Enter). Which command in VS Code can I use?
There is lineBreakInsert but this does not behave exactly the same as Enter normally does - it inserts a line break but keeps the cursor in the same position.
Is this what you are looking for:
{
"key": "ctrl+o",
"command": "type",
"args": {
"text": "\n"
},
"when": "textInputFocus"
}
Currently this is my workaround, but it's not a perfect solution:
I installed multi-command and added this binding (in keybindings.json):
{
"key": "ctrl+o",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"lineBreakInsert",
"cursorDown",
"cursorHome"
]
},
"when": "textInputFocus"
}
Related
Am new in VScode and i added a new shortcut to move the cursor right, as the right arrow key, forces me to move my hand, NOW THE RIGHT ARROW IS DISABLED and i can do other tasks such moving a full word right as the shortcut has completely replaced the right arrow key,
can someone tell me an easy shortcut to move the cursor to the right without the right arrow after each line of code, such as print(" hello world")cursor here
or tell me how to make both my custom shortcut sequence and the right arrow key work
also i noticed that the default keybinding (shift+alt+i) to move the cursor to the end of the line never worked.
thank you in advance
i found a similar post here: Visual Studio Code - multiple keyboard shortcuts?
and I've fixed moving right, but i can't do the same with the arrow up, no matter what keybinding i set,also would be nice to be able to add such moving shortcuts without having to affect the current right setting
**here is my current code on the keybiding.jsonfile - could anyone suggest a fix on this code without disrupting the current settings? **
{
"key": "shift+alt+i",
"command": "-editor.action.insertCursorAtEndOfEachLineSelected",
"when": "editorTextFocus"
},
{
"key": "shift+alt+r",
"command": "cursorRight",
"when": "textInputFocus"
},
{`enter code here`
"key": "Right",
"command": "cursorRight"
}
{
"key": "Left",
"command": "cursorLeft"
}
{
"key": "right",
"command": "-cursorRight",
"when": "textInputFocus"
},
{
"key": "shift+alt+l",
"command": "cursorLeft",
"when": "textInputFocus"
},
{
"key": "left",
"command": "-cursorLeft",
"when": "textInputFocus"
}
I've deleted all the commands and replaced them with this all seems to work enter code here
{
"key": "alt+r",
"command": "cursorRight",
"when": "textInputFocus"
},
{
"key": "Right",
"command": "cursorRight"
},
// down
{
"key": "alt+d",
"command": "cursorDown"
},
// UP
{
"key": "alt+u",
"command": "cursorUp"
},
// LEFT
{
"key": "alt+l",
"command": "cursorLeft",
},
{
"key": "Left",
"command": "cursorLeft"
},
First, I have a shortcut on VSCode to Wrap a text with the thing I'm typing.
<div>
Hello World
</div>
If I select "World" and use the Emmet: Wrap with Abbreviation shortcut and type span I can make this:
<div>
Hello <span>World</span>
</div>
But here's the thing :
I know we can create custom wrapping that are not the same on each side of the word we selected (source: VS Code : create custom snippet/shortcut)
{
"key": "ctrl+i",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "{something}$TM_SELECTED_TEXT{/some other thing}"
},
"when": "editorTextFocus && editorHasSelection"
}
What I would like is to select my variable, then use the shortcut, and it will print what I need UNDER the selected line, and whitout breaking the current line where I come from.
For this example, I'm selecting the var $user_id, press shortcut, and then boom it will add the second line.
$user_id = User::where('user_name', $user_name)->get()->first()->id;
dd($user_id);
Here's a start:
{
"key": "ctrl+alt+c",
"command": "editor.action.insertSnippet",
"args": {
"snippet": " ==> here we need to find how to line break without spliting the code and then: <==
{console.log(}$TM_SELECTED_TEXT{)}"
},
"when": "editorTextFocus && editorHasSelection"
}
Do you think it is possible ? Maybe the solution is to use Keyboard Macro separately from VSCode ?
I came up with this solution, thanks to #Mark in comments, related to this thread : How can I insert a snippet on a new line with vscode?
Install Multi-command VSCode extension
Open the settings file of the extension (settings.json)
Implement your code (here's mine with console.log() and dd() )
"multiCommand.commands": [
{
"command": "multiCommand.console.log",
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "console.log(\"$CLIPBOARD: \", $$CLIPBOARD)\n$0"
}
},
]
},
{
"command": "multiCommand.dd",
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "dd($$CLIPBOARD);"
}
},
]
}
Implement the shortcut in your VSCode settings (keybindings.json)
{
"key": "ctrl+1",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.console.log" }
},
{
"key": "ctrl+2",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.dd" }
}
If I'm understanding your question correctly, you can probably just use $0 to show where your cursor will end and use \n to insert a line break.
However, I'm not entirely sure if this works when creating a snippet from the Keyboard Shortcut file, but it works from the snippet file so I'm assuming it'll work here.
I would like to assign a keyboard shortcut which-
puts the cursor at the end of current line
and then, insert ->
I googled a lot. But not found any satisfactory solution.
Any idea ?
If your cursor doesn't start at the end of the line you will need to use a macro extension to run two commands. Using multi-command below, make this keybinding (in your keybindings.json):
{
"key": "alt+w", // whatever keybinding you like
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"cursorEnd",
{
"command": "type",
"args": { "text": "->" }
}
]
},
// "when": "editorLangId == php" // for example
}
Steps to reproduce:
simple code:
if () {}
cursor is between {}
i press Enter
result:
if () {
}
expected result:
if () {
}
I want a empty line not to be inserted.
It may be that it works by default(adds a empty line), and when Alt+Enter it doesn't add a empty line.
I did not find settings in vscode. I didn't find anything on google.
I tried this:
{
"key": "alt+enter",
"command": "type",
"args": {
"text": "\n"
},
"when": "editorTextFocus"
}
Because Alt+Enter does nothing by default.
However, the onEnterRules function used with the editor.autoIndent option detects the addition of the \n character and adds an extra empty line anyway. :(
I want to use editor.autoIndent. But I want to turn off (do not turn on) using the shortcut Alt+Enter.
Worst option: look for an extension that does exactly the same as editor.autoIndent, but has the ability to create a shortcut Alt+Enter to work the way I want.
You can use the extension multi-command and construct a command that does what you want.
Add this to your settings.json (global or workspace)
"multiCommand.commands": {
"multiCommand.lineBreakNoEmptyline": {
"sequence": [
"lineBreakInsert",
"deleteWordRight",
"cursorRight",
"cursorHome"
]
}
}
Add this to your keybindings.json:
{
"key": "alt+enter",
"command": "multiCommand.lineBreakNoEmptyline",
"when": "editorTextFocus"
}
Or using the keybinding only method
{
"key": "alt+enter",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"lineBreakInsert",
"deleteWordRight",
"cursorRight",
"cursorHome"
]
},
"when": "editorTextFocus"
}
I'll show the info from my comment here so that it is clearer. This keybinding:
{
"key": "alt+enter", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
// "lineBreakInsert",
{
"command": "type",
"args": {
"text": "\n"
}
},
"editor.action.clipboardCutAction"
]
},
}
The type command acts differently than a lineBreakInsert command so it is a little easier to then delete that extra line as the cursor is already there. It is just a small improvement, 2 less commands.
Demo:
I'm looking for a shortcut that would clear the debug console + the terminal, and that would work when my cursor is on the editor.
I tried this code in the keybindings.json which only works for the terminal, and when the cursor is on the terminal (unless I removed the "when" part). But in any case this doesn't clear the debug console.
{
"key": "ctrl+k",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus"
},
{
"key": "ctrl+k",
"command": "workbench.debug.panel.action.clearReplAction",
"when": "inDebugRepl"
},
You will probably have to use a macro extension like multi-command that will allow you to run multiple commands.
In your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.clearTerminalandDebugConsole",
"sequence": [
"workbench.action.terminal.clear",
"workbench.debug.panel.action.clearReplAction"
]
}
]
and in keybindings.json:
{
"key": "ctrl+alt+k",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.clearTerminalandDebugConsole" },
// below since you wanted it to work with editor focus
"when": "editorTextFocus"
},
You used Ctrl-K but that is a sequence used in many already-bound conflicting commands, so I used Ctrl-Alt-K.