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


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.
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:
Nautilus scripts are a powerful feature. This post on Fedora Magazine gives a practical example of converting images to a particular format. I also found another good example by Neil Brown here. 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 because there were no contributions for several years. If you are interested in the details, please see this askubuntu.com post, this one and this one. 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. 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. I think I may have stumbled upon it via a Reddit post Is there a replacement for Nautilus-actions / Filemanager-actions?. This is an official GNOME project, you can check out the project's git repository.
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
- 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
- Create the extensions directory (if it doesn't exist):
mkdir -p ~/.local/share/nautilus-python/extensions/
- 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
- 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])
- Make the file executable:
chmod +x ~/.local/share/nautilus-python/extensions/ptyxis_tab_extension.py
- Restart Nautilus:
nautilus -q
- Enjoy!
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!
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.