Start VScode with WSL properly - visual-studio-code

When I open VScode through Ubuntu PowerShell with "code ." I don't have instant inline syntax checking while coding. Also all filenames are white:
First connexion from PowerShell with command "code ."
The weird thing is that if I click on the lower right corner and I reopen the folder in Windows: Reopening Folder in Windows
then if I reopen it again in WSL, finally I get the filenames colored and the inline syntax check works!
Back to WSL
Seems it wasn't properly connected the first time or something.
How can I properly boot VScode with WSL directly with a PowerShell command without having to switch to windows and then back to WSL?
This is how c_cpp_properties.json looks:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

Give this a try, install the following extensions:
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner
Note: I guess you have gcc installed already

Your connection to WSL is probably not being properly established.
To ensure that the connection is established properly, you can try launching VSCode using the code command with the --remote option, like this:
code --remote wsl+<distribution_name> .
Using --remote will ensure that VSCode with the WSL instance, and should prevent the connection from breaking as easily. You should see inline syntax checking and colored filenames right away.
[NOTICE] When you do this for the first time, you may get a prompt to install the WSL extension. Click install, close VSCode, wait about 10s, and then reopen it again using the same command. You may have to do this at least 2-3 times depending on your setup.
If you continue to experience issues, you may want to check your VSCode settings to ensure that they are configured properly for working with WSL. For example, you can try setting the remote.WSL.useWsl setting to true to ensure that VSCode uses WSL as the default terminal.
I haven't used VSCode like that in quite a while though, so the settings may be different and the remote.WSL.useWsl key may not exist anymore. It's worth trying though.

Related

VSCode: "Shell Integration failed to activate"

It used to work fine, but recently whenever I run my code (Python), I get the message "Shell Integration failed to activate" when I hover my mouse over the active terminal. The active Python terminal doesn't recognize any command line commands like "pip" anymore (see screenshot)
What can I do? I played around with a couple of settings as described here, but nothing seems to work. My current setting.json file looks like this:
{
"python.defaultInterpreterPath": "C:\\Users\\cleme\\AppData\\Local\\Programs\\Python\\Python310\\python.exe",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"path": "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",
"source": "PowerShell",
"icon": "terminal-powershell",
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell"
}
I ended up deleting the whole Code folder within the C:\Users\AppData\Roaming.
Note: You have to check the box - view Hidden Files to see these folders within File Explorer.
This solved it.
There must have been some weird corrupted cache issue at the heart of this.
I found this in C:\Users<profile>\AppData\Roaming\Code\logs<datetimestamp>\ptyhost.log
[2022-10-17 07:25:50.695] [ptyhost] [warning] Couldn't get layout info, a terminal was probably disconnected Could not find pty on pty host
[2022-10-17 07:25:54.040] [ptyhost] [warning] Shell integration cannot be enabled for executable "C:\Windows\System32\cmd.exe" and args []
[2022-10-17 07:26:57.939] [ptyhost] [warning] Shell integration cannot be enabled for executable "C:\Windows\System32\cmd.exe" and args []
I think it is related to this.
BTW: I had tried adding a couple of PowerShell VSCode IDE Extensions when things went awry.
IIRC they were:
PowerShell
PowerShell Preview
I had upgraded PowerShell to the latest version 7 too.
$PSVersionTable yielded the following from the respective executables.
Executable Path
Version
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
5.1.17763.1852
C:\Program Files\PowerShell\7\pwsh.exe
7.2.6
Maybe that combo hosed things
Follow up note: Whilst it seemed to have fixed the issue once... soon after the problem manifested itself again. This time I couldn't reset VS Code to a working environment.
Then I realised had opened the Project folder in the explorer on the left of the IDE.
I had a subfolder that had a PS1 file within that.
It didn't have its own .vscode\settings.json
When I copied it down from the parent folder, suddenly everything worked again.
.vscode\settings.json just contained this:
{
}
Again this ended up being a one time solution.
I ended up back at:
Shell activation failed to activate for cmd.exe
I posted this as a new ticket here
In the end I found this to be more of a display issue than anything else!
You can actually type cls then hit ENTER, to restore things to the status quo.
The Terminal section is black with no prompt to begin with. But you can type in it!!!
Perhaps the Prompt is black text on a black background initally, since when you start typing cls, it's quite a way to the right of the left margin.
Closing VSCode, verifying that powershell works, then reopening it worked for me.

Live Server Extension in VS Code is not working in WSL

I have been working with visual studio a long of time ago in Windows without any problems.
I installed WSL in my PC, all perfect.
But now I migrated one of my webpages directory to the WSL root
\wsl$\Ubuntu\home\user\webPage
And when I click on "Live Server" extension, it loads propertly in:
http://127.0.0.1:5500/index.html
But when I make a change and save, is not working
Do I have to change something else? Of course all the extensions are installed, seems to be the same
Modify this settings in your VS Code extension:
"liveServer.settings.https": {
"passphrase": "true",
"key": "true",
"cert": "true",
"enable": 1
},
You have to install this extension Remote Developer Extenson Pack
Once you do this, open your Visual Studio normally and you will be able to develop, there are some extensions not compatible.
More references here
It is important to see in your VS code in the left-down side, that you are in WSL:Ubuntu in color green, You can also open in that sideWSL UBUNTU
I had the same issue, but when trying it on Windows, it was working correctly, so I guess that Live Server will try to load it as localhost (which is 127...) but WSL doesn't use that. This can be confirmed by running ifconfig on the WSL Terminal.
I found a setting on the extension called "Use local IP". Once checked, it will open the Window with the WSL address instead of localhost and changes are shown properly.

Visual Studio Code WSL Remote - WSL Terminal Ignoring Cwd

I have read several other forums and posts about setting the default directory when opening a new integrated terminal session to no avail.
When I first start using VSC it seems to open the workspace directory for the first couple days. But it must have synched my settings from a previous install and brought back something that I could never figure out on my previous windows install. For some reason when it opens a terminal it opens to the AppData folder location for Microsoft VS Code as shown below:
<user>#<hostname>:/mnt/c/Users/<user>/AppData/Local/Programs/Microsoft VS Code$
If for instance I go into File > Preferences > Settings and try adding $HOME or ${HOME} I get:
The terminal process failed to launch: STarting directory (cwd)
"/home/<user>/${HOME}" does not exist.
or
The terminal process failed to launch: Starting directory (cwd)
"/home/<user>/$HOME" does not exist.
After doing this if I delete the information in Cwd then I receive the error that:
Setting has an invalid type, expect "string". Fix in JSON.
I can fix this by changing the setting to:
"terminal.integrated.cwd": "."
My user profile is basically default:
{
"workbench.editorAssociations": [
{
"viewType": "jupyter.notebook.ipynb",
"filenamePattern": "*.ipynb"
}
],
"git.autofetch": true,
"workbench.colorTheme": "One Dark Pro",
"python.languageServer": "Pylance",
"terminal.integrated.scrollback": 10000,
"terminal.integrated.tabs.enabled": true,
"terminal.integrated.tabs.location": "left",
"terminal.integrated.bellDuration": 10000,
"terminal.integrated.cwd": "."
}
There are only a few settings I changed like tab location and scrollback value.
Any thoughts?
Re-install and newer versions of WSL seemed to be the only fix for this.

VS Code opens a new debug console every time I build/run

Every time I build or run a program in VSCode a new python debug console is loaded. Before I know it I have 20+ and need to start deleting them. After 32 open consoles I get the error "The terminal process terminated with exit code: 256". I changed the terminal from the default console to git bash recently. How can I stop this?
A way around this issue is to stop VS Code from redundantly printing to the TERMINAL during debugging in the first place. Since it prints to the DEBUG CONSOLE as well, you can use that instead.
Change console to "none" "internalConsole" in each configuration in your project's launch.json file:
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
}
]
May 2019 Update: the "none" option was replaced by "internalConsole" so I edited my answer to reflect that. Here's the relevant GitHub Issue.
Adding "args": ["&&", "exit"], to launch.json remedies this for Git Bash. Certainly a hack, but I no longer have to manually close many debug terminals.
This may have been resolved in recent debug updates to core VS Code within the last year (as of 8/2022), but I was still seeing this intermittently.
I don't know if this will help the original poster, but I discovered that for me the issue persisted due to using Git Bash as the default terminal in Windows. Switching to Command Prompt as the default terminal fixed the issue. I haven't tested with other platforms or terminals.
Changing the default terminal to Command Prompt causes the Python extension to launch the "Python Debug" terminal with Command Prompt instead of Git Bash. I did log a VS Code/Python Extension defect about this. The initial response is that Git Bash is not officially supported currently.
There appears to be a communication problem between the Git Bash terminals and VS Code that causes this issue. Some of the characters between Git Bash and VS Code get dropped. Sometimes this mangles the debug command and I get and error and have to retry in addition to getting an extra debug window.
There is some additional background info and hacks to fix this from the past in this answer.
Hopefully fixed in the Insiders Build and should be in v1.54. See Debug opens a new integrated terminal for each Python session
. Test it in the Insiders Build if you can and report at the issue if it fixed/did not fix.
Actually you can delete all the instances of the terminal just by clicking on the trash can icon 🗑. If it does not work for the first time, restart the VS Code and try again.

How to add multiple terminals in VS Code?

Can we add multiple different terminals in the VS Code? I am planning to add following three terminal and work with all of those :
Windows Command prompt
PowerShell
Git Bash
I know I need to add the following command in Preferences => Setting
// // 64-bit cmd if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe",
// // 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
// // Git Bash
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
I want to add all of the above three commands in setting.json
And when I click + different terminal should open and I want to work with those terminals without changing the preferences.
Is it possible in VS Code or not?
There is a way to make this happens with these steps by installing an extension:
Find an extension called Shell launcher and install it or you can find it here. Reload VS Code if you want or after you finished all steps.
Go to Files --> Preferences --> Settings and this will open settings.json file and you then insert this (you can edit this to your heart's content):
Code:
"shellLauncher.shells.windows": [
{
"shell": "C:\\Windows\\<sysnative>\\cmd.exe",
"label": "cmd"
},
{
"shell": "C:\\Windows\\<sysnative>\\WindowsPowerShell\\v1.0\\powershell.exe",
"label": "PowerShell"
},
{
"shell": "C:\\Program Files\\Git\\bin\\bash.exe",
"label": "Git bash"
},
{
"shell": "C:\\Windows\\<sysnative>\\bash.exe",
"label": "WSL Bash"
}
]
PS: You can use shellLauncher.shells.linux for Linux or shellLauncher.shells.osx for macOS.
Go to Files --> Preferences --> Keyboard Shortcuts and then find on {} icon on the top right corner to open keybindings.json file. Insert this:
Code:
[
{ "key": "ctrl+alt+`", "command": "shellLauncher.launch" }
]
Update: Type shelllauncher into the search bar. You can then see Shell Launcher: Launch command. Highlight and use any keybinding you like. For example, I picked Ctrl + Alt + (backtick)` for myself.
You can reload your VS Code and click the key combination you have assigned and that will give you the option to choose which integrated terminal you want to use.
For more details, please check the official site: https://marketplace.visualstudio.com/items?itemName=Tyriar.shell-launcher
Enjoy!
Even the question is asked last year and the answer is accepted but still I feel to answer this question as I didn't found any simple, suitable and complete answer while as a development I need multiple terminal handy in a click like below:-
and I don't bother about their path, add another extension for what the VS Code is already capable of or reload VS Shell etc and go to insert and setup the settings files manually.
I found this question is asked many times and almost all landed up manually setup of write some settings etc. or sometimes only opted to get a single type of terminal. The answer of #Pawan is somewhat near but again that solution finally land up to a single terminal, going to command setup for switch terminal and this one will work for git or any other terminal.
If you have tools installed which worked on command line like power-shell and git along with default cmd prompt in windows then the follow the quick three steps to get all terminals at once and switch to anyone with a click.
Open terminal, it should be visible (use ctrl+` or from menu View-> Integrated Terminal )
Open commands search (use Ctrl+Shift+P or from menu View->Command Palette...)
In command box Type "Terminal: Select Default Shell" to select this option from drop down.
As you select this option, then all the available commands which are in path will be listed below like below
Just click any one which you like to add for quick access from command list.
Finally, in the terminal window, just click on + sign next to terminal list as shown below:-
The terminal selected in step 5 will now added after performing step6 to the list without removal of earlier terminal.
Repeat step 3-6 for adding any other terminal to command list.
To work with particular terminal just select the required one in the terminal list of the terminal window.
press ctrl + shift + ` shortcut, or press a cross sign to run new terminal, then type bash if your default mode is powershell or powershell if your default mode is bash. And here you are, your terminal is switched.
For now VS Code support defining only one of available terminals as default at a time and you can not add multiple shell terminals.
If you don't want to install "Shell Launcher" plugin as suggested by #ian0411 then here is a quick way to change/select default terminal.
Press "Ctrl + Shift + P" to open the Command Palette.
Type "Terminal: Select Default Shell" and hit enter. You will be prompted to select Git Bash, PowerShell or Cmd as shown below:
Note: This will change the default shell terminal but there is no harm changing it whenever you need to use another.
BTW, if you need to switch only between Cmd & Powershell then you can type cmd or powershell (in already opened terminal) to switch to desired terminal. But it would not work for Git Bash.
I don't see this in the above, but read all the answers. I think this is the best approach for what is supported right now. I believe, like myself, the OP simply wants to open VSCode, and down yonder on the terminal window, we just want a list of options to open from.
Yes, you can open the command, "Terminal: Select Default Shell" and loop through that to add various types of terminals...
Followed by:
I don't know when this option was added to the dropdown, but look! No need to start by looking up the command. I think this is a lot smoother than any other answer, but it still results in the annoying overwrite of your default term to whatever your last choice was before leaving session.
Here, we can more easily open the Select Default Shell - it should support "Select New Shell". Simple, you'd think. Hopefully someone adds that soon or I have time to contribute.
I'm fairly certain that these are old-style settings.json and won't work as discussed in VS Code 2021. The new style looks like:
...
"terminal.integrated.profiles.windows":
{ "Bash":
{ "path": ["C:\\Programs\\Git\\bin\\bash.exe"],
"icon": "terminal-bash",
},
"Command Prompt":
{ "path": ["${env:windir}\\System32\\cmd.exe"],
"args": [],
"icon": "terminal-cmd"
},
"PowerShell":
{ "path": ["C:\\Programs\\PowerShell\\pwsh.exe"],
"source": "PowerShell",
"args": [],
"icon": "terminal-powershell"
},
},
"terminal.integrated.defaultProfile.windows": "Bash"
...
That will get you access to Command and PowerShells and set bash as the default shell (my preference). If you want to launch a standalone application like git-bash, you will need something else.
As of https://code.visualstudio.com/updates/v1_35 you can now:
select from the pulldown menu directly above the terminal "Select Default Shell"
select the one you like
click the +
done
(the same goes for opening split screen: before this do 1 + 2 and then click the split screen button)
(although overriding the default but does no longer matter if this is your flow)
This can be done by adding a different key at the end. By just changing your example to:
// // 64-bit cmd if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe",
// // 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows2": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
// // Git Bash
"terminal.integrated.shell.windows3": "C:\\Program Files\\Git\\bin\\bash.exe",
Note that the key ...shell.windows is changed to ...shell.windows2 and ...shell.windows3.
Follow-up finding: I've noticed that once restart of the IDE, only the first terminal gets displayed. I had to reopen the settings and save it once to get both the terminals again. Will post if any better solution available.
In the terminal tab, there's a Split Terminal button. Works like a charm
To open the multiple terminal please check the screen shot for the same(on the right bottom of the visual studio code their will be a dropdown and just after it, their is +(plus) icon . On pressing it the new terminal will open.).
The recommended way to automatically open multiple terminal windows is to use the Tasks feature. See Automating launching of terminals in the VS Code documentation.
For WSL Ubuntu on Windows terminal:
File -> preferences -> settings -> click code icon in top right
Enter the following:
{
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe",
"git.enableSmartCommit": true
}