Add shell command into vscode command palette - visual-studio-code

I have pandoc installed on my device. I want to add the pandoc export command so I can create PDF file by compiling that command in one click in vscode command palette (No need to type that long command again and again). Is there a simple way to do it besides creating my own extension?

In the Command Pallete, search for Preferences: Open Keyboard Shortcuts (JSON) - there are some similar ones so chose this one specifically. Then, in the file that it will open - keybindings.json - , add the below keybinding. There will be an outer [] surrounding all of the keybindings together.
Then, using whatever keybinding you chose, here alt+b and focus is in the terminal, it should print out that command. You will need to hit Return/Enter to actually run the command.
{
"key": "alt+b", // whatever keybinding you wish
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "pandoc '${file}' --pdf-engine=xelatex -o example13.pdf"
},
"when": "terminalFocus && !terminalTextSelected"
}
Use whatever your pandoc command is. ${file} is the current file.
You don't really need the "whwn" clause if you want to trigger it whether the terminal has focus or not - so you could comment that out if you used a unique "key". Also, if you want it to run without having to hit the Enter after it is printed into your terminal just add a \u000D (which is a Return) to the end like this:
"text": "pandoc '${file}' --pdf-engine=xelatex -o example13.pdf\u000D"

Create a custom task in VS Code that executes your pandoc command.
Then use the command palette (Tasks: Run Task) to execute it or create a keyboard binding for the task.
A task that converts the file currently opened in the editor would look like this:
{
"label": "Pandoc: Convert current file",
"type": "shell",
"command": "pandoc '${file}' ...",
"problemMatcher": []
}
Adjust the "pandoc '${file}' ..." command as required.
You can also reference other variables predefined by VS Code.

Related

How do I run the AAA command through the command interface?

for example, I will take the command to export a document to htiml using the asciidoc extension.
What command can be written in the terminal to run it through vscode?
Or maybe you can make a task for the command, which I usually run through the command pallet (Ctrl+Shift+P)?
p.s. I'm not interested in how you can simulate a command. I want to execute it with vscode tools.
This task (in tasks.json) will run that command:
"tasks": [
{
"label": "asciiDoc to html", // whatever name you wish
// find the command name from the extension or from Keyboard Shortcuts
"command": "${command:asciidoc.saveHTML}",
"type": "shell",
"problemMatcher": []
}
}
Run the task with the asciiDoc file you want converted as the current editor. That task can be assigned to a shortcut if you wish (in keybindings.json):
{
"key": "alt+r", // whatever keybinding you want
"command": "workbench.action.tasks.runTask",
"args": "asciiDoc to html" // same name here as in your task
}

Is it possible to get the full file name of the file you have open/are viewing in VS Code via the integrated Terminal?

In the VS Code terminal, is there a command that will give you the name of the file that you currently have open? e.g. just the file name IndexController.php?
I frequently run a test command in my terminal make test-watch /long/path/to/file.php and I need to right click on the file name and select "copy relative path" to get that path, which I then have to paste in.
It would save me a little bit of hassle if I could add a make test-watch-here that just read from the open file. Is it possible?
You can use the extension Command Variable v1.19.0
With the command extension.commandvariable.inTerminal you can type the result of a command in the terminal.
The extension contains also a command to transform a string with variables to there value.
An example for your case:
{
"key": "ctrl+i f5", // or any other combo
"command": "extension.commandvariable.inTerminal",
"args": {
"command": "extension.commandvariable.transform",
"args": { "text": "${relativeFile}" }
}
}
The key combo works in the editor and the terminal.
See the doc of the extension to see what you can do more with the transform command (like find/replace with regexp)

VSCode change key binding for cancel signal in terminal

Not sure if it's called the "cancel signal" but I'm talking about ctrl+c when you want to stop something running. I actually want Terminal: Copy Selection to be ctrl+c, and this I can change in Keyboard Shortcuts. But then I have no way of changing the cancel signal to what I want, which is: ctrl+shift+c. That's because I can't seem to find that key binding.
Ctrl+C is only valid for shells, which for VSCOde only run in terminal panes. The other key bindings you speak of apply to the VSCode IDE in general, not to any shell running in a terminal pane that might exist.
When a terminal pane has focus you should find that Ctrl+C works as usual. I do not think that key binding is reassignable since it is built into the shell running in the terminal pane.
However, VSCode provides the ability to kill the active terminal, or to relaunch it. Maybe you would like to assign hotkeys to those actions?
BTW, you can set the behavior for terminal panes, so they copy to clipboard whenever text is selected. No keystroke required. You can enable that feature by editing settings.json and adding this line:
"terminal.integrated.copyOnSelection": true,
You can send a control sequence to the terminal, see send text to a terminal like this:
{
"key": "ctrl+shift+c",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "\u0003\u000D" // Ctrl+C
},
// "when": "terminalFocus && !terminalTextSelected"
},
Use the when clause if you want it available only when the terminal has focus and with or without a selection.
\u003 is Ctrl+C see
/** End of Text (Caret = ^C) */
export const ETX = '\x03';
from escape sequences
and \u000D is a return from the same source.
And then your other code to set Ctrl+C to copy terminal text:
{
"key": "ctrl+c",
"command": "workbench.action.terminal.copySelection",
"when": "terminalFocus && terminalProcessSupported && terminalTextSelected && terminalTextSelected"
},
{
"key": "ctrl+shift+c",
"command": "-workbench.action.terminal.copySelection",
"when": "terminalFocus && terminalProcessSupported && terminalTextSelected && terminalTextSelected"
}
Demo already running a serve task :

Using keyboard shortcut to send file path in terminal, need path separators for git-bash on Windows

I have created a keybinding in vscode using this code in keybinding.json file-
{
"key": "f10",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "${file}",
},
"when": "editorFocus"
}
it sends the path of the focused file opened using backslash, like this,
C:\Users\User\Desktop\hello.cpp
it works in cmd. but my git bash needs path using slash.
Is there any way to send path with slash like
C:/Users/User/Desktop/hello.cpp
You need to go upvote this issue:
https://github.com/microsoft/vscode/issues/111457 (Git Bash Terminal Slash issue)
It doesn't look there is a way to do this without grabbing the result in a script and manipulating the string yourself.

Bind a keypress to a shell command that uses the current file in visual studio code

Is there a way to create a keybinding to execute a shell command on a file?
something like:
{
"key": "ctrl+shift+e",
"command": "run",
"command": "touch $file",
"when": "editorTextFocus"
}
I don't want to use tasks as this needs to be global for the whole editor, not to a particular workspace.
[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.
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
Run Shell Script with Keyboard Shortcuts
In this example I set a keyboard shortcut to run android reload on my react native project.
Install the macros extension to execute multiple commands.
After installed go to Preferences>Settings (or cmd+,)
Find Macros Configuration and use to edit in User Settings, or simply add the following in your user settings:
{
//... 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
],
}
}
Now that we have the command set, we need to set a trigger shortcut. So go to Preferences>Keyboard Shortcuts (or cmd+k), open keybindings.json and edit like this:
{
"key": "alt+s",
"command":"macros.runCommandInTerminal"
}
Save your User Settings customizations and keybindings.json, respectively
Bind file format and indenting with save shortcut
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.
],
//....
}
UPDATE 2:
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
Original:
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 1:
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.