Fixing clickable links in VS Code on KDE 5

TL;DR

KDE_SESSION_VERSION=5 code

The problem

You’re using VS Code on KDE 5 and clicking on a link does not open it in your browser.

xdg-open and KDE_SESSION_VERSION

If you run VS Code with verbose output code --verbose and try clicking on a link, you’ll get the following:

/usr/bin/xdg-open: 611: kfmclient: not found
/usr/bin/xdg-open: 491: test: Illegal number:

On Linux, VS Code uses xdg-open to open URLs. xdg-open uses different commands to open your browser depending on your desktop environment.

Here’s the KDE related code:

if [ -n "${KDE_SESSION_VERSION}" ]; then
  case "${KDE_SESSION_VERSION}" in
    4)
      kde-open "$1"
    ;;
    5)
      kde-open${KDE_SESSION_VERSION} "$1"
    ;;
  esac
else
    kfmclient exec "$1"
    kfmclient_fix_exit_code $?
fi

So the kfmclient: not found error we saw in the VS Code logs was due to the environmental variable KDE_SESSION_VERSION not being defined 🤔

The solution

To make xdg-open use kde-open5 (the KDE 5 command for opening a URL in the default browser), we just need to define the environmental variable KDE_SESSION_VERSION=5.

If you’re using bash as your shell:

echo "export KDE_SESSION_VERSION='5'" >> ~/.bashrc

(For other shells just replace ~/.bashrc with the location of your shell’s config file)

Finally, log out then log back in.