Is there a way to create a keybinding to execute a shell command on a file? something like:
{
\"key\": \"ctrl+shift+e\",
\"command\": \"run\",
\"comm
with Marks answer here I could resolve my case that was run an android command with some shortcut. After that I decided to create an guide on my GitHub, hope that helps.
GitHub: LucasMMota
{
//... other configs you could have
"macros": {
"runCommandInTerminal": [
"editor.action.insertLineAfter", // go to new line below
{
"command": "type",
"args": {
"text": "adb shell input text \"RR\" " // exec cmd to reload android device (could be any else)
}
},
// select text typed above
{
"command": "cursorMove",
"args": {
"to": "wrappedLineStart",
"by": "wrappedLine",
"value": 1,
"select": true
}
},
"workbench.action.terminal.runSelectedText", //run command
"editor.action.clipboardCutAction", // remove cmd typed
//"editor.action.deleteLines", //couldn't use. When uncommented, command doesn't work
],
}
}
{
"key": "alt+s",
"command":"macros.runCommandInTerminal"
}
keybindings.json
:
{
"key": "cmd+s",
"command": "macros.saveContentAndIndent",
"when": "editorTextFocus && !editorReadonly"
},
User Settings
:
"macros": {
"saveContentAndIndent": [
"editor.action.formatDocument", // format
"workbench.action.files.save", // as I used Save shorcut, I add the save bind again.
],
//....
}
[See my edit below - this is now much easier to do.]
I figured out a way to do what you want but it is a bit of a hack. As you probably know it is easy to bind a task if you can create the task in .vscode/tasks.json and use something like the following:
{
"key": "shift+escape",
"command": "workbench.action.tasks.runTask",
"args": "Start server and process files"
},
It is much trickier to run a script via a keybinding without a pre-existing task. However, you can try the following which takes advantage of the "workbench.action.terminal.runSelectedText",
command. But we need to get the script into a selection first so:
Use the macros extension (which is a little rough) so that we can tie together multiple commands. In your user settings:
"runCommandInTerminal": [
{
"command": "type",
"args": {
"text": "node -v"
}
},
{
"command": "cursorMove",
"args": {
"to": "wrappedLineStart",
"by": "wrappedLine",
"value": 1,
"select": true
}
},
"workbench.action.terminal.runSelectedText",
"editor.action.clipboardCutAction",
// "workbench.action.terminal.focus"
],
This is a general example of a setting "runCommandInTerminal"
that you can then bind to any key chord you wish in keybindings.json, like
{
"key": "ctrl+shift+e",
"command": "macros.runCommandInTerminal"
},
Your example is a little harder because you want to access something like ${file}
which you cannot do in the settings, only tasks.json and launch.json. Fortunately, there is a command which will get the current file: "copyFilePath"
. So try
"runCommandInTerminal": [
"copyFilePath",
{
"command": "type",
"args": {
"text": "touch "
}
},
"editor.action.clipboardPasteAction",
{
"command": "cursorMove",
"args": {
"to": "wrappedLineStart",
"by": "wrappedLine",
"value": 1,
"select": true
}
},
"workbench.action.terminal.runSelectedText",
"editor.action.clipboardCutAction",
// "workbench.action.terminal.focus"
],
First get the file path, then output the first part of your script command "touch
".
Next append the filepath to the end of the command.
Move the cursor to select the preceding.
Run the selection in the terminal.
Cut the selection from the editor.
This can be run with your keybinding. You will see the flash of the script being typed and cut but your editor code will be unaffected. It is best to run it from a blank line in the editor but you can run it from the beginning of a line of unrelated code if you wish (but indentation may be lost for now).
It is hacky but seems to work. I would love to know if there is an extension or another way to do this cleaner.
EDIT May, 2019:
Since my original answer (as @Jeff indicated) vscode has added the sendSequence
command. And the ability to use variables with that command. So now the OP's question is much easier to accomplish:
{
"key": "alt+x",
"command": "workbench.action.terminal.sendSequence",
"args": {"text": "touch ${file}"}
}
in your keybindings.json file. See variables with a sendSequnce command.
Update: VS Code now provides variables for integrated terminal commands. https://code.visualstudio.com/updates/v1_32#_variable-support-in-send-sequence-command
If you'd like to do more powerful tasks such as running them in the background (instead of just sending text to the current terminal) get the Command Runner or macro-commander extension
I created an extension macro-commander to solve this with a non-hacky solution. It is an improved version of the macros
extension that runs the commands in order (synchronously) and lets you inject the filepath/folderpath into the command. Commands can be run in the user's VS Code terminal or a hidden background terminal.
UPDATE: As of today (months later) I realized, much to my dismay, there was an existing extension that can probably do what you (and I) wanted. It is Command Runner written by edonet. If I had known about it I probably would've never made my extension.
Command Runner doesn't have as much generic possibilities (multiple commands, user input, or javascript) but it has a better UX and is probably the extension you (the reader) should try first. However, if Command Runner can't do what you're looking for, then macro-commander probably can, albeit with less elegance.
Whenever I decide to do a full update to macro-commander I'll talk with edonet and see if we can combine/add the functionality into his extension, in which case I'll update the macro-commander README to redirect to his/her extension.