Software & AppsOperating SystemLinux

How To Set Default Browser for xdg-open in Ubuntu?

Ubuntu 12

In the world of Ubuntu, xdg-open is a handy command-line tool that allows you to open a file or URL in the user’s preferred application. However, you might want to change the default browser that xdg-open uses. In this article, we’ll guide you through the process of setting a default browser for xdg-open in Ubuntu.

Quick Answer

To set the default browser for xdg-open in Ubuntu, you can use the xdg-mime command. First, check the current default browser using xdg-mime query default x-scheme-handler/http. Then, set a new default browser using xdg-mime default <browser.desktop> x-scheme-handler/http. Additionally, set the BROWSER environment variable in your ~/.bashrc file and check the other default browser settings using update-alternatives --get-selections.

Understanding xdg-open and xdg-mime

Before we dive into the steps, let’s understand what xdg-open and xdg-mime are. xdg-open is a command in Ubuntu that opens a file or URL in the user’s preferred application. On the other hand, xdg-mime is a command-line tool for querying information about file type handling and adding descriptions for new file types.

Checking the Current Default Browser

First, you’ll need to check which browser is currently set as the default for xdg-open. To do this, open a terminal and run the following command:

xdg-mime query default x-scheme-handler/http

This command queries the default application for handling HTTP, which is typically a web browser. The output might be something like firefox.desktop, indicating that Firefox is currently the default browser.

Setting a New Default Browser

If the current default browser is not the one you want, you can set a new one using the xdg-mime command. Replace <browser.desktop> with the desktop file of your desired browser. For instance, if you want to set Chromium as your default browser, you would use:

xdg-mime default chromium-browser.desktop x-scheme-handler/http

This command sets Chromium as the default handler for HTTP.

Setting Default Browser for HTTPS and HTML Files

Similarly, you need to set the default browser for HTTPS:

xdg-mime default <browser.desktop> x-scheme-handler/https

And for HTML files:

xdg-mime default <browser.desktop> text/html

This ensures that both HTTPS links and HTML files are opened with your chosen browser.

Setting the BROWSER Environment Variable

Next, you should set the BROWSER environment variable to your desired browser. This can be done by adding the following line to your ~/.bashrc file:

export BROWSER=<browser>

Replace <browser> with the command or path to your desired browser (e.g., chromium-browser). This step ensures that scripts and programs that rely on the BROWSER environment variable will also use your chosen browser.

Checking Other Default Browser Settings

Finally, you should also check that the following commands and symlinks are set correctly:

sensible-browser
gnome-www-browser
x-www-browser
www-browser

You can use the update-alternatives --get-selections command to see the list of alternatives. If necessary, you can change these settings using the update-alternatives --config <command> command.

Conclusion

By following these steps, you should be able to set the default browser for xdg-open in Ubuntu. This can be helpful in various situations, such as when you’re scripting or when you prefer a different browser than the system default. Remember to replace <browser.desktop> with the actual desktop file of your chosen browser, and <browser> with the command or path to that browser. Happy browsing!

How do I check the current default browser for `xdg-open` in Ubuntu?

To check the current default browser, open a terminal and run the command xdg-mime query default x-scheme-handler/http.

How do I set a new default browser for `xdg-open`?

To set a new default browser, use the command xdg-mime default <browser.desktop> x-scheme-handler/http, replacing <browser.desktop> with the desktop file of your desired browser.

How do I set the default browser for HTTPS links?

To set the default browser for HTTPS links, use the command xdg-mime default <browser.desktop> x-scheme-handler/https, replacing <browser.desktop> with the desktop file of your desired browser.

How do I set the default browser for HTML files?

To set the default browser for HTML files, use the command xdg-mime default <browser.desktop> text/html, replacing <browser.desktop> with the desktop file of your desired browser.

How do I set the `BROWSER` environment variable?

To set the BROWSER environment variable, add the line export BROWSER=<browser> to your ~/.bashrc file, replacing <browser> with the command or path to your desired browser.

How do I check and change other default browser settings?

You can use the command update-alternatives --get-selections to see the list of alternatives for commands like sensible-browser, gnome-www-browser, x-www-browser, and www-browser. If necessary, you can change these settings using the update-alternatives --config <command> command.

Why would I want to change the default browser for `xdg-open`?

Changing the default browser for xdg-open can be useful when you prefer a different browser than the system default or when scripting or programming and need to ensure that specific browsers are used for opening files or URLs.

Leave a Comment

Your email address will not be published. Required fields are marked *