How can I start multiple integrated terminal panels with a click? - macros

Within Visual Studio Code, I'd like to have the possibility to push a button or run a command that would create multiple terminal panels and run commands on them.
This is useful especially when dealing with code stored into frequently unmounted drives, where every time you mount it you have to start a few terminals and navigate to the proper directories.
Is it possible?

On Mac we can leverage the power of AppleScript to accomplish this.
Firstly, we need to add a keystroke for creating a new terminal instance:
{ "key": "cmd+n", "command": "workbench.action.terminal.new",
"when": "terminalFocus" }
Secondly, we create an AppleScript, like the following:
tell application "Visual Studio Code" to activate
tell application "System Events"
# Term 1
keystroke "`" using control down
keystroke "cd path/number/1"
keystroke return
# Term 2
keystroke "n" using command down
keystroke "cd path/number/2"
keystroke return
# Term 3
keystroke "n" using command down
keystroke "cd path/number/3"
keystroke return
# End
keystroke "`" using control down
end tell
Lastly, we add a VSC Task with the command:
osascript ./my_script.scpt
And then just run the task.
I hope somebody will find this helpful.

Related

Add shell command into vscode command palette

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.

VS Code - Shortcut to change the current directory in the PowerShell console to the currently opened file? [duplicate]

This question already has answers here:
How to quickly change shell folder to match the currently open file
(5 answers)
Closed 3 years ago.
I am running the PowerShell extension in Visual Studio Code with its PowerShell Integrated Console attached. I have two PowerShell scripts each open in separate tabs. The files are located in different directories.
I would like to quickly change the current directory to the currently active tab in the PowerShell console using a keyboard shortcut (e.g. Ctrl+Alt+D).
How does one accomplish this?
UPDATE: This is the best solution so far
(Thanks to Mark in the comments of the original post.)
In the keybindings.json file, add the following key binding:
{
"key": "ctrl+alt+d",
"command": "workbench.action.terminal.sendSequence",
"args": {"text": "cd \"${fileDirname}\"\u000D"}
}
This works in both cmd and PowerShell consoles.
(The only flaw is that it does not change the directory across drives for the cmd console only).
UPDATE 2:
A new command will be added in v1.39 workbench.action.terminal.newWithCwd, see add terminal here command. The example keybinding given is:
{
"key": "cmd+shift+h",
"command": "workbench.action.terminal.newWithCwd",
"args": {
"cwd": "${fileDirname}"
}
}
This will create a new terminal, not change the current terminal.
Previous answer
Credit goes to SeeminglyScience for this answer.
There is not currently a way to set a keyboard shortcut, but since that is not explicitly stated in the title, I will post another shortcut.
If you do not already have a file called Microsoft.VSCode_profile.ps1 in your "%UserProfile%\Documents\WindowsPowerShell" directory, create one.
Create a custom PowerShell editor command by adding the following to the Microsoft.VSCode_profile.ps1 file:
Register-EditorCommand -Name 'ChangeDirHere' -DisplayName 'Change to the Directory of the Current File' -ScriptBlock {
Set-Location ([System.IO.Path]::GetDirectoryName($psEditor.GetEditorContext().CurrentFile.Path))
}
Restart VS Code.
Open any PowerShell file.
Press Alt+Shift+S (or whatever keybinding you have for "Powershell: Show Additional Commands from PowerShell Modules").
Select the custom command from the drop-down list of PowerShell commands.

How to clear the entire terminal (PowerShell)

I had an issue. Using the clear or cls command in powershell clears only the visible portion of the terminal,I would like to know how to clear the entire terminal?
I use VSCode by the way.
To also clear the scrollback buffer, not just the visible portion of the terminal in Visual Studio Code's integrated terminal, use one of the following methods:
Use the command palette:
Press Ctrl+Shift+P and type tclear to match the Terminal: Clear command and press Enter
Use the integrated terminal's context menu:
Right-click in the terminal and select Clear from the context menu.
On Windows, you may have to enable the integrated terminal's context menu first, given that by default right-clicking pastes text from the clipboard:
Open the settings (Ctrl+,) and change setting terminal.integrated.rightClickBehavior to either default or selectWord (the latter selects the word under the cursor before showing the context menu).
Use a keyboard shortcut from inside the integrated terminal (current as of v1.71 of VSCode):
On macOS, a shortcut exists by default: Cmd+K
On Linux and Windows, you can define an analogous custom key binding, Ctrl+K, as follows, by directly editing file keybindings.json (command Preferences: Open Keyboard Shortcuts (JSON) from the command palette), and placing the following object inside the existing array ([ ... ]):
{
"key": "ctrl+k",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus && terminalHasBeenCreated || terminalFocus && terminalProcessSupported"
}
Using a command you can invoke from a shell in the integrated terminal:
Note: A truly cross-platform solution would require executing the VSCode-internal workbench.action.terminal.clear command from a shell, but I don't know how to do that / if it is possible at all - do tell us if you know.
Linux (at least as observed on Ubuntu):
Use the standard clear utility (/usr/bin/clear), which also clears the scrollback buffer.
From PowerShell, you may also use Clear-Host or its built-in alias, cls.
By contrast, [Console]::Clear() does NOT clear the scrollback buffer and clear just one screenful.
macOS:
Unfortunately, neither /usr/bin/clear nor PowerShell's Clear-Host (cls) nor .NET's [Console]::Clear() clear the scrollback buffer - they all clear just one screenful.
Print the following ANSI control sequence: '\e[2J\e[3J\e[H' (\e represents the ESC char. (0x1b, 27); e.g., from bash: printf '\e[2J\e[3J\e[H'; from PowerShell: "`e[2J`e[3J`e[H"
You can easily wrap this call in a shell script for use from any shell: create a file named, say, cclear, in a directory listed in your system's PATH variable, then make it executable with chmod a+x; then save the following content to it:
#!/bin/bash
# Clears the terminal screen *and the scrollback buffer*.
# (Needed only on macOS, where /usr/bin/clear doesn't do the latter.)
printf '\e[2J\e[3J\e[H'
Windows:
NO solution that I'm aware of: cmd.exe's internal cls command and PowerShell's internal Clear-Host command clear only one screenful in the integrated terminal (not also the scrollback buffer - even though they also do the latter in a regular console window and in Windows Terminal).
Unfortunately, the escape sequence that works on macOS ("`e[2J`e[3J`e[H" or, for Windows PowerShell, "$([char]27)[2J$([char]27)[3J$([char]27)[H") is not effective: on Windows it just clears one screenful.
(By contrast, all of these methods do also clear the scrollback buffer in regular console windows and Windows Terminal.)
right click on the powershell button,
then select clear,
when you are at the command window, type "clear" command, to clear the terminal window.

Ctrl+c not working in integrated terminal which uses Powershell

I'm using Powershell in the integrated terminal by adding the following line to the settings.json file.
"terminal.integrated.shell.windows": "C:\\WINDOWS\\system32\\WindowsPowershell\\v1.0\\powershell.exe",
It works very well, but usually, when I'm in Powershell, typing ctrl+c cancels what I had typed and opens a new line.
But in the integrated terminal it just prints ^C.
Is there a way to fix it or find an alternative method to achieve this?
Thanks
This is with VSCode and not necessarily with the PowerShell Extension. You can see this by just using the default cmd.exe terminal, CTRL+C does nothing. It does not print the ^C at all, and creates no new line.
If you want this to work as expected in the normal command prompt or PowerShell.exe you will need to submit an issue to VSCode repository and request it.
I would expect this is all tied to the keybindings.json file. I went through that file but could not find a command available to the same function that occurs in the full command prompt or console. So this will likely need a new command added for VSCode.
If you search through the keybindings file you can see the terminal has that key CTRL+C bound to copySelection when terminalFocus && terminalTextSelected. This is why the ^C is being output, and no new line is being added.
A workaround:
Pressing Esc will erase the line back to the beginning.

Sublime Text 3 build system: keep console running

I set up a build system in Sublime Text 3 to run Matlab files. This works really fine:
{
"cmd": ["/usr/local/MATLAB/R2013b/bin/matlab", "-nosplash", "-nodesktop", "-nojvm", "-r \"run('$file');\""]
}
The problem is I want to keep Matlab running in the Sublime console after $file is executed. Is this possible?
Thank you in advance.
Ok, I've finally found a solution that runs the build system in an external xterm terminal. If you use this Sublime will open a xterm window and execute the build system there. This window remains open, so e.g. Matlab plot windows will not be closed after execution of the code. I've combined the build system with and without the external terminal into one build system:
{
"cmd": ["/usr/local/MATLAB/R2013b/bin/matlab", "-nosplash", "-nodesktop", "-r \"run('$file');quit;\""],
"selector": "source.m",
"variants": [
{
"name": "xterm",
"cmd": ["xterm", "-e", "/usr/local/MATLAB/R2013b/bin/matlab", "-nosplash", "-nodesktop", "-r \"run('$file');\""]
}
]
}
and then assigned a user key binding to access the xterm variant easily:
[
{ "keys": ["ctrl+shift+b"], "command": "build", "args": {"variant": "xterm"} }
]
This xterm solution should also work with any other interpreter that you want to prevent from being closed after code execution finishes.
An alternative method is to have both Sublime and Matlab open and then set up a script with AutoHotKey (Windows) or Autokey (Linux) that copies the filename or the code to evaluate and then pastes this in Matlab's command window.
The benefits of this method are:
You maintain all of the useful features of MatLab as an IDE
You can evaluate code snippets (the F9 feature) as well as the whole file
See detailed instructions for Linux or Windows