Add “Open with VS Code” to the Right‐Click Menu - GitMasterNikanjam/C_WiKi GitHub Wiki

Add “Open with VS Code” to the Right-Click Menu — Windows and Linux

Applies to: Windows 10/11, Ubuntu GNOME (Files/Nautilus), KDE Plasma (Dolphin), and XFCE (Thunar)
Goal: Open files and directories directly in an existing Visual Studio Code installation.
No VS Code reinstall or upgrade is required.

Contents

  1. What the menu entries do
  2. Windows 10 and 11 — PowerShell method
  3. Ubuntu GNOME — Files/Nautilus
  4. KDE Plasma — Dolphin
  5. XFCE — Thunar
  6. Troubleshooting and reference

1. What the menu entries do

Right-click location Result
A file Opens the selected file in VS Code
A folder Opens that folder as a VS Code workspace
Empty space inside a folder Opens the current folder in VS Code, if the file manager supports this action

The setup differs across operating systems and Linux file managers. The GNOME/Nautilus script below provides a Scripts submenu for selected files/folders, not a native top-level item or an empty-background action. Dolphin and Thunar provide their own action mechanisms.

2. Windows 10 and 11 — PowerShell method

2.1 Verify your existing Code.exe

For this example, VS Code was already installed at:

C:\Users\LENOVO\AppData\Local\Programs\Microsoft VS Code\Code.exe

Open PowerShell (administrator rights are not required) and run:

$code = 'C:\Users\LENOVO\AppData\Local\Programs\Microsoft VS Code\Code.exe'
Test-Path -LiteralPath $code

The result must be True. If not, replace $code with the correct path. Typical installations are:

%LOCALAPPDATA%\Programs\Microsoft VS Code\Code.exe
C:\Program Files\Microsoft VS Code\Code.exe

2.2 Add all three context-menu entries

Copy the following entire PowerShell script:

# Existing Visual Studio Code executable
$code = 'C:\Users\LENOVO\AppData\Local\Programs\Microsoft VS Code\Code.exe'

if (-not (Test-Path -LiteralPath $code -PathType Leaf)) {
    throw "VS Code executable not found: $code"
}

# Write only to the current user's registry; no administrator required.
# .NET registry methods avoid the PowerShell -> reg.exe quoting problem.
$entries = @(
    @{ Key = 'Software\Classes\Directory\shell\VSCode';
       Argument = '%1' },
    @{ Key = 'Software\Classes\Directory\Background\shell\VSCode';
       Argument = '%V' },
    @{ Key = 'Software\Classes\*\shell\VSCode';
       Argument = '%1' }
)

foreach ($entry in $entries) {
    $menu = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey($entry.Key)
    $menu.SetValue('', 'Open with VS Code')
    $menu.SetValue('Icon', $code)
    $menu.Close()

    $commandKey = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey(
        ($entry.Key + '\command')
    )
    $command = '"' + $code + '" "' + $entry.Argument + '"'
    $commandKey.SetValue('', $command)
    $commandKey.Close()
}

Write-Host 'VS Code context menu entries created successfully.'

How it works:

  • HKCU (HKEY_CURRENT_USER) affects only your Windows account.
  • Directory\shell handles right-clicking an actual folder.
  • Directory\Background\shell handles right-clicking empty space inside a folder.
  • *\shell handles right-clicking files of any type.
  • %1 is the selected file/folder path; %V is the folder represented by the background menu.
  • The executable and target paths are double-quoted to support spaces.
  • Icon supplies the installed VS Code icon.

2.3 Test

  1. Right-click a folder: choose Open with VS Code.
  2. Open that folder in File Explorer and right-click empty space: choose Open with VS Code.
  3. Right-click a .cpp or .txt file: choose Open with VS Code.
  4. On Windows 11, custom classic-menu entries often appear under Show more options (or Shift+F10) rather than the new compact menu.

If File Explorer does not refresh, restart it from PowerShell:

Stop-Process -Name explorer -Force
Start-Process explorer.exe

This will close and restart Explorer windows; save ongoing Explorer file operations first.

2.4 Inspect the saved commands

$paths = @(
    'Software\Classes\Directory\shell\VSCode\command',
    'Software\Classes\Directory\Background\shell\VSCode\command',
    'Software\Classes\*\shell\VSCode\command'
)

foreach ($path in $paths) {
    $key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($path)
    if ($null -ne $key) {
        "${path}: " + $key.GetValue('')
        $key.Close()
    }
}

Expected values include:

"C:\Users\LENOVO\AppData\Local\Programs\Microsoft VS Code\Code.exe" "%1"
"C:\Users\LENOVO\AppData\Local\Programs\Microsoft VS Code\Code.exe" "%V"

2.5 Remove the menu entries (undo)

This removes only the three VSCode registry keys created by this guide. If another tool also uses those exact keys, inspect them before removal.

$paths = @(
    'Software\Classes\Directory\shell\VSCode',
    'Software\Classes\Directory\Background\shell\VSCode',
    'Software\Classes\*\shell\VSCode'
)

foreach ($path in $paths) {
    [Microsoft.Win32.Registry]::CurrentUser.DeleteSubKeyTree($path, $false)
}

Write-Host 'VS Code right-click entries removed.'

2.6 Why reg add showed ERROR: Invalid syntax

When PowerShell invokes the external reg.exe program, its handling of nested quotes can differ from what the command author intended. A line like this is easy to get wrong:

# Fragile: shown as an example of what NOT to use
reg add "HKCU\Software\Classes\Directory\shell\VSCode\command" /ve /d "`"$code`" `"%1`"" /f

Using [Microsoft.Win32.Registry] above bypasses reg.exe argument parsing and preserves the needed quotes reliably.


3. Ubuntu GNOME — Files/Nautilus

Ubuntu's default GNOME file manager is commonly called Files or Nautilus. Its simple built-in extension mechanism is an executable script in ~/.local/share/nautilus/scripts/.

3.1 Confirm VS Code command works

Open a terminal:

command -v code
code --version

If code is missing, check how VS Code was installed. A standard .deb installation usually provides code on PATH; a Flatpak installation may instead use flatpak run com.visualstudio.code. Do not install or upgrade anything just to create a context menu. Adjust the script's launch command to your existing installation.

3.2 Create a Nautilus script

mkdir -p "$HOME/.local/share/nautilus/scripts"

cat > "$HOME/.local/share/nautilus/scripts/Open with VS Code" <<'EOF_SCRIPT'
#!/usr/bin/env bash
# Nautilus passes locally selected paths as positional arguments.
if (( $# == 0 )); then
    exit 0
fi

# The -- separator prevents filenames beginning with '-' from being
# interpreted as VS Code command-line options.
code --reuse-window -- "$@" &
EOF_SCRIPT

chmod +x "$HOME/.local/share/nautilus/scripts/Open with VS Code"

Compatibility note: VS Code CLI versions may differ in their handling of -- and --reuse-window. If the command fails, test code -- "$HOME/test folder" in a terminal and use code "$@" & if necessary. For Flatpak, substitute flatpak run com.visualstudio.code for code and confirm it can access the selected path.

3.3 Use it

  1. Open Files.
  2. Right-click a selected file or directory.
  3. Choose Scripts → Open with VS Code.

This action appears in the Scripts submenu, not necessarily the top-level right-click menu. The script does not supply a general empty-space/background menu entry. That requires a different Nautilus extension approach.

3.4 Undo

rm -- "$HOME/.local/share/nautilus/scripts/Open with VS Code"

If the Scripts entry does not appear, check the script is executable and restart Files if needed. Avoid running Nautilus itself with sudo.


4. KDE Plasma — Dolphin

Dolphin supports .desktop service menus. On modern KDE Frameworks, user-level service menus live at ~/.local/share/kio/servicemenus/.

4.1 Verify VS Code

command -v code
code --version

4.2 Add a service menu for files and folders

mkdir -p "$HOME/.local/share/kio/servicemenus"

cat > "$HOME/.local/share/kio/servicemenus/open-with-vscode.desktop" <<'EOF_DESKTOP'
[Desktop Entry]
Type=Service
MimeType=all/all;inode/directory;
Actions=openWithVSCode;
X-KDE-Priority=TopLevel

[Desktop Action openWithVSCode]
Name=Open with VS Code
Icon=code
Exec=code %F
EOF_DESKTOP

chmod +x "$HOME/.local/share/kio/servicemenus/open-with-vscode.desktop"

%F passes selected local file paths as separate arguments. Icon=code uses the installed icon-theme name if available; the action still works if the icon is missing. The X-KDE-Priority hint asks for a top-level action but placement can depend on Dolphin/KDE version.

Background menus: Dolphin has distinct context-menu behavior depending on the clicked area and version. The service menu above is intended for selected files/folders; do not assume it appears when clicking empty folder space.

4.3 Older KDE versions

KDE Frameworks versions older than 5.85 may use ~/.local/share/kservices5/ServiceMenus/ and require this extra key in [Desktop Entry]:

ServiceTypes=KonqPopupMenu/Plugin

Prefer the new kio/servicemenus location on supported recent versions.

4.4 Undo

rm -- "$HOME/.local/share/kio/servicemenus/open-with-vscode.desktop"

5. XFCE — Thunar

Thunar includes a graphical Custom Actions editor, so a terminal script is not necessary.

  1. Open Thunar.
  2. Go to Edit → Configure custom actions (menu labels can vary by release).
  3. Click Add (+).
  4. Name: Open with VS Code
  5. Command: code %F
  6. Optionally choose a VS Code icon.
  7. In Appearance Conditions, enable the relevant file types and Directories. Configure the action's visibility for your Thunar version.
  8. Save. Right-click a selected file or directory to test.

If using a Flatpak version, replace the command appropriately (for example flatpak run com.visualstudio.code %F) and verify file access permissions. Thunar custom actions generally target selected items; empty-background availability depends on Thunar's version and action configuration.

Undo: Return to Configure custom actions, select the entry, and remove it.


6. Troubleshooting and reference

Context menu appears but VS Code does not open

  • Windows: Verify Test-Path -LiteralPath $code is True. Ensure the command stored in the registry contains quoted paths.
  • Linux: Test code . from a terminal. Confirm your graphical session sees the same command on its PATH.
  • Linux Flatpak: Try flatpak run com.visualstudio.code . and check sandbox folder access.

Menu is not visible

  • Windows 11: Click Show more options.
  • Nautilus: The menu entry is under Scripts for selected items; scripts must be executable.
  • Dolphin: Verify the correct KDE-specific directory and .desktop executable bit. The action can appear under Actions depending on version.
  • Thunar: Check Appearance Conditions and whether the file/directory type is enabled.

Windows registry changes fail with Access Denied

Confirm the command is writing beneath HKCU\Software\Classes, not HKLM. Managed corporate systems may enforce their own policies.

Safety and reversibility

  • The Windows script changes only your current user's Explorer context-menu registry entries.
  • No administrator account, VS Code reinstall, or operating-system modification is required.
  • Scripts are shown in full above and include matching undo steps.
  • Do not run shell scripts downloaded from unknown websites without reading them.

Authoritative documentation


Quick recommendation: On Windows, use the PowerShell registry method in §2. On Ubuntu GNOME, use the Nautilus Scripts method in §3. On KDE Plasma, use Dolphin service menus in §4. On XFCE, use Thunar's built-in Custom Actions editor in §5.