Adding Your Own Context Menu Entries to GNOME Files (Nautilus)

Victor MitiVictor Miti
5 min read

Nautilus or "Files" is the default file manager that ships with GNOME. The stock context menu entries are sufficient for general purposes. However, there are times when you want the ability to add other entries to enable you perform custom actions. In my case, I wanted the ability to open the current directory in a new tab within my currently active terminal application window. The default available entry, at least in Fedora 41, is "Open in Console", which opens a new terminal application window, instead of adding a tab to an existing one.

Screenshot of GNOME Files (Nautilus) default context menu showing only the standard "Open in Console" option without any custom extensions.

I was constantly getting annoyed by this. Every time this happened, I'd end up with terminal application windows scattered across my desktop, or I'd resort to manually opening a new tab and typing cd /path/to/long/directory/name. Neither solution was efficient. I decided to do something about it.

Kowalski, options!

I searched online and found several approaches:

As an aside, when I asked Claude for a solution to my problem, the initial response I got was to use Nautilus actions. After raising the issue of it being deprecated, Claude then suggested using Nautilus scripts, which I didn't want. It was only then that Claude suggested Nautilus python (among others which included FileManager Actions and creating a custom GNOME extension).

And, we have a winner!

I decided to go with Nautilus Python, as it seemed to be the most practical solution for my use case, and was a perfect fit for me as a Python developer.

Procedure

  1. First, install the Nautilus Python extension. This is for Fedora. If you're using a different distro, please adapt accordingly.
sudo dnf install nautilus-python
  1. Create the extensions directory (if it doesn't exist):
mkdir -p ~/.local/share/nautilus-python/extensions/
  1. Create a new file in the extensions directory. In my case, I called it ptyxis_tab_extension.py, because Ptyxis is the default terminal application in Fedora (as of Fedora 41), replacing the long-standing GNOME Terminal. This change came after a Fedora Workstation discussion about adopting this newer terminal emulator, as shared in this Reddit thread.
touch ~/.local/share/nautilus-python/extensions/ptyxis_tab_extension.py
  1. Write your code. You can see some examples here. Here's the code for opening a new ptyxis terminal tab at the current location:
#!/usr/bin/env python3

import subprocess
from gi.repository import Nautilus, GObject


class PtyxisTabMenuProvider(GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        pass

    def get_file_items(self, files):
        # Only show for folders
        if len(files) != 1 or not files[0].is_directory():
            return []

        item = Nautilus.MenuItem(
            name="PtyxisTabExtension::open_in_tab",
            label="Open in Console Tab",
            tip="Open the folder in a Ptyxis console tab",
        )

        item.connect("activate", self.open_in_ptyxis_tab, files[0])
        return [item]

    # Handle right-click on background (empty space)
    def get_background_items(self, current_folder):
        item = Nautilus.MenuItem(
            name="PtyxisTabExtension::open_current_in_tab",
            label="Open in Console Tab",
            tip="Open this folder in a Ptyxis console tab",
        )

        item.connect("activate", self.open_current_in_ptyxis_tab, current_folder)
        return [item]

    def open_in_ptyxis_tab(self, menu, file):
        filepath = file.get_location().get_path()
        subprocess.Popen(["ptyxis", "--tab", "-d", filepath])

    def open_current_in_ptyxis_tab(self, menu, folder):
        filepath = folder.get_location().get_path()
        subprocess.Popen(["ptyxis", "--tab", "-d", filepath])
  1. Make the file executable:
chmod +x ~/.local/share/nautilus-python/extensions/ptyxis_tab_extension.py
  1. Restart Nautilus:
nautilus -q
  1. Enjoy!

Screenshot of GNOME Files (Nautilus) context menu showing both the standard "Open in Console" option and the custom "Open in Console Tab" option implemented with nautilus-python extension.

Verba Finalia

With very few lines of code, we have an extension that adds the "Open in Console Tab" option in two places:

  • When right-clicking on a folder

  • When right-clicking in the empty space within a directory

When clicked, it runs ptyxis --tab -d /path/to/folder which opens the selected directory in a new tab in your existing Ptyxis window (or open a New window if there isn't any).

How cool is that? No more scattered terminal windows or tedious directory typing! Now I can quickly navigate my file system and open directories in terminal tabs with just a right-click.

The current implementation works well, but there are a few potential enhancements:

  • Positioning the menu option closer to the original "Open in Console" option

  • Adding an icon to match the Ptyxis style

But that's something for another day! For now, my problem is solved and I am happy!

0
Subscribe to my newsletter

Read articles from Victor Miti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Victor Miti
Victor Miti

Building things with Python, Django & Wagtail. Learning something new everyday.