Open file from integrated terminal in VS Code - visual-studio-code

I have VS Code setup with WSL on my Windows 10 machine. I am trying to find the command I can use to open an existing file from the integrated terminal in the current VS Code window.
I tried code filename, which launches a new VS Code window. I tried with code -r filename, but it also launches a new window.
Is there a way to quickly open a file when I'm focussed on the terminal?

As of version 1.43.1, this appears to now work as expected, without additional add ons.

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:

Is there a way to get the currently open file in VS Code via command line?

I'd like to get the full path of the currently open (last active) file in Visual Studio Code from command line to use in command-line scripts. There's a solution for referring to it in the integrated terminal. However, I'm using a separate terminal (Terminal.app on macOS).

VS Code with WSL now opens the browser.sh file instead of launching the browser

The problem: when running a React project (i.e. npm run start) with WSL from the terminal in VS Code, the file "browser.sh" is opened in a new window. The browser is no longer launched.
This is a new behavior with version 1.52. The release notes indicate that they now add the system variable "BROWSER" and use it to launch the browser. In my case it is set to /home/dan/.vscode-server/bin/ea3859d4ba2f3e577a159bc91e3074c5d85c0523/bin/helpers/browser.sh
The browser.sh file is a shell script created by VS Code. It exists and has executable permissions (-rwxr-xr-x).
The workaround: Type unset BROWSER every time a terminal is opened inside VS Code.
The desired solution: Convince VS Code to run the shell script or to launch the default browser directly.

Open a file in VS code using `open -r`

I was trying to use the command code -r file_name.py in the integrated terminal in VS code to open the file in the existing window that I'd already opened. However, another new VS code window was still generated. What's weird is that originally, I could open a file by simply using code file_name.py without using the -r flag. Is there any way to solve the problem? I want the file to be opened in a new tab in the same window instead of in a new window. Any experience shared will be appreciated!
When using VS code the correct command to open a file in an existing code window is indeed code -r .\file_name.py, if you are doing so from a terminal within VSCode it will do so within that instance of VSCode.
My instance of VSCode defaults to using CMD on Windows 10 for this command. What are you using to run this command?

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"