如何打开 VSCode 中的文件夹通过终端在一个新鲜的状态?

VSCode 似乎总是在其上一个 UI 状态下打开一个文件夹。

我正在寻找类似 Sublime 的 remember_open_files: false,或者换句话说,我希望 VSCode 以一个干净的 UI 状态打开,而不管上次打开文件夹时 UI 处于什么状态。

现在的情况是:

cd my-project-folder/
code .
# VSCode opens folder with saved UI state

我想要的:

cd my-project-folder/
code .
# VSCode opens folder with fresh UI state
176620 次浏览

I checked through all of the settings available in the VSCode preferences for me, and I don't think there is a way to do this.

There are some settings related to what VSCode window instance that folders will open into, but nothing that seems similar to Sublime Text's remember_open_files setting.

The closest thing I found was running code --user-data-dir . (feel free to replace . with some other directory so you don't pollute your current working directory) from the terminal to specify that VSCode shouldn't remember ANY previous settings, but this seems like overkill for what you're trying to accomplish (as VSCode will literally run as if it's the first time it's being run after a fresh install).

EDIT: I just discovered a View: Close All Editors command in the command palette (CMD + SHIFT + P). The keyboard shortcut for OSX is CMD + K, CMD + W, and this will close all the files you have opened!

I tried to do it through command line using the command

code -n .

which should have opened VS code in current folder with a new session but it does not seem to work at all. I believe that code . seems to ignore the -n new session option and restores the previous session for the folder. So this feature is probably not implemented in VS code.

(Refer here for the commandline options for VS code.)

$ code . --user-data-dir=.

This will open Visual Studio Code in current working directory. I use Bash with Ubuntu 16.04LTS.

I am using VS Code Version 1.24.1 (As of the time of original posting).

If you want to open a folder through the terminal, you can execute the command:

code -n name_of_your_folder/

or

code -n path_to your_folder/

Same thing goes for a file, and these open VS Code in a new window.

Also, note that you should have VS Code installed.

You're welcome!

Part of the answer on this thread worked for me. Basically, make sure VSC is in the Applications folder. Then open the Command Palette (F1 or ⇧⌘P on Mac) and type shell command to find the Shell Command: Install 'code' command in PATH command.

Restart Visual Studio Code if it's open. In terminal, navigate to the folder you want to open in VSC, and type code .. Hopefully it should work for you.

code . opens VS Code at the current terminal folder

this is the command that works for me on windows

code -r .

From inside VS Code built-in terminal, cd into your project folder/directory and enter command:

code -a .

Note period at end.

This will open your current directory/project folder without opening a new window.

If you are using VS Code [Version: 1.50.0] then open your command prompt and go to your project's directory and just run the command:

code -a .

i.e. [Also can see the photo] [1]: https://i.stack.imgur.com/sMmkH.png

To get the right folder in VS Code v 1.50.1 Terminal I tried a lot of options which didn't work for me. At the end I found very easy solution. I went to File->Open Workspace and found that my Workspace had the wrong folders inside it, which I simply deleted (from Workspace only!). Then I opened the folder I needed in my Workspace, opened Terminal->New Terminal, and everything worked perfectly well. Please let me know if this will work for someone else.

If you are using a Mac, you need to first install the VSCode command amongst the shell command list.

Do this:

  1. Open VSCode
  2. press CMD + SHIFT + P
  3. type shell command
  4. select Install code command in path
  5. navigate to any project from the terminal and type code .

This command works to open a specific folder in VS code using terminal

code -r Documents/VS/C++/

here -r switch is used to open a file or a folder in an already opened VS code window and then you specify the path of the file or folder you want to open if you want to open it in a new window use -n switch instead of -r

If you want to open folder with vscode, you just go to folder ( you can user terminal or file explorer) with terminal, and do "code ."

This works for me with VSCode on Linux:

cd path/to/project
codium .

I have the same problem on Mac.

I solved it in the following steps:

  1. I opened the "Command Pallete" on the VSCode. This can be done by CMD + SHIFT + P
  2. Type "shell"
  3. Click on 'Install code command in path'
  4. Give an administrator password
  5. You will get a message that it has been installed
  6. Now run "code ."
    NB: Make sure you already cd into the folder you want to open before you run code .

I tried every mentioned answers, what's work from me is this:-

I created a shell script to open folders.

$ sudo nano  /usr/local/sbin/code2


/usr/bin/code-oss -n --user-data-dir '/home/myusername/.config/Code - OSS2/'  -a $@


$ sudo chmod +x  /usr/local/sbin/code2

You can remove --user-data-dir '/home/myusername/.config/Code - OSS2/' from the script if you want to use default code-oss config folder.

When I want to open a folder, I use the command like this :-

$ code2 .

I add this command code2 to open with option thunar to open folders directly from files manger.

I did it a simpler way just by three steps. I am currently in a project folder and want to open another folder in vs code using the cli or terminal. What I first did is navigated in the folder which I wanna open in vs code inside the terminal. Once I m inside that particular directory or folder I simply typed the command :

start code .

This will open that directory or folder in a new vs code window.

The complete process is :

  1. open the terminal is vs code

  2. navigate to the folder u want to open

  3. once u r inside that particular folder type the command :

    start code .

Go to the directory in the command pallet on your computer the navigate to the the specific folder using cd the type code . and that will open the folder and the files in it inside vs code. works like a charm.

If Visual Studio Code is installed using flatpak then a bash alias can help launch the application from the terminal.

alias code="flatpak run com.visualstudio.code"