How to prevent vscode from the default application when opening a directory in Linux

How to prevent vscode from the default application when opening a directory in Linux

Problem

Sometimes, the OS open a new VSCode instance when you want to open a directory (via xdg-open) with the default file manager (such as nautilus).

Many users have claimed about this issue: #41037, #80499, #44344, archlinux, ...

Background

The OS chooses an application when opening a file (directory is a special file type, called inode/directory, in this case) is called "Default applications".

If VSCode is chosen to be the default application for inode/directory, whenever you want to open a directory from the command line (xdg-open directory-path), VSCode will be used.

The OS determines the default application by reading the configuration from several files:

  • $HOME/.config/mimeapps.list
  • /usr/share/applications/mimeinfo.cache
  • $HOME/.local/share/applications/mimeinfo.cache

To check the default application for a specific file type

Such as checking the default application for opening a directory

Command 1:

xdg-mime query default "inode/directory"

returns

org.gnome.Nautilus.desktop

Command 2:

gio mime inode/directory

returns

Default application for “inode/directory”: org.gnome.Nautilus.desktop
Registered applications:
	org.gnome.Nautilus.desktop
	code.desktop
Recommended applications:
	org.gnome.Nautilus.desktop
	code.desktop

Why VSCode is chosen to be the default directory opener

VSCode IDE has the ability to open a directory, which is defined in code.desktop.

[Desktop Entry]
Name=@@NAME_LONG@@
Comment=Code Editing. Redefined.
GenericName=Text Editor
Exec=@@EXEC@@ --unity-launch %F
Icon=@@ICON@@
Type=Application
StartupNotify=false
StartupWMClass=@@NAME_SHORT@@
Categories=TextEditor;Development;IDE;
MimeType=text/plain;inode/directory;application/x-@@NAME@@-workspace;
Actions=new-empty-window;
Keywords=vscode;

[Desktop Action new-empty-window]
Name=New Empty Window
Exec=@@EXEC@@ --new-window %F
Icon=@@ICON@@
Notice the line MimeType=text/plain;inode/directory;application/x-@@NAME@@-workspace;

inode/directory entry in the assigned value of MimeType specify for the ability to open a directory of VSCode. When being installed, this file is stored at /usr/share/applications/code.desktop.

When installing/updating VSCode, the OS mistakenly re-organize the  mimetype file at /usr/share/applications/mimeinfo.cache, and the OS resets the default directory ( inode/directory) opener to be VSCode.

Phenomenons with IDEA editors

The "Open In" menu in any IDEA editor is changed from "Files" to "Visual Studio Code".

Screenshots after pressing ctrl + shift + @.

After installing VS Code
Expected

Screenshots of the right-click menu:

After installing VS Code
Expected

Solution

Long-term solution

This solution prevents the restoration of VSCode being the default directory opener, in the future VSCode update.

Step 1: find out the default entry of inode/directory in /usr/share/applications/mimeinfo.cache.

cat /usr/share/applications/mimeinfo.cache | grep inode/directory

Result:

inode/directory=code.desktop;org.gnome.Nautilus.desktop;

Step 2: override the default with a local configuration.

Edit, or create if the file does not exist, ~/.config/mimeapps.list.

[Default Applications]
inode/directory=org.gnome.Nautilus.desktop;code.desktop;

This method is described by gnome here.

Short-term solution

You can directly and manually edit the configuration file which causes the issue /usr/share/applications/mimeinfo.cache

Before edit:

inode/directory=code.desktop;org.gnome.Nautilus.desktop;

After edit:

inode/directory=org.gnome.Nautilus.desktop;code.desktop;

Check

Check the result with gio or xdg-mine described previously in this post (link).

Before

# xdg-mime query default "inode/directory"
code.desktop
# gio mime inode/directory
Default application for “inode/directory”: code.desktop
Registered applications:
	code.desktop
	org.gnome.Nautilus.desktop
Recommended applications:
	code.desktop
	org.gnome.Nautilus.desktop

After

# xdg-mime query default "inode/directory"
org.gnome.Nautilus.desktop
# gio mime inode/directory
Default application for “inode/directory”: org.gnome.Nautilus.desktop
Registered applications:
	code.desktop
	org.gnome.Nautilus.desktop
Recommended applications:
	code.desktop
	org.gnome.Nautilus.desktop
Buy Me A Coffee