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

[Nautilus](https://apps.gnome.org/en/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.](https://cdn.hashnode.com/res/hashnode/image/upload/v1745683485703/e1109078-052e-4af9-8749-5f37724c8ef5.png align="center")

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](https://open.spotify.com/track/6by0au2JlDXmdE1Xh2jo9U?si=0667337715674c84).

## [Kowalski, options!](https://madagascar.fandom.com/wiki/Options_\(given_by_Kowalski\))

I searched online and found several approaches:

* **Nautilus scripts** are a powerful feature. [This post on Fedora Magazine](https://fedoramagazine.org/integrating-scripts-nautilus/) gives a practical example of converting images to a particular format. I also found another good example by Neil Brown [here](https://neilzone.co.uk/2023/04/automating-actions-in-nautilus-gnomes-file-manager-with-scripts/). However, for my particular use case, I didn't want to have to right-click, then go to "Scripts", then "Open in Console Tab". I wanted a top-level entry, not something hidden in a sub-menu.
    
* **Nautilus actions**, which was deprecated and renamed to **FileManager Actions**, then later [archived](https://gitlab.gnome.org/Archive/filemanager-actions) because [there were no contributions for several years](https://gitlab.gnome.org/Infrastructure/Infrastructure/-/issues/671). If you are interested in the details, please see [this askubuntu.com post](https://askubuntu.com/questions/1138673/is-filemanager-actions-working-with-19-04/), [this one](https://askubuntu.com/questions/1030940/nautilus-actions-in-18-04) and [this one](https://askubuntu.com/questions/1405687/how-to-install-filemanager-actions-in-ubuntu-22-04). Martin Bartlett has written a full-function replacement for this extension that works with the current version of Gnome Files. It's called **Actions For Nautilus** and it's at [https://github.com/bassmanitram/actions-for-nautilus](https://github.com/bassmanitram/actions-for-nautilus). As my use case was a very simple one, I thought this route was going to be too complex for my needs. So I continued digging through the rabbit hole that is the internet!
    
* **Nautilus Python**, an extension for Nautilus that allows further extending it with Python scripts with the help of Nautilus’s [GObject API](https://docs.gtk.org/gobject/). I think I may have stumbled upon it via a Reddit post [Is there a replacement for Nautilus-actions / Filemanager-actions?](https://www.reddit.com/r/gnome/comments/rf0leo/is_there_a_replacement_for_nautilusactions/). This is an official GNOME project, you can check out the [project's git repository](https://gitlab.gnome.org/GNOME/nautilus-python/).
    

As an aside, when I asked [Claude](https://claude.ai/) 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.
    

```plaintext
sudo dnf install nautilus-python
```

2. Create the extensions directory (if it doesn't exist):
    

```plaintext
mkdir -p ~/.local/share/nautilus-python/extensions/
```

3. Create a new file in the extensions directory. In my case, I called it `ptyxis_tab_extension.py`, because [Ptyxis](https://gitlab.gnome.org/chergert/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](https://pagure.io/fedora-workstation/issue/417) about adopting this newer terminal emulator, as [shared in this Reddit thread](https://www.reddit.com/r/Fedora/comments/1ew2rmg/ptyxis_formerly_known_as_prompt_will_replace/).
    

```plaintext
touch ~/.local/share/nautilus-python/extensions/ptyxis_tab_extension.py
```

4. Write your code. You can see some examples [here](https://gitlab.gnome.org/GNOME/nautilus-python/-/tree/master/examples?ref_type=heads). Here's the code for opening a new `ptyxis` terminal tab at the current location:
    

```python
#!/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])
```

5. Make the file executable:
    

```plaintext
chmod +x ~/.local/share/nautilus-python/extensions/ptyxis_tab_extension.py
```

6. Restart Nautilus:
    

```plaintext
nautilus -q
```

7. 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.](https://cdn.hashnode.com/res/hashnode/image/upload/v1745683610111/92159b01-1453-4da1-9e3f-8a9d74c69311.png align="center")

## 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!
