How to Add Custom Options to the Context Menu on Linux?
By Yoel González — Aug 03, 2024
The context menu, or pop-up menu, is the graphical interface that opens up when pressing right-click at any of the programs that we commonly use. In this article I'm going to show you how you can easily add your own custom options to the Linux file manager.
Step 1: Install Dolphin (file manager).
Out of all the file managers I've tried on Linux, Dolphin is the only one where I've been able to edit the context menu successfully. Simply run:
in order to install it.
Step 2: Set a folder for your custom scripts.
This is where you will store all your bash scripts, if you don't have any yet you can copy my basic example "sayhello" in there. MAKE SURE THAT THE FOLDER'S PATH DOES NOT CONTAIN ANY SPACES!
Step 3: Add the folder's path to your PATH variable.
Copy the full path of the folder you just created, then navigate to the hidden file:
Open ".profile" with your text editor, then add this to the last line:
This is how it looks for me:
Save the file, and you will need to restart your computer so that the changes take effect. After doing that, use the following command to make sure the new path was added correctly to your PATH variable.
Now you can run:
from the terminal and it should prompt you with a welcome message.
Step 4: Add a custom entry to the context menu.
Navigate to:
Notice that if this folder doesn't exist you will need to create it. Then you'll want to put in there the different ".desktop" files that will link your custom scripts with the context menu options. In this case, you can use my example file "sayhello.desktop". After doing this you should be able to see the new option pop up in the context menu when you right-click somewhere around the file manager. If it doesn't work right away, you can restart Dolphin and try again.
Understanding ".desktop" files.
Although desktop entry files can have multiple uses around Linux, this is the basic structure that we'll be using for our context menu purposes:
Type=Service
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=inode/directory;all/allfiles;
Actions=sayhello;
Encoding=UTF-8
[Desktop Action sayhello]
Name=Say Hello
Exec=x-terminal-emulator -e /bin/bash -c "sayhello '%F' -f"
We are using this document to define a button called "Say Hello" that will appear under the "Actions" category in the context menu. The MimeType option specifies the scope in which this button will appear. In this case I want it to appear when right clicking any folder, group of folders, file or group of files. The command that I'm executing here is basically telling the computer to open up a new terminal window, and from there run the bash script "sayhello" which is also taking in any possibly selected files as arguments. Notice that in this example both the action and the script share the same name, but this is not strictly required.
These are some of the different MimeTypes that we can use:
all/allfiles;.............(all files)
image/png;................(png files)
image/webp;...............(webp files)
image/jpeg;...............(jpg or jpeg files)
application/x-deb;........(deb installers)
application/javascript;...(js files)
application/x-subrip;.....(srt files)
text/plain;...............(txt files)
video/mp4;................(mp4 files)
If you want your button to appear from the top level of the context menu, instead of having to go "Actions > Say Hello", add this to [Desktop Entry].
To create a submenu with multiple options under the same category, add this to [Desktop Entry].
And last but not least, you can actually link multiple actions using the same desktop entry file just as seen in the following example:
Type=Service
X-KDE-Submenu=YouTube Download
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=inode/directory;all/allfiles;
Actions=mp3;mp4;srt;
Encoding=UTF-8
[Desktop Action mp3]
Name=MP3
Execx-terminal-emulator -e /bin/bash -c "ytdownload -mp3 '%F'"
[Desktop Action mp4]
Name=MP4
Execx-terminal-emulator -e /bin/bash -c "ytdownload -mp4 '%F'"
[Desktop Action srt]
Name=SRT
Execx-terminal-emulator -e /bin/bash -c "ytdownload -srt '%F'"