How to open multiples files in one Code instance from command line? - visual-studio-code

If I open a file in Visual Studio Code from the command line or from within File Explorer, each file will open in a separate Code instance. Is there a way to make them all open in the same instance? I.e. add them all to the 'Working Files' list?

Upcoming version will have a new option files.openInNewWindow which you can set to off to always open files into the last active instance. This is then identical to starting VS Code with the -r option but is much nicer to use (e.g. when you open a file from the desktop).
Update for our VS Code 1.0 release:
The setting is available as window.openFilesInNewWindow

You can use the command line option of -r or --reuse-window to assure it opens in the last active VS Code window
You can review the other options here

code Folder1/File1.txt Folder2/File2.java Folder3/File3.css
This command opens three files in one instance of the editor. All files are going to be added to the working files list.
This even works with much more files in the parameter list.

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

Make Visual Studio Code open in clean state

I work in multiple projects spread in multiple folders on macOS.
I usually start working on them by running:
cd ~/workspace/project-a
code .
That always causes a new window to be open with the last files I worked on that project. My next move is to close all tabs, if the editor was split I have to do that as many times as split editors I had.
Is there a setting that would allow me to always start on a clean state?
My settings that I believe are related to this issue are the following:
"window.restoreWindows": "none",
"files.hotExit": "onExitAndWindowClose",
I tried off for files.hotExit but the behaviour remained the same.
Also if possible, where is this information stored (open files for given folders)? Is that a dot file inside the folder or elsewhere inside Visual Studio Code installation?
Make sure you are on the latest VSCode (1.24.1 as on 18-Jun-18)
Then make sure you have below in your settings
"window.restoreWindows": "none",
Make sure there are no JSON errors in your custom settings file. This also could cause the settings to be not loaded at all.
Next try launching the folder using
code -n .
Also $HOME/Library/Application Support/Code/Backups/workspaces.json contains the information of open workspaces
Also refer to below thread
Visual Studio Code always reopens previous file or folder
You could try, in addition of the setting "window.restoreWindows": "none" to start with:
code -n
That would force a new VSCode Window to be opened.
Also if possible, where is this information stored (open files for given folders)?
See issue 3884
# Windows
%APPDATA%\Code\Workspaces
# Mac
$HOME/Library/Application Support/Code
$HOME/Library/Application Support/Code/Workspaces
$HOME/Library/Application Support/Code/Backups/workspace.json
Window > Open Folders In New Window
set it to off

Visual Studio Code edit file

When I try to edit a file in visual studio code, I type the command 'code filename.ps1' and the code used to automatically open as a second tab in the editor.
Now, when I do the same thing, it opens in a new window. I have not changed anything in my settings and I can't find an associated Preference that controls this.
Anyone know exactly the default setting that I need to look at?
According to CLI Options this should work:
code -r filename.ps1
>
-r or --reuse-window
Forces opening a file or folder in the last active window.

re-open vscode without last workspace or last files

I wish set VSCode to be launched clean, no last workspace or last files opened..
is this possible?
I use it as unity3d debugger and annoying me all the times that opens a previous workspace (no workspaces saved)
thanks in advance
Besides my comment above, also see open a new window, v1.22 released today if you prefer to open via a command line.
Controls if a new empty window should open when starting a second
instance without arguments or if the last running instance should get
focus. // - on: open a new empty window // - off: the last active
running instance will get focus // Note that there can still be
cases where this setting is ignored (e.g. when using the -new-window
or -reuse-window command line option).
"window.openWithoutArgumentsInNewWindow": "on"
But the setting
"window.restoreWindows": "none"
should also work for opening in other ways (but seems to require a few restarts - perhaps it has been fixed as of 1.22?).
According to the VSCode command line documentation, you can use -n or --new-window flag to open new session without any old files.
I just checked and it works.
The full command to run VSCode is (on my PC):
"C:\Program Files (x86)\Microsoft VS Code\Code.exe" -n
or just
code -n
Tip: You can change the shortcut for VSCode to include the -n flag so it opens new session always.
None of the other solutions were working for me.
Recently hit this issue where I tried to do a find and replace with 15000 files and vscode crashed while trying to save all those files. I closed the window and re-opened but it kept trying to re-open those 15000 files and would crash again.
I was able to fix this by deleting the corresponding workspace folder (search by folder name) in
/Users/{username}/Library/Application Support/Code/User/workspaceStorage
Then when you reopen that folder nothing is saved or open.

How to open Visual Studio Code from the command line on linux?

I know I can use command "code" to open VS code or file, but I don't know what should I do to make it possible after I install VS code in Ubuntu.Thanks.
Launching from the Command Line
You can launch VS Code from the command line to quickly open a file, folder, or project. Typically, you open VS Code within the context of a folder. We find the best way to do this is to simply type:
code .
Tip: We have instructions for Mac users in our Setup topic that enable you to start VS Code from within a terminal. We add the VS Code executable to the PATH environment variable on Windows and Linux automatically during installation.
Sometimes you will want to open or create a file. If the specified files does not exist, VS Code will create them for you:
code index.html style.css readme.md
Tip: You can have as many file names as you want separated by spaces.
Source: https://code.visualstudio.com/Docs/editor/codebasics
So, there are a couple of solutions for this.
I've linked a video that shows you how to add vscode to $PATH
(which didn't work for me because I couldn't find the "shell:install path" command)
I uninstalled the vscode from my ubuntu and re-installed using sudo snap install --classic code
(This method worked for me)
Tell me which one works for you... and if you have extensions installed to your vscode then i guess you ought to make a backup or something.
Link to the video: https://youtu.be/iP5FKZXtDBs