Shortcut to run code via integrated terminal - visual-studio-code

Currently Ctrl+Enter runs code from the main directory.
I want to change this shortcut to run code from the integrated terminal directly.
My current workflow is:
right click file.py
Open in integrated terminal
type python file.py
Is there way to change the shortcut above to use the workflow instead.

If you want, you can make a task and assign a keybinding to it.
If you don't have a tasks.json file, go to the Command Palette (Ctrl+Shift+P if that hasn't been remapped), and enter Tasks: Configure Tasks, Configure tasks.json file from template, and Others. You should now have a tasks.json file.
The tasks key is an array, and you can insert an object there. For your case, you can have
{
"label": "Run python file",
"type": "shell",
"command": "py",
"args": ["${file}"],
"group": {
"kind": "build",
"isDefault": false
}
}
To have the keybinding, go to Keyboard Shortcuts (Settings > Keyboard Shortcuts [Ctrl + K Ctrl + S if this hasn't been remapped]) and click Open Keyboard Shortcuts (JSON).
There, define your own keybinding. For now, I choose F5F5, and we'll add this object.
{
"key": "f5 f5",
"command": "workbench.action.tasks.runTask",
"when": "editorLangId == python",
"args": "Run python file"
}
To explain, when we press F5 followed by F5 and we are an editor identified as a Python file, VSCode will run the command workbench.action.tasks.runTask. To choose which task VSCode will run, we'll provide the name of the task through the args key which is Run python file.

Related

How to pass arguments to existing vscode commands

I would like to be able to run an existing vscode command or extension and pass arguments to it. From my research so far this is what I would expect the vscode task.json to look like, but it does not work. What am I doing wrong?
{
"label": "open index html in editor",
"command": "${command:workbench.action.files.openFile}",
"args": ["/home/host_user/src/index.html"]
},
{
"label": "liveView index html",
"command": "${command:extension.liveServer.goOnline}",
"args": ["/home/host_user/src/index.html"]
},
If I run the file command it opens a vscode prompt to select a file. If I try to liveView the file, it runs live view on my root workspace (as no path is getting passed to it).
This is the extension I'm trying to open with file with:
https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer
Thanks

Can vscode open files with external programs?

Is there any way to set vscode such that double-clicking a .docx file in the embedded file explorer causes the file to be opened in Microsoft Word?
Context
I'm used to PyCharm but I've been migrating to vscode. I'm used to creating Word document files (.docx) and double-clicking them in the file explorer on the left side sub-window to launch Word and see what the document looks like. This works in PyCharm, but in vscode it tries to open the file as a binary and claims it has no editor. Even if it did had an editor, I wanted it to open in my second monitor (or at least to be able to move it to my second monitor). For the time being, I'm opening a file explorer window and double clicking the file there, which has been less than optimal.
It's not quite double clicking, but you could create an open file command as a task, then bind that task to a keyboard shortcut, which amounts to one click and one hotkey.
Creating the open file task
We preferably make this task user-level, so that is accessible in every project.
ctrl+shift+p and search Tasks: Open User Tasks
In the tasks.json file, add the open file task to the tasks array:
// Inside "tasks": [ ... ]
{
"label": "open file",
"type": "shell",
"command": "${file}",
"presentation": {
// Hides the task terminal from showing up
"reveal": "never",
}
}
Change the "command" to whatever the open file command for your shell is (just ${file} works for windows, but change it to xdg-open ${file} for linux)
Assigning a keybinding to the open file task
To bind the task command to a keybinding, ctrl+shift+p and search keyboard shortcuts (JSON), open the file. Add the following to the array (changing "key" to whatever combination you desire)
// [ ... ]
{
"key": "ctrl+alt+l",
"command": "workbench.action.tasks.runTask",
"args": "open file"
}
To open any file, click on it to focus it in a tab (which sets the ${file} defined in tasks.json) and use the assigned hotkey. (As a side note, opening files that have vscode as their default program will open it in the same instance, so it makes it seem like the task isn't doing anything)
To work, Windows users should take care with command quotes
tasks.json :
{
"label": "open file",
"type": "shell",
"command": "'c:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE' '${file}'"
}

How to set a keymap to run code in Visual Studio Code?

I currently have multiple keymaps to run code, meaning a separate shortcut for Python, Java, C++, C. Is there a possibility to bind the run method for all of them to a single key?
You might need the extension Code Runner.
In the description of the extension there is:
To run code:
use shortcut Ctrl+Alt+N
or press F1 and then select/type Run Code,
or right click the Text Editor and then click Run Code in editor context menu
or click Run Code button in editor title menu
or click Run Code button in context menu of file explorer
Hope it helps ;-)
You can add the editorLangId to the where property
{
"key": "ctrl+alt+f5",
"command": "run python",
"when": "editorTextFocus && editorLangId == python"
},
{
"key": "ctrl+alt+f5",
"command": "run c++",
"when": "editorTextFocus && editorLangId == cpp"
}

Is there a way to set up a shortcut to call Build Tasks in VS Code?

Currently, I have a Build Task set up in Visual Studio Code (not Visual Studio). When I press Ctrl+Shift+B, I get a list of my build tasks, I then have to select my task and then it will compile and run my program.
Is there an easier way to do this, so instead of Ctrl+Shift+B -> Enter, I can just press one button and have a preset Build Task run? Either a keyboard button or a GUI button will work great.
Mark the task as your default build task via Terminal -> Configure Default Build Task... This simply adds the following to the task in tasks.json:
"group": {
"kind": "build",
"isDefault": true
}
After that, Ctrl+Shift+B will run the task directly.
Additionally, you could also have a default test task with "kind": "test". That task can be directly run with the Tasks: Run Test Task command (no shortcut assigned by default).
And finally, if having two shortcuts is still not enough (or you don't want to modify tasks.json), you can set up keybindings to run tasks directly by their name:
{
"key": "ctrl+b",
"command": "workbench.action.tasks.runTask",
"args": "run"
}
Replacing run with the label of your Build Task.
To open keybindings.json press Ctrl+K Ctrl+S or click File -> Preferences -> Keyboard Shortcuts. You may need to add [] if the file was previously empty.
You can add this code in keybindings.json located at C:\Users\%User%\AppData\Roaming\Code\User\:
[
{
"key": "ctrl+shift+r",
"command": "workbench.action.tasks.runTask",
"args": "run"
},
// [...]
]
Source: https://lronaldo.github.io/cpctelera/files/buildsys/vscode_integration-txt.html

VS Code Key Binding for quick switch between terminal screens?

I'm trying to figure out if there is a way to set up a key binding to allow a quick switch between the terminal windows I have open in the built in terminal rather than having to click on the drop down selector each time. Does anyone know the command for this when making a personal key binding or where I can see a list of all the possible commands for VSC? Thank you in advance!
If you want something that might feel a bit more fluid than using arbitrary key bindings, you can get Ctrl+Tab and Ctrl+Shift+Tab working for both editor switching and terminal switching.
Open your keybindings file with ctrl+shift+p search for Open Keyboard Shortcuts (JSON). Then add..
{
"key": "ctrl+tab",
"command": "workbench.action.openNextRecentlyUsedEditorInGroup",
"when": "editorFocus"
},
{ "key": "shift+ctrl+tab",
"command": "workbench.action.openPreviousRecentlyUsedEditorInGroup",
"when": "editorFocus"
},
{
"key": "ctrl+tab",
"command": "workbench.action.terminal.focusNext",
"when": "terminalFocus"
},
{
"key": "ctrl+shift+tab",
"command": "workbench.action.terminal.focusPrevious",
"when": "terminalFocus"
}
From Microsofts Documentation there is a tip:
Tip: If you use multiple terminals extensively, you can add key
bindings for the focusNext, focusPrevious and kill commands outlined
in the Key Bindings section to allow navigation between them using
only the keyboard.
From here:
Other terminal commands are available and can be bound to your
preferred keyboard shortcuts. They are:
workbench.action.terminal.focus: Focus the terminal. This is like
toggle but focuses the terminal instead of hiding it, if it is
visible.
workbench.action.terminal.focusNext: Focuses the next terminal instance.
workbench.action.terminal.focusPrevious: Focuses the previous terminal instance.
workbench.action.terminal.kill: Remove the current terminal instance.
workbench.action.terminal.runSelectedText: Run the selected text in the terminal instance.
Just assign these shortcuts to your preferred keybindings and you are good to go.
That might not be a solution for jumping directly to a terminal (e.g. like vims gt2) but it certainly is a start.
Edit: Just toyed around and found that you can also focus on a specific terminal. Just add any of these commands to your keybindings.json and you are good to go!
// - workbench.action.terminal.focusAtIndex1
// - workbench.action.terminal.focusAtIndex2
// - workbench.action.terminal.focusAtIndex3
// - workbench.action.terminal.focusAtIndex4
// - workbench.action.terminal.focusAtIndex5
// - workbench.action.terminal.focusAtIndex6
// - workbench.action.terminal.focusAtIndex7
// - workbench.action.terminal.focusAtIndex8
// - workbench.action.terminal.focusAtIndex9
e.g.
{ "key": "yourkeybinding", "command": "workbench.action.terminal.focusAtIndex1"}
Here's the solution I built that works well for me on OSX.
It mimics the same shortcuts used in the editor to open new files (cmd+n) and switch between tabs (cmd+left|right) and the same shortcuts work for terminals when that view is in focus.
Hit cmd+shift+p and type keyboard to find Preferences: Open Keyboard Shortcuts File
Add the following to the keybindings.json file and save it.
{
"key": "cmd+alt+right",
"command": "workbench.action.terminal.focusNext",
"when": "terminalFocus"
},
{
"key": "cmd+alt+left",
"command": "workbench.action.terminal.focusPrevious",
"when": "terminalFocus"
},
{
"key": "cmd+n",
"command": "workbench.action.terminal.new",
"when": "terminalFocus"
},
{
"key": "cmd+w",
"command": "workbench.action.terminal.kill",
"when": "terminalFocus"
}
It also does the same for closing terminals (cmd+w)
Based on Haini answer, Add the code below to your keybindings.json
{
"key": "shift+up",
"command": "workbench.action.terminal.focusPrevious",
"when": "terminalFocus"
},
{
"key": "shift+down",
"command": "workbench.action.terminal.focusNext",
"when": "terminalFocus"
}
Now, you can switch between terminals using shift + down or shift + up
on mac, ⇧⌘[ and ⇧⌘] worked for me.
this allows me to switch between different terminal instances when the terminal has focus. i use ⌘j to toggle focus between the terminal and editor.
source:
Navigate between terminal groups using focus next ⇧⌘] and focus previous ⇧⌘[.
https://code.visualstudio.com/docs/editor/integrated-terminal
As for now it already switches tabs on the editor and terminal, just try ctrl + page up or ctrl + page down.
To switch between terminal you can use below commands
On Mac
cmd + shift + [
cmd + shift + ]
This command will switch between terminal group. To find all commands:
command + shift + p (on mac)
Open keyboard shortcuts