201

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

3
  • 15
    More challenging: I am using SSH in the integrated terminal and I would like to open a remote file... Commented Feb 25, 2017 at 21:21
  • 2
    With SSH I use readlink -f something.php to print the full path of the file, and then a Ctrl+click on the path open it in a new tab. Commented Apr 9, 2020 at 15:39
  • 1
    Now in 2022 the accepted answer works (at least for me) for remote SSH in integrated terminal. The readlink + Ctrl+click trick works as well. Commented Jan 20, 2022 at 16:39

14 Answers 14

279

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

code -r something.php
Sign up to request clarification or add additional context in comments.

14 Comments

@JanJongboom can't confirm any slowness (Ubuntu MATE, vscodfe 1.18). Might be something on your machine...
Opens a new window for me on Windows 10 :/
The -r or --reuse-window option does not work for me, neither on Win7 nor on Win10.
-r did not for me too. edit: I was using wsl terminal, so watch out for that.
@AliMertCakar Did you get it to open in the SAME window via. Ubuntu WSL? I've this problem :(
|
41
  1. Open Visual Studio Code

  2. Press CMD + SHIFT + P (this opens "Command Palette")

  3. Type shell command

  4. Select “Install code command in path”

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

4 Comments

this was the most helpful answer for me
I think this should be the accepted answer on Mac.
The option: "Shell Command: Install 'code' command in PATH" doesn't come up for me.
Since "cursor" IDE is installed and override "code" command, it can be a real mess to open vscode from the terminal so this "Install code command in path" particularly helped in this case
33

just

code file_name

I tried it on Win10 and on Linux (Ubuntu)

2 Comments

Also works for me on Mac. It seems they simplified it since the times of the accepted answer (2016).
This doesn't work as it opens up a new window
24

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.

8 Comments

Not for me on macOS 10.11... Have associated files with vscode, run open something.php and it opens a new vscode instance...
Interesting. I have macOS 10.12 Sierra and Visual Studio Code 1.8.1. The only custom settings I have in VSCode have nothing to do with the terminal.
Oh, and I use zsh as shell and iTerm2 Build 3.0.13 if that's relevant.
This is actually faster than using the code -r command mentioned in the accepted answer.
For me too, on ubuntu 16.04, the -r option is not necessary. It opens in the current vscode window by default.
|
13

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]

Comments

6

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

Comments

5

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

Comments

3

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

Comments

2

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

5 Comments

Not sure if this is the same issue, but I recently updated vscode (on a mac) and it seems like cmd+click'ing a hovered file link in the terminal now brings up the Go To File (cmd+p) window instead of just opening the file. If I ls'd a directory in the integrated terminal and wanted to open a file I was hovering over in a buffer, do you happen to know what the right command is now? Cmd+click used to work, and it was a lot more convenient that way.
@totalhack I have the same 1.66, but on Windows, and Ctrl+clicking the link does open the browser. For a file in folder of the integrated terminal, Ctrl+click does open the file in VSCode. Check the Terminal: Explorer Kind setting.
I think that might be something different. That is set to "integrated" but the description doesn't seem to be talking about the same thing. Based on this link it sounds like my problem is "if a file or URL cannot be detected, they are still surfaced as low confidence 'workspace search' links". Previously it would just open the file, even if it wasn't something I had opened. Now it always does this workspace search, which is useless for new files.
Perhaps something changed that makes it more glued to this workspace concept, which I don't really use specifically for reasons like this. If I manually add more directories to my workspace I can at least get them to surface in my "workspace search", so it's two clicks to open instead of the previous one cmd+click. Still will run into this problem if I try to open a file outside the workspace, but can code -r file_out_of_workspace as a backup.
@totalhack Strange. Could you detail this issue in a separate question?
1

I would like to add that it's helpful to know in which environment you're trying to run the command. If you're using Windows, the default shell for VSCode's integrated terminal is PowerShell, therefore, you can use the following commands interchangeably to open your files within your workspace as others suggested code -r file_name.rb or code file_name.rb.

Now, if you're using Windows Subsystem for Linux (WSL), you will first need to install the WSL extension in VSCode. Once installed, you can connect to WSL either through "Open a Remote Window" or the "Remote Explorer." This will allow you to switch to your Linux distro within WSL and run the same commands mentioned above to open files within your workspace. Hope this adds some clarity for those of you hacking on Windows.

Comments

1

The version of my VS Code is 1.92.0.

With my Code, just open "File -> Preferences -> Settings" and search "open files". You would see:

Window: Open Files In New Window

Then turn it on or off, you will control your Code.

Comments

0

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

Comments

0

write code on terminal then click on shift it will show you files choose one and enter

Comments

-2

You can do code file.ext to open it in a different terminal and

code -r file.ext to open in same terminal

1 Comment

This is repeating existing answers;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.