Jan Jongboom
Jan Jongboom

Reputation: 27342

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

Upvotes: 187

Views: 202312

Answers (14)

zloy_zhake
zloy_zhake

Reputation: 2807

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

code -r something.php

Upvotes: 264

bitte
bitte

Reputation: 1

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

code -r file.ext to open in same terminal

Upvotes: -2

Sameh
Sameh

Reputation: 1

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

Upvotes: 0

wzFelix
wzFelix

Reputation: 45

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.

Upvotes: 1

Fermin
Fermin

Reputation: 11

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.

Upvotes: 1

Mou
Mou

Reputation: 521

  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.

Upvotes: 40

praegustator
praegustator

Reputation: 445

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]

Upvotes: 13

VonC
VonC

Reputation: 1328122

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

Upvotes: 1

drkvogel
drkvogel

Reputation: 2601

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

Upvotes: 3

DevDaniels
DevDaniels

Reputation: 61

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

Upvotes: 6

RafaelJan
RafaelJan

Reputation: 3606

just

code file_name

I tried it on Win10 and on Linux (Ubuntu)

Upvotes: 33

hello_earth
hello_earth

Reputation: 1562

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

Upvotes: 0

Craig Poole
Craig Poole

Reputation: 750

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

Upvotes: 5

ellimist
ellimist

Reputation: 626

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.

Upvotes: 24

Related Questions