Is there a way to launch Visual Studio Code and force it to open in a Remote Container? - visual-studio-code

I regularly open Visual Studio Code via code . and then have to click the Re-open in Container button.
Am I missing any shortcut to force it to do this from the command line?

You can open devcontainer directly with devcontainer CLI.
Install devcontainer CLI by running
Remote-Containers: Install devcontainer CLI from VS Code. Then just run:
devcontainer open
in the same directory where you'd run code . and you will skip the Re-open step.

There is a way to do this GUIcally in Windows. I suspect this will also work for SSH / WSL workspaces.
Open your dev container in VSCode as usual.
Right-click on your VSCode taskbar icon; it should show you a jump list with MRU workspaces.
Look for that [Dev Container] list item (mine shows localhost:2375 because that's my docker host). You might like to pin it.
You can open directly into the dev container by clicking on that item.

I have done this with ApiLogicServer - you build the .devcontainer file, plus the Dockerfile

Related

I have to "source ~/.zshrc" everytime I open a new terminal only in vscode

Currently have to manually write source ./zshrc to get my zsh plugins to work within the vscode terminal window, however I dont have this issue if I open a zsh shell outside of vscode. I am using Debian on wsl2.
In terminal I get proper syntax example (due to a plugin)
However in vscode I dont unless i run source ./zshrc
As seen here:

Vscode (code.exe) command line arguments to immediately open a remote workspace folder over SSH

If I want to open a workspace on a regular local folder under C:\ I can just execute the command:
code.exe C:\the-local-workspace-folder
Is there any equivalent command for opening remote SSH workspaces?
I want to be able to use my keyboard launcher to open them, rather than needing to open them the fiddly way in the internal menus inside vscode.
So I need a regular system command to be able to do this.
I've looked through the command line arguments here: https://code.visualstudio.com/docs/editor/command-line - but can't find anything about remote workspaces at all.
I've also tried commands like:
code.exe username#hostname.example.com:/workspace-folder
code.exe username#hostname.example.com/workspace-folder
...but they don't work for this.
This looks like a recent fix, according to the issue tracking here. I've tested with my own settings, and this works:
"C:\Users\myusername\AppData\Local\Programs\Microsoft VS Code\Code.exe" \
--remote ssh-remote+myubuntumachine /home/myusername/myprojectdirectory
The myubuntumachine should be the name given in the Host myubuntumachine in the SSH config file on CTRL+SHIFT+P/Remote SSH: Open Configuration File...
(Actually, on my machine I don't have the machine name, but some sort of hash value, although either works.)
VSCode 1.63 (Nov. 2021) adds the option -n (issue 137529), to make sure a new window is opened for remote CLI:
The following options got added to the remote CLI:
-n to open a new window of the same remote as the current window
-n --remote=wsl+ubuntu to open a new window of a different remote
-n --remote=local to open a new local window
To get started, you need to:
Install an OpenSSH compatible SSH client if one is not already present.
Install Visual Studio Code or Visual Studio Code Insiders.
Install the Remote Development extension pack.
Read more : https://code.visualstudio.com/docs/remote/ssh
Installations extension :
code.exe --install-extension ms-vscode-remote.remote-ssh
Follow the step-by-step tutorial or if you have a simple SSH host setup, connect to it as follows:
Press F1 and run the Remote-SSH: Open SSH Host... command. Enter
your user and host/IP in the following format in the input box that
appears and press enter: user#host-or-ip or
user#domain#host-or-ip If prompted, enter your password (but we suggest setting up key based authentication). After you are connected,
use File > Open Folder to open a folder on the host.
You can press F1 to bring up the Command Palette and type in Remote-SSH for a full list of available commands.
command list
You can change the location by launching VS Code with the --extensions-dir command-line option.
Where are extensions installed?#
Extensions are installed in a per user extensions folder. Depending on your platform, the location is in the following folder:
Windows %USERPROFILE%.vscode\extensions
Linux ~/.vscode/extensions
macOS ~/.vscode/extensions
To run remote ssh and open the folder in command-line :
code.exe --remote ssh-remote+root#server.com <your-directory>

No "open folder in container" or any other command in remote-containers vs code

I'm using a remote machine, and want to run a docker inside that machine (local->remote->docker) and wish to use the Remote development in Containers tools of vs code
Unfortunatlly, I do not see any of the actions after installing the extension.
My view (ctrl+shift+p -> type remote-containers)
While the tutorial has actions I don't even see:
You need to make sure you are not connected to your remote host via Remote-SSH in VSCode. If you are, you will not see "Open Folder in Container..." or the other options.
Try opening up VSCode without connecting using Remote-SSH. You will find that the option is now present.
To set up a remote Docker host, you first need to be able to access your remote host using key based authentication as describe here
You then set
"docker.host":"ssh://your-remote-user#your-remote-machine-fqdn-or-ip-here"
in setting.json.
Once that is setup, you can only attach to running containers. Test this out before proceeding:
Start a container on the remote host
Hit F1 in your vscode and then select "Attach to Running Container..."
You should see the container you started and you should be able to attach to this
Once you are past this point, you will need to create your devcontainer.json file as per the documentation
Make sure you have installed Remote Container extension.
and ssh as mentioned on the comment above.
Once installed, click on Docker icon, under containers, right click on the container you need to work with vcode and select attach vscode.
I had this issue when I opened vscode for the first time in a year after only needing to install it for some random project that wanted me to use "dev containers" for, I don't remember what.
But I don't care about dev containers anymore and I don't remember that project, however, vscode was functionally unusable because it opened in that old project directory and was trying to do... something with containers. I don't know, I don't care, I just wanted to open another folder so a junior programmer could use an IDE on my machine they're familiar with instead of emacs/vim/whatever.
The solution for me was to use the "extensions" tab CTRL+SHIFT+X, disable the "Dev Containers" extension, then use "File -> Open Recent Folder" to select some other directory. This gets around the "can't open in container" bug. If you don't have a history of other opened directories, I think you need to completely reinstall Vscode or something.

How to open folders in VSCode via Terminal in a fresh state?

It appears that VSCode always opens a folder in with the last UI state it had.
I'm looking for something like Sublime's remember_open_files: false, or in other words, I would like VSCode to open up with a clean UI state regardless of what state the UI was in the last time the folder was open.
What's happening now:
cd my-project-folder/
code .
# VSCode opens folder with saved UI state
What I want:
cd my-project-folder/
code .
# VSCode opens folder with fresh UI state
I tried to do it through command line using the command
code -n .
which should have opened VS code in current folder with a new session but it does not seem to work at all. I believe that code . seems to ignore the -n new session option and restores the previous session for the folder. So this feature is probably not implemented in VS code.
(Refer here for the commandline options for VS code.)
this is the command that works for me on windows
code -r .
From inside VS Code built-in terminal, cd into your project folder/directory and enter command:
code -a .
Note period at end.
This will open your current directory/project folder without opening a new window.
Part of the answer on this thread worked for me. Basically, make sure VSC is in the Applications folder. Then open the Command Palette (F1 or ⇧⌘P on Mac) and type shell command to find the Shell Command: Install 'code' command in PATH command.
Restart Visual Studio Code if it's open. In terminal, navigate to the folder you want to open in VSC, and type code .. Hopefully it should work for you.
If you are using a Mac, you need to first install the VSCode command amongst the shell command list.
Do this:
Open VSCode
press CMD + SHIFT + P
type shell command
select Install code command in path
navigate to any project from the terminal and type code .
code . opens VS Code at the current terminal folder
If you are using VS Code [Version: 1.50.0] then open your command prompt and go to your project's directory and just run the command:
code -a .
i.e. [Also can see the photo]
[1]: https://i.stack.imgur.com/sMmkH.png
$ code . --user-data-dir=.
This will open Visual Studio Code in current working directory. I use Bash with Ubuntu 16.04LTS.
I am using VS Code Version 1.24.1 (As of the time of original posting).
If you want to open a folder through the terminal, you can execute the command:
code -n name_of_your_folder/
or
code -n path_to your_folder/
Same thing goes for a file, and these open VS Code in a new window.
Also, note that you should have VS Code installed.
You're welcome!
I checked through all of the settings available in the VSCode preferences for me, and I don't think there is a way to do this.
There are some settings related to what VSCode window instance that folders will open into, but nothing that seems similar to Sublime Text's remember_open_files setting.
The closest thing I found was running code --user-data-dir . (feel free to replace . with some other directory so you don't pollute your current working directory) from the terminal to specify that VSCode shouldn't remember ANY previous settings, but this seems like overkill for what you're trying to accomplish (as VSCode will literally run as if it's the first time it's being run after a fresh install).
EDIT: I just discovered a View: Close All Editors command in the command palette (CMD + SHIFT + P). The keyboard shortcut for OSX is CMD + K, CMD + W, and this will close all the files you have opened!
I have the same problem on Mac.
I solved it in the following steps:
I opened the "Command Pallete" on the VSCode. This can be done by CMD + SHIFT + P
Type "shell"
Click on 'Install code command in path'
Give an administrator password
You will get a message that it has been installed
Now run "code ."
NB: Make sure you already cd into the folder you want to open before you run code .
If you want to open folder with vscode, you just go to folder ( you can user terminal or file explorer) with terminal, and do "code ."
To get the right folder in VS Code v 1.50.1 Terminal I tried a lot of options which didn't work for me. At the end I found very easy solution. I went to File->Open Workspace and found that my Workspace had the wrong folders inside it, which I simply deleted (from Workspace only!). Then I opened the folder I needed in my Workspace, opened Terminal->New Terminal, and everything worked perfectly well. Please let me know if this will work for someone else.
This command works to open a specific folder in VS code using terminal
code -r Documents/VS/C++/
here -r switch is used to open a file or a folder in an already opened VS code window
and then you specify the path of the file or folder you want to open
if you want to open it in a new window use -n switch instead of -r
This works for me with VSCode on Linux:
cd path/to/project
codium .
I tried every mentioned answers, what's work from me is this:-
I created a shell script to open folders.
$ sudo nano /usr/local/sbin/code2
/usr/bin/code-oss -n --user-data-dir '/home/myusername/.config/Code - OSS2/' -a $#
$ sudo chmod +x /usr/local/sbin/code2
You can remove --user-data-dir '/home/myusername/.config/Code - OSS2/' from the script if you want to use default code-oss config folder.
When I want to open a folder, I use the command like this :-
$ code2 .
I add this command code2 to open with option thunar to open folders directly from files manger.
Go to the directory in the command pallet on your computer the navigate to the the specific folder using cd
the type code . and that will open the folder and the files in it inside vs code. works like a charm.
If Visual Studio Code is installed using flatpak then a bash alias can help launch the application from the terminal.
alias code="flatpak run com.visualstudio.code"
I did it a simpler way just by three steps. I am currently in a project folder and want to open another folder in vs code using the cli or terminal. What I first did is navigated in the folder which I wanna open in vs code inside the terminal. Once I m inside that particular directory or folder I simply typed the command :
start code .
This will open that directory or folder in a new vs code window.
The complete process is :
open the terminal is vs code
navigate to the folder u want to open
once u r inside that particular folder type the command :
start code .

How to open a file from the integrated terminal in Visual Studio Code?

Is there a way of opening a file from the terminal in Visual Studio Code that opens in the same vscode instance that runs the terminal? Similar to c9 tool in Cloud9.
I'm aware of the code tool, but when you run code something.php from the integrated terminal it opens a new vscode instance, which is not what I want...
You can use -r or --reuse-window command line option.
code -r something.php
just
code file_name
I tried it on Win10 and on Linux (Ubuntu)
I don't know what operating system you're using, but on MacOS you can just say open filename.ext in the integrated terminal, and it will open a new tab in the same VSCode instance, ready for you to edit.
If you are having command not found: code in macOS, use a full path to it.
/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code -r [filename]
Open Visual Studio Code
Press CMD + SHIFT + P (this opens "Command Palette")
Type shell command
Select “Install code command in path”
Navigate to any project from the terminal, and type code .
If it didn't work, select “Uninstall code command from path” first, then reinstall it again.
I use code -r . to open the current directory in the main window.
You can use the code command from the CLI to open a file, but if you want it to open in the existing window, either use code -r <file> as mentioned in other answers (which does work for me on Ubuntu Linux), or, if -r does not work (under WSL?), make sure window.openFilesInNewWindow is either off or default in settings.json or the in VS Code settings UI, then use code <file>.
Many things can be found in open --help
A work around that worked for me on MacOS is:
open -a 'Visual Studio Code.app' something.php
in the version 1.31.0 that I have installed, on Windows 7, the only way I found to do this is to e.g. change the file associations in system so that .cproj and .cs files are opened by Visual Studio Code by default, and type "filename.cs" in Terminal to open file by that name in the same window... -r option is not working for the first call (opens a new window), but with each subsequent call that same window is correctly reused. ok can't get to open whole directories this way - it's a bit shoddy anyway. probably it would be more convenient to use an outside shell and work with "-r" option
VSCode 1.64 (Jan. 2022) comes with a new command:
Keyboard Navigable Links
Previously, link navigation in the terminal required the use of a mouse.
Now, links can be opened using only the keyboard via the following commands:
Terminal: Open Detected Link... to view all links (web, file, word)
Terminal: Open Last Web Link... ex: https://github.com/microsoft/vscode
Terminal: Open Last File Link... ex: /Users/user/repo/file.txt
Check if the last command Terminal: Open Last File Link... would help in your case.
See also "Terminal shell integration"