Open the same directory twice - visual-studio-code

On command line, I could open a directory like:
code .
Now, I want two of the identical diretory opened and displayed in another window.
(It's not spliting the editor)
Then, I tried again.
code .
Nothing happened.
There's still one directory opened.
How to achieve such a task?

You can do this with the following ways:
1- Manually open a new window (ctrlshiftn) and go to:
File > Add folder to Workspace. Then just select the folder.
2- Open the command panel (ctrlshiftp) enter dupl and hit enter that will duplicate the current workspace in a new window
*dupl is just a filter for the complete command that is: Workspaces: Duplicate Workspace in New Window thanks to dipnlink comment
Source: https://code.visualstudio.com/docs/editor/multi-root-workspaces
*Done in windows (10), vscode (1.35.1)

In the latest version, it is actually much simpler.
In a window of the project you want to duplicate, open the command panel (Command + Shift + P in Mac or Ctrl + Shift + P in Ubuntu), then type dupl (and select Workspaces: Duplicate As Workspace in New Window), this will duplicate your workspace in a new window. Now you can have 2 windows of the same project at the same time.

click
file > duplicate workspace

Click Add workspace folder... and open the same folder. Works for me, other solution doesn't work, just jump focus to another window.

As far as I know, the accepted answer is not correct as C14L indicates.
It can be done by opening a different folder that includes the folder you're working in. So say we have /project and /project/src and /project/build, you're probably interested in having src open in two instances of VSCode, which you can achieve by opening /project and /project/src.

In addition to accepted answer, here's the trick if you want to open same project in two different windows but showing two different git branches:
Copy the project folder and open that folder!
If you want two windows in two different git branches then this simple trick works. I don't know of any other solutions.

The accepted answer works, but I don't really like the fact that the folder is opened into an unnamed workspace, and upon closing window, we will be prompted to save that workspace. I am expecting more like Sublime Text like behavior.
Luckily there is VSCodium, VSCode's twin/clone without telemetry to Microsoft. So I ended up opening the folder once in either VSCode or VSCodium, then a second time in the other. This so far worked quite well for me.

I wanted to do this for working in two branches side by side and then I realized it was not possible because you cannot have two active branches in exact same git repo folder.
So, if you are looking to do the same, you should instead just make a copy of a folder and switch to another branch there. Options described in other answers will not work for this scenario due to fundamental way git works.

Related

Renaming files doesn't take effect in VScode while using WSL2

I have a very annoying problem in my VScode setup.
I'm using WSL as a terminal to work on my projects and occasionally, mistype the name of one of the folder or file that I'm working with.
For example:
I accidentally created the Mainheader.js file in layout folder (without the capitalized L) therefore, I decided to rename the folder with a capital L.
Now on my React app, any changes made to MainHeader.js file will not be reflected. I did update the related import.
So I decided to delete the entire folder and recreate it with MainHeader.js but this is what I end up with.
The file is there in my folder but when I click on it, I get
"Unable to open Mainheader.js - File not found"
So I try to create it then I get this error:
Unable to create file 'wsl\path]to\MainHeader.js' that already exists when overwrite flag is not set
Has anyone run into this type of behavior in WSL before? It's quite annoying because the only workaround I've found so far is to create a completely different folder with a different name...
Any help would be appreciated. I can't really work like this.
I've been having the same problem for the past 2 days. I presume its a permissions issue, but unfortunately I don't know how to permanently fix it.
I did find this work-around though:
Open a new VS Code window. (I'd recommend closing any VS Code window that had
your project directory open.)
Create a duplicate or copy the contents of the problem file so you don't lose
your code.
Delete the problem file.
Now create the file again using VS Code. Go to File > New Text File. Next paste
in your code.
Now save your new file. Go to File > Save As and save your new file with at the same path + filename + extension that was giving you problems previously. VS Code should allow you to save the new file without any issue.
Now you can open this new VS Code window to your project directory and you should be able to continuing accessing the file that was a problem before.
Basically we just deleted the problem file and then created it again from scratch in a new VS Code window.
Hope this work-around works for you!
EDIT 09/20/2022
Following Baza86's answer here solved the issue for me. Seems like it was a permissions issue of sorts, but if you use the Remote-WSL extension VS code can directly access the linux filesystem.
How to run VScode in sudo mode in WSL2?
You may need add the case option to you options in the wsl config. The default is set to off, however you can set this to off, dir or force.
Open your wsl.conf using sudo with any text editor while running window subsystem Linux. The config file resides in /etc/wsl.conf. This file is used to configure settings per-distribution for Linux distros running on WSL 1 or WSL 2.
My default config looked like this yours may be different:
[automount]
options = "metadata"
add
[automount]
options = "metadata,case=dir"
Here is the official Microsoft docs for Advanced settings configuration in WSL - https://learn.microsoft.com/en-us/windows/wsl/wsl-config

How to change folder that opened by default in VSCode?

I have small problem with VSCode folder, that opened by default.
Problem description: I start new instance of VSCode (trough File->New Window), and then if I choose File->Open Folder it opens dialog with my Windows user folder as starting point (C:\Users\MyUser)
Question: How can I change that folder in settings (if it possible)? So by default it will show as start point for example D:\development\ ?
At the time I write this answer, this is not possible. There are two problems on Windows, and one problem on Mac and Linux:
VS Code does not provide a default path to the file dialog 1. It does remember the last folder that you opened a file in, but that path cannot be used as a default because it is overwritten constantly.
On Windows only, Electron ignores the default path when creating a file dialog if the default path is a directory 2.
An extension also cannot solve this, because extensions are not allowed to modify the File menu 3.
I think the best option at this point is to pin a folder to the Quick Access area in Windows Explorer, as suggested in a comment, or to put an actual shortcut in the user profile folder.
Workspaces and File > Open Recent may also be helpful if you often open the same folders.
Your main problem is that you are unable to open your specific folder in VScode.
To solve that you can simply open the terminal/cmd in that specific window by just typing cmd in your search bar or just by pressing shift+right-click in that folder.
Now your cmd is open and you just have to type "code ." in the cmd and press enter to open the current folder in your VSCode.
In case that code . doesn't work for you then you have to add the Vscode in the environment variables of your windows.
Visual Studio doesn't provide a specific feature to open a specific path. But there is a solution to your problem. You are saying that you want D:\develpment as a default when you open VS Code. You can go to that specific directory or create shortcut to desktop then click right click on that folder and then click on open with code. If you didnot see open with code then reinstall your VS code and check on open with code when you are reinstalling VS Code.
make a shortcut on the desktop for vscode and then modify it and add the folder after the .exe command. This will default open that folder when you double click on it.
Visual Studio Code can be installed in two ways - User setup and System setup. I strongly believe you have User setup installed in your PC. Try re-installing it System-wide. That should probably fix your problem.
For more information: https://code.visualstudio.com/docs/setup/windows#_user-setup-versus-system-setup
PS: A lot more information is needed, you can share a screenshot of the window and elaborate more on it.

Is there a way to open all files in a directory in the current workspace using the VS Code CLI?

I want to open all files under /var/log/my_program/ in my current open workspace. To be clear, I want to open the files from this directory while keeping the files that are already open in my current workspace open as well.
e.g. if I have ~/hello-world.html open, I would like to open /var/log/my_program/error.log and /var/log/my_program/access.log in the workspace as well, having a total of 3 files open at the same time in the same window/workspace.
if I run code /var/log/my_program/* it will open up a new window with all of the files open from that directory. i.e. hello-world.html will be in one window and error.log and access.log will be in a new window
if I run code -r /var/log/my_program/* it will overwrite my current workspace with the files from that directory. i.e. hello-world.html will no longer be open in any window, and error.log and access.log will be open in my original workspace window.
I want to be able to keep the state of my current workspace AND open the files from the /var/log/my_program/ directory. i.e. I would like hello-world.html to stay open in the workspace AND have error.log and access.log open as well.
I would like to add that this is just a simplified example of what I am trying to do. I understand it would be easy enough to just open these two files easily in the command line, but the actual files in the dir I want to open could be several and all with timestamps in their names, so I cant easily open them by name
Is this possible? Thanks for any help!
EDIT:
I am really looking for a solution using the CLI. I am running a command similar to rm /var/log/my_program/*; systemctl restart my_program; code /var/log/my_program/*. This is so the dir doesnt get full of old log files, and I can see the new log files opened after I have restarted the process for my_program
The reasoning for all of this is to make my debugging routine less cumbersome, e.g. without mouse clicks or gui interaction
In VS CODE itself there is a keyboard shortcut that might be helpful:
ctrl + r
Which will open a drop down of folders.
Once you arrow down/up to the desired folder, you have two options:
Click enter to open the new folder instead of the current workspace, or
ctrl + enter to open a new folder in addition to the current open workspace
If I understand what you are trying to do correctly, you want to use the --add argument. You can run code --reuse-window --add <path> to open the folder at <path> in the current workspace. The shortened version would be code -r -a <path>.

Visual Studio Code source control not showing changes

Visual Studio Code source control panel is empty when I click on it. Nothing to expand and nothing to click on.
Things I've tried:
Uninstalled/Reinstalled Git
Uninstall/Reinstalled VS Code
Removed extensions folder
Open your project with cmd.
> cd your-folder-location
> code . -n
It worked for me
Dude, just lost an hour because my SCM in VSCode randomly stopped showing anything today. I restarted everything, tried git init, everything on the forums. Made sure Git built in extension is enabled, mine was already enabled so I was totally lost.
All I had to do was disable and then reenable the built in Git extension. and it fixed it.
Go to Extensions.
Filter by "built in".
Click the gear icon by Git, and click disable.
Then click it again, and click enable.
Here is a screenshot reference
In my case, somehow, the Source Control Repositories option, available under the 3 dots ... on the SOURCE CONTROL tab, was no longer selected.
All I had to do was press the ... and select Source Control Repositories, then select the correct repo, and all the changes were again listed.
I had a similar issue. It seems vs code has two source control extensions. When I clicked View -> SCM it opened an extension with changes displaying.
source control extension 1
source control extension 2
I had this problem 2-3 times for the last 2 years (OS -> Linux Mint). The changes on any file didn't appear to the source control nor have they been marked on the line I've edited. When manually go to "Source control" and click on the refresh button they appear but the lines that I had change didn't light up (there were no visual marking on the files after editing them). This happened when I switched to a different branch while the workspace was open to the 2 monitors at the same time. Or when working on several projects (opened 2-3 or more VS Code instances). The scariest thing was that it didn't work not only for one repository(project) but for all of them. I've read alot on the subject and tried everything that I found and think of. There is some issue with git path mapping or something.
The thing that I tried:
reload VS Code
restarting VS Code
disable all extensions
enable/disable all git related options in (file -> preferences -> settings)
deleting (folders and files) and cloning the repository
updating git
removing and installing git
restarting PC (don't judge me I was desperate)
But the only solution that worked for me was:
open VS Code (if open, don't close it)
go to the directory where you keep your repositories (not from VS Code but from you file explorer).
go one folder above it (if you are in .../{{some folder}}/{{you repos}}, go to ../{{some folder}})
then open you repositories containing folder (/{{you repos}}) by right click -> open with VS Code
wait until everything loads. The Source Control will mark alot of changes, don't worry about it.
then close VS Code (all windows (instances), because it will open a new instance)
after that go to the directory where you keep your repositories again and right click and open with VS code the repository of you choice. Now at this point the Source control will start working properly.
What worked for me was going to my "code" folder where I keep all my repos, right-clicking on the folder containing the repo I want and opening that folder with VS Code.
The VS Code window for this specific repo was closed. I did have another window for a different repo open. As soon as the window opened, the changes showed up in source control and I was able to commit, push and everything else like normal.
I faced this problem when I opened a repo in a directory inside symlink.
My solution: just open this directory in original destination without any symlinks
I had this problem in a repository not as a problem from config but because I had a coverage folder with thousands of files not tracked and it seemed to slow the process of checking that out too much.
So I added that folder to .gitignore and it started working again.
Restating my Vscode And Giving time to load properly Solved my Problem
I had the same problem. What I did was:
Open another folder with File -> Open Folder...
Close the VSC
Open VSC
Open the original folder with File -> Open Folder...
After this I saw that the source control started loading and my changes came back.
Hope this works for you.
I couldn't see any changes in while trying git status. I opened changed files in text editor and they were not changed either. That lead me to conclusion that changes can't be seen by the system (and therefore by git).
The Autosave option was disabled, simply saving the changes helped.
That was my beginning with VSC, in Pycharm never had such problem.
I had this problem, because I was changing files one folder down from where I opened Visual Studio Code.
Solution- open Visual Studio Code without a location, File/Open Folder - open the folder I am directly working out of.
Unstaged changes now show in the direct folder I'm working in. Unstage changes previously auto-staged by Visual Studio Code when working on a nested project directory to see them (open a Terminal and run git reset).
I encountered the same issue, and I fixed that by removing the files.watcherExclude property in settings.json file.
Because the value of files.watcherExclude became { "**/**/*": true } somehow.
// settings.json
// remove or comment next line
// files.watcherExclude: { "**/**/*": true }
For me, the files were in WSL (Windows subsystem for linux) but I was not opening the folder as such.
in the bottom left, click the green >< symbol, then click "reopen folder in WSL"
VSC remote mode image
presto.
Go to View -> Terminal
cd to root folder, and run git status and see if you have any errors
I had a unsafe repository fatat error, as my repo was on a network drive. Did as suggested by git to add an exception and it fixed the issue.
close vscode
moved local Code config folder as backup (~/.config/Code/)
reopen vscode (this will still show problem)
close and reopen vscode (this will show db re-write issue but it will re-create config folder.)
this solved my problem. this reset many of my settings but It can be checked from config backup.
In setting check Git: Autorefresh
I experience this problem when I right click a folder and open it up with VS code. Instead now I start VS code from the start menu and after that I use File -> Open Folder option.
I experienced this issue with VSCode V1.70.1, all I did is just closing VSCode completely and open a new instance and I could find git changes appears simultaneously as expected.
If running into this issue on a Mac, make sure you are running your instance of Visual Studio Code from your Applications folder and not your Downloads folder. I managed to fire up an instance from the wrong folder and this prevented my Source Control from being able to properly load git info and also caused Visual Studio updates to fail. More on the issue can be found here.
Here is another possible solution for Linux users:
In my case, it was only not showing lines changed with the file open. Source control tab was showing fine.
I have a symlink from /var/www to /mnt/{hdd-uuid}/www. When I created my workspace, it was using the path /var/www/project-folder, instead of the full path, and this was giving me the error.
I opened my .code-workpsace file in another text editor and changed the references in the JSON from ../../../../var/www/project-folder to /mnt/{hdd-uuid}/www/project-folder, then reopen VS Code. Close all file tabs open and, when you open again a modified file it will show the lines changed.
It is possible that you need to trust the repo again.
Try opening project or folder which contains git files. Later try opening your wanted folder. This sorted the issue for me.
Double-check git is actually installed on your system. I just did a fresh install of Windows 11 the other day, and although one of the development tools I installed thereafter downloaded and supposedly installed git in Windows, actually, it never installed it. Fail!
I fixed this issue by toggled-on the AutoSave feature in VSCode via File > AutoSave. I noticed that the badge on the github does not show up until the file is actually saved first.
What worked for me was that I was forgetting to save, so just enabled autosave option.
File > Auto Save (check)
Open Visual Studio code -> View -> Appearance -> Show activity bar

Accidentally deleted the Git Shell shortcut

So where is it? I checked the usual suspects, namely Program Files and Program Files (x86), but I didn't see any mention of GitHub there.
I know someone offered an answer but deleted it for whatever reason before I could accept it, so here goes.
You need to create a shortcut to a command, which is actually rather convoluted.
Right-click on the desktop and do New > Shortcut
Browse to any file, but make sure it's not a .exe file!!
Click next through the rest of the wizard and accept the defaults
Now right-click the shortcut, go to Properties > Shortcut tab
Set the Target to: "C:\Documents and Settings\\AppData\Local\GitHub\GitHub.appref-ms" --open-shell
In step 5 above, the quotes are required.
A better solution is to run
%LOCALAPPDATA%\GitHub\GitHub.appref-ms --reinstall-shortcuts
which will recreate the shortcut on the desktop.