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

Visual Studio-Code

Visual Studio-Code Problem Overview


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

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

You can use -r or --reuse-window command line option.

code -r something.php

Solution 2 - Visual Studio-Code

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.

Solution 3 - Visual Studio-Code

just

code file_name

I tried it on Win10 and on Linux (Ubuntu)

Solution 4 - Visual Studio-Code

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]

Solution 5 - Visual Studio-Code

I use code -r . to open the current directory in the main window.

Solution 6 - Visual Studio-Code

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

Solution 7 - Visual Studio-Code

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

Solution 8 - Visual Studio-Code

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

Solution 9 - Visual Studio-Code

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"

cwd -- open link

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJan JongboomView Question on Stackoverflow
Solution 1 - Visual Studio-Codezloy_zhakeView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeellimistView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeRafaelJanView Answer on Stackoverflow
Solution 4 - Visual Studio-CodepraegustatorView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeCraig PooleView Answer on Stackoverflow
Solution 6 - Visual Studio-CodedrkvogelView Answer on Stackoverflow
Solution 7 - Visual Studio-Codehello_earthView Answer on Stackoverflow
Solution 8 - Visual Studio-CodeCodeHunterView Answer on Stackoverflow
Solution 9 - Visual Studio-CodeVonCView Answer on Stackoverflow