How to "restart" on save in Visual Studio Code? - visual-studio-code

I've setup the Debugging with Chrome and it works reasonably well. However, every time I make a change & save the .htm file, I have to click the Restart icon in the toolbar for the changes to propagate to the instance of Chrome.
Is there a way for VS Code to "restart" when I save the file?
P.S. I am editing a local file (no web servers involved).

I wanted to do exactly the same thing and accomplished it by installing an extension that allows me to execute a VS Code extension command when a file is saved.
The extension is Save and Run Ext and is a fork off of another extension but allows you to execute VS Code extension commands.
I added the following configuration to my settings.json file:
{
"saveAndRunExt": {
"commands": [
{
"match": "\\.(css$|js$|html$)",
"isShellCommand": false,
"cmd": "workbench.action.debug.restart"
}
]
}
}
What this does is whenever a .css, .js, or .html file is saved, it will restart the debugger. You could of course configure it to do whatever you want.
An alternative (one step) solution is instead of saving the file, just restart the debugger, and it automatically saves all files and changes are propagated to Chrome.

Related

Reload file changes in VsCode after running custom task

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.

Visual Studio Code: shortcut to open a certain file

I have a text file on my local drive in which I like to take random notes. I want to be able to quickly open this file in VS Code from every project I work on. The Ctrl+P shortcut only shows files which had already been opened before in the current workspace, so the first time I open the file in a new workspace I still have to open it manually.
Does anybody know an extension or any built-in functionality providing a quick way to open a certain file that has not been opened before? My ideal solution would be a dedicated hotkey that just opens a configurable file.
I've searched the marketplace and tried several "favorites" extensions. None of them provides what I'm looking for.
The pitfall is that the solution has to work for local files in remote workspaces as well, i.e. I am connected to a remote server via VS Code's remote SSH extension and still want to open the file from my local filesystem.
Any ideas?
You can try the extension HTML Related Links v0.15.1 and the command htmlRelatedLinks.openFile to open a local file. Specify the full path.
At first I did not had this extension installed in the remote computer. VSC complained that the command htmlRelatedLinks.openFile did not exist. That means that the VSC remote window uses the local key binding file but executes the commands on the remote machine. This means you have different key bindings if you are at the physical keyboard of the remote machine compared to a remote connection. The same goes if you have set your XDISPLAY (??) environment variable (for Linux systems, remote display)
You tried it with v0.14 (see comment) and found out that VSC uses the scheme vscode-remote (not documented).
I modified the extension so we can set the scheme of the URI.
Using scheme file did not work. In local VSC all local file URIs have this scheme.
But if we use the scheme vscode-local (that you have found somewhere) I was able to open a file from the local disk in the remote VSC instance with a key-binding.
{
"key": "ctrl+i n", // or any other combo
"command": "htmlRelatedLinks.openFile",
"args": {
"file": "C:\\Projects\\Notes\\notes.txt",
"method": "vscode.open",
"viewColumn": "split",
"useScheme": "vscode-local"
}
}

VS Code - How to have Terminal pane open by default?

In Visual Studio Code when I open the application I have to re-open the integrated terminal window every time.
Do you know the steps to have the CLI pane open as soon as the application loads by default without manually doing ctrl-backtick every time?
You can try the following:
Install an extension: Auto Run
Command.
Reload Visual Studio Code after installation.
In settings ctrl+, add following:
"auto-run-command.rules": [
{
"command": "workbench.action.terminal.new",
},
],
No built in way to do this , however you can submit a feature request here.
Or wait for this extension to mature.
I'm new, so I can't post comments yet. But I think an improvement to the solution offered by #victor-s is to use workbench.action.terminal.toggleTerminal, since that won't create a new terminal if one is already open.

How to debug a VS Code extension when loaded from VSIX?

I have a vs code extension (currently private), and it runs fine when I run it via the VS Code debugger, (i.e., a "Launch Extension" target in my launch.json). However, when I install the extension from a vsix file, the extension never activates.
Here's what I tried:
View elements & commands show up, which implies the package.json is being read correctly.
I placed vscode.window.showInformationMessage(...) statements in my extension.js and extension.js's activate() methods. I see expected output when I launch via VS Code debugger, but not when installed via vsix file.
I changed the activation events to include "onLanguage:javascript", hoping that opening a js file would cause my extension to activate, but it didn't.
A few questions:
Any suggestions on what is going on here?
Any suggestions on how to debug this?
Any instructions on how I can attach a debugger?

Visual Studio Code: Auto-refresh file changes

Working with Visual Studio Code I have noticed if a file you are working with change, whenever that file get focused in a code panel it will be reloaded from the disk (if you don't have changes in the file through VSCode).
However, if you are on that file there is no alert to warning you about file changes.
I've been reviewing the settings and I cannot find anything like the visual studio option:
Detect when file is changed outside the environment
So my question: Is there any hidden setting or some hack to make that warning happen.
Update
Solved in version 0.3.0 of Visual Studio Code.
The file will be updated from disk if there is no changes through the editor. (very useful to read log files during a process execution)
If there are changes on both sides (from disk and through the editor) when ever you try to save the file using VSCode, the editor will warn you about that situation (i.e. "dirty writes") and a file comparison will allow you to decide what to do.
VSCode will never refresh the file if you have changes in that file that are not saved to disk. However, if the file is open and does not have changes, it will replace with the changes on disk, that is true.
There is currently no way to disable this behaviour.
{
"files.useExperimentalFileWatcher" : true
}
in Code -> Preferences -> Settings
Tested with Visual Studio Code Version 1.26.1 on mac and win
SUPER-SHIFT-p > File: Revert File is the only way
(where SUPER is Command on Mac and Ctrl on PC)
In version 1.57.1 (June 2021) there is still no setting like Detect when file is changed outside the environment.
But if the file was accidentally changed outside, you can easily revert the changes with just the Undo (Ctrl+Z) command
On Ubuntu, after creating a new file using vscode, it doesn't show up until I refresh the explorer manually. It's really frustrating. There was a key I changed in settings.json and everything worked fine after that:
"files.legacyWatcher": "on",
save, restart the vscode and be safe :)