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

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).

Related

Open file from integrated terminal in VS 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.

Is it possible to open a C# solution in Rider via the command line?

It's possible with VS code via installing a shell command, as seen in this SO question: How to open Visual Studio Code from the command line on OSX?
Is the same thing possible with Jetbrain's Rider?
It is possible to open any file, folder, or solution and project in Rider for editing.
Here is a corresponding help page.
If you use a standalone Rider, you need to create a shell script. Call the action "Tools | Create Command-Line Launcher...". Then you will be able to call rider in a terminal as the shell script is by default created in /usr/local/bin.
In the case of using Toolbox installations, all scripts should be generated using Toolbox. Take a look at Create Command-line Launcher Intellij not found to find out how to set it in Toolbox.
Rider, in my case at least, and I installed it on Linux via snap, comes with a built-in rider command. It does open Rider, however, the issue is that the terminal becomes an output for logs from Rider. Additionally, as soon as I close the terminal, Rider gets closed as well. I guess the Rider process becomes a child process of the shell instance that I used to invoke it.

Visual Studio Code - Where is the terminal history saved?

I noticed that the command history is saved when using Visual Studio Code. Using the arrow up and down keys, I can toggle through previously issued commands in the integrated terminal.
Where is the terminal history saved on the drive? Is it possible to open a file in notepad (or notepad++) and investigate the complete history, delete it?
I'm using Windows 10, and I have concluded that deleting the folders C:\Users\john\.vscode and C:\Users\john\AppData\Roaming\Code will not delete the terminal history for a particular project.
VSCode terminal use external shell. For linux the default shell is bash, I am not so sure about window, but I guess the default shell for window is powershell.
If your terminal shell is indeed powershell, this should help you locate the log file.
On Linux & MacOS, type the following command to display the path of the history file:
echo $HISTFILE
The history file may be different between VSCode and your regular terminal.
So you should type this command within the integrated terminal.
On Ubuntu you can see your history using this command:
vi ~/.bash_history

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"