I have created a task in VsCode to run the autoflake8 tool which removes the unused imports and variables from the python files and task definition looks like this
{
"label": "Run Autoflake8",
"type": "process",
"command": "/Users/rbhanot/.local/bin/autoflake8",
"args": ["-ri", "--remove-unused-variables", "${file}"]
}
The task runs just fine and the file does gets updated but the problem is that VsCode still keeps showing red error markers until I close the file and open it again. So VsCode is not able to detect that file has changed and reload the changed file.
Is there anyway to fix or alter this behaviour because let's say if I have multiple files open I need to reload them again manually which is a pain.
I have the Multi-command extension installed on VS Code and use it to launch Streamlit apps using this configuration in settings.json which is run via a keyboard shortcut:
"multiCommand.commands": [
{
"command": "multiCommand.streamlitActiveFile",
"label": "Streamlit: Run Active File",
"description": "Streamlit run active file in active terminal",
"sequence": [
"workbench.action.terminal.focus",
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "streamlit run ${relativeFile}\u000D"
}
}
]
}
]
This has worked fine thus far while I have been using the standard command prompt on a Windows machine. Now I've changed to using Git Bash as my default terminal, and the command above fails because the relative file path is passed as a Windows path with backslashes, and Git Bash can't parse that:
$ streamlit run visuals\streamlit\time_plot.py
Usage: streamlit run [OPTIONS] TARGET [ARGS]...
Try 'streamlit run --help' for help.
Error: Invalid value: File does not exist: visualsstreamlittime_plot.py
In short, the backslashes are ignored and the path becomes a single file name instead. I'd like some way to be able to launch Streamlit apps using a multi-command via a keyboard shortcut, and I'm open to either of these solutions, or something else I haven't considered:
Send a relative file path that can be parsed correctly regardless of which terminal is the default terminal.
Launch a Windows command prompt even when Git Bash is the default terminal, and activate the Streamlit app there.
Change the working directory to the directory containing the streamlit file (again regardless of which terminal is the default terminal) and launch the file there without having to give a path.
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}'"
}
I've always used Sublime Text 3 for Python Coding, in which Build Systems were the only way of running a file.
Now I'm starting C++ programming, and I'm using VSCode. The problem I have is that, in that dock where:
PROBLEMS , OUTPUT , DEBUG CONSOLE , TERMINAL
The only thing I've had trouble with, is OUTPUT window, because nothing ever appears, so if I wanna run either Python or C++ code, it won't show anything to let me know what output would the program give. And if I just try to run it (pressing f5) it, it will open terminal and run it from it.
I saw something called Tasks but I don't know how they work, and they seem to be like Build Systems from Sublime Text 3.
I wanna use OUTPUT window, how can I get it working?
to run code in the output section of visual studio code you can use the extension Code Runner
just install, then press CTRL + ALT + N to run the file (supports c++ & python)
I wanna use OUTPUT window, how can I get it working?
As it stands now, you can not use the Output Window as a way to log code, that is reserved for native logs like Window, Main, Shared or Extensions to log their output using channels. There is a good candidate called the Debug Console.
You can log to the Debug Console using a launch.json configurable:
First, create a launch.json for your program.
Then add the following properties, in addition to whatever you need to get your program going:
internalConsoleOptions
Controls when the internal debug console should open
redirectOutput
Both of these used in tandem will open the 'Debug Console' instead of terminal and just provide the necessary output; though it still sends to terminal should you still want it:
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"internalConsoleOptions": "openOnSessionStart",
"redirectOutput": true
}
]
For c++ they have an extensive tutorial on how to debug and run: https://code.visualstudio.com/docs/languages/cpp (in addition to setting up launch.json and tasks.json files respectively https://code.visualstudio.com/docs/cpp/config-mingw#_build-helloworldcpp)
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
ext install formulahendry.code-runner
With Visual Studio Code (not the traditional Visual Studio IDEs), can you run PowerShell in the Command Palette? I ask because I commonly use it in the full IDE.
I have not seen PowerShell mentioned in the documentation, other than for basic syntax highlighting. I have tried it with no success. Is it unsupported, or is it an optional feature I can configure somehow?
Note: to those voting up the PowerGUI answer, it is not correct as it references the wrong edition of Visual Studio for this question. It is helpful if you are using the full IDE, but not the new editor named: Code.
This was the first problem I wanted to solve in VSCode. I did not find a way to
type PowerShell commands but I have found a way to create and run PowerShell tasks.
In the command palette type and choose Tasks: Configure Task Runner. It will
show you a json file configured for the default task runner.
I replaced the content with this
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"-NoProfile",
"Invoke-Build.ps1"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
As a result in the command palette I can choose and run my predefined tasks (Build, Test, and combined Build, Test). I can add other tasks and probably bind them to some hotkeys. This is not exactly what I would like to have in VSCode for PowerShell but for the preview it is at least something.
P.S. This just my first experiment that somewhat worked. It is not perfect, more likely. There are many configuration parameters for this json file that I have not tried yet.
With version 0.10.1 of Visual Studio Code, you can run and debug PowerShell. Here is a sample launch.json that I use:
{
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell",
"type": "PowerShell",
"program": "MyScript.ps1",
"args": "-Verbose"
}
]
}
Unfortunately, I cannot get the args to work (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/24). Another problem I have is that Read-Host does not work with VS Code (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/29). Definitely some rough edges.
Here's how you can configure the Powershell task to execute the currently opened .ps1 file without any Invoke-Build dependency:
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"${file}"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
Note: This is just a slight modification of Roman's answer (My edit to his answer was rejected).
Open the Debug view, in the View Bar select Debug from the View menu or press Ctrl + Shift + D.
In the Launch Configuration dropdown (shown in the following screenshot), select Add Configuration...
The launch.json configuration will open in the editor, type PowerShell and select the debug configurations you want, as shown in the following screenshot.
Save the launch.json file and select the debug configuration you want from the Launch Configuration dropdown:
Now you can debug PowerShell scripts through VSCode.
The Scripting Guys wrote a comprehensive 2 part blog post on this topic, like everything else they write, it's well worth a read if you're new to VSCode and PowerShell.
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/06/debugging-powershell-script-in-visual-studio-code-part-1/
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/13/debugging-powershell-script-in-visual-studio-code-part-2/
EDIT: I am aware that this question is several years old, but I came across it before I found any up to date information on how to set up PowerShell debugging in VSCode
As of version 0.10.3 there is now a PowerShell extension written by Microsoft that will enable writing PowerShell with IntelliSense and highlighting in Visual Studio Code
http://blogs.msdn.com/b/powershell/archive/2015/11/17/announcing-windows-powershell-for-visual-studio-code-and-more.aspx
At least in V1.4 (July 2016) this has been made a lot simpler.
The relevant documentation can be found here:
https://code.visualstudio.com/docs/editor/integrated-terminal
Essentially all you're doing is adding an entry to the settings.json file for User Settings.
Adding the following value:
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows":"C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"
IMG: Shows the User Settings settings.json file and the successful Integrated Console showing the PowerShell prompt:
An "Integrated Terminal" is now integrated into Visual Studio Code. This is a PowerShell Terminal in Windows. (Likely BASH in Linux / MacOS). The terminal appears in a panel at the bottom of the code editor, not in the Command Pallete.
Keyboard Shortcut to open Terminal: CTRL + `
Sources:
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
https://code.visualstudio.com/docs/getstarted/keybindings
I confirmed in version 1.19.2. Not sure when the feature was first integrated.
There is a much easier way to run PowerShell, no configuration needed:
Install the Code Runner Extension
Open the PowerShell code file in Text Editor, then 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 context menu, the code will run and the output will be shown in the Output Window.
Besides, you could select part of the PowerShell code and run the code snippet. Very convenient!
PowerGUI tool provides a nice interface to Visual Studio. The goal of this extension is to bring PowerShell development into Visual Studio.
Here is how it looks -
Along with IntelliSense, the PowerGUI Visual Studio Extension provides the following features and more to make it easier to work with PowerShell.
PowerShell file and project types: You can create/edit PowerShell code files and assemble them into projects with more than one file.
PowerShell code snippets: The code snippet feature can be used for PowerShell code.
PowerShell console window: This feature provides the PowerShell console environment within Visual Studio IDE. This allows you to run commands or view output of scripts. Figure B shows the console window opened in the IDE.
PowerShell debugging: This feature is why I installed the extension; it provides a way to debug scripts within Visual Studio. It's a straight-forward way to locate syntax or logical problems in a script.
Syntax highlighting and script analysis: These are more Visual Studio features made available to PowerShell development.
In order to install the PowerGUI Visual Studio Extension, PowerGUI must be installed, with the caveat that the correct version of each product is installed.
Download Here
It also provides Debugging facility which is the best part I like as a developer.
Thanks!