 Today, I want to talk about how you can edit the context menus within your file manager. Let me switch over to my desktop and I'll show you exactly what I mean. I'm going to open up the file manager on my system is PCmanfm and if I go into one of my directories here, this is my GitLab Repose directory, it has all of my GitLab repositories cloned here on the system. But if I right-click on something, whether it be a directory or a file, I get a menu with various things I can do, in this case with this particular directory or with this particular file, you know, it tells me all the various programs I could open this particular log file, which is a plain text file. So you know, I get all the text editors on my system, you know, as possibilities of things I could open that file with it was a MP3, all the music players would probably be listed in that context menu open with, you know, things like rhythm box and dead beef and MPV and VLC, anything that can play music would be what would be in that context menu. Sometimes though, you want to be able to add your own custom actions to these context menus in your file manager and that's what we're going to talk about today. So the first thing I want to do is let's go over to the ArchWiki because anytime you're interested in how to do something, obviously you need to read the documentation, right? Things people need to get in the habit of just going and looking up the documentation. So if I want to do something with PC man FM, for example, I searched for PC man FM here in the ArchWiki and if I scroll down, there is a little subsection here adding custom items to your context menu and they even have an example piece of code here. So you can see that this needs to be a desktop entry file and it needs to go in your home directory slash dot local slash share slash file dash manager slash actions slash the name of the file dot desktop. So let's just copy this code and then let's create this file. So let me open up Emacs for this. I'll make this full screen here. So I'm going to do space period to run the find file command here inside doom Emacs, but really I'm not finding a file. I'm actually just going to create one. So I'm going to do dot local slash share slash file dash manager slash actions slash and then the name of the desktop file we're going to create. What I want to do as an example, the first example is all the directories in my GUI file manager in this case PC man FM. I would like to be able to right click on them and then if I want to open them inside dear ed inside Emacs, I would like to have that option listed in that context menu. So I'm going to call this Emacs dear ed dot desktop and it's going to ask me, do I want to create the file and all the directories as part of that path? Yes. And now let me go ahead and paste the example code that we got from the arch wiki. So now I just need to go in here and edit some values. The main one is the name. This will of course appear in our menu. So the name of this will be open in Emacs dear ed. That will suffice for the description and then this here is another name so you can have multiple names in various languages if you want it to be compatible for languages other than English. But for me, I don't need to do that. Then we need to set an icon. And in this case, pretty much every standard icon set has a Emacs icon. So just Emacs should work for us here. And then the mime type. So we need the file type, right, the mime type of these files. For example, if I wanted this to only appear on audio files, then I would go in here and I would have to get the mime type of all the audio files, which I believe typically are things like audio mpeg, for example, for mp3 or audio flak. I could be a little wrong on this. If you're ever confused about exactly what the mime type or something is, you can actually get that information from a terminal. Let me open a terminal. Clear the screen. Let me show you this. There's a very simple way to do this on Arch. The first thing you need to do, you need to install this package here. You need a sudo pacman-capital s pearl-file-mimeinfo. Install that package. I've already got it installed, so I won't reinstall it. And once you have that package installed on your system, you now have this program available to you, mime type. So if you're ever interested in getting the mime type of a particular file or directory, just run mime type on that particular file or directory. For example, my bash rc, what's its mime type? Its text slash plain, it's just a plain text file. If I want to know what the mime type of a directory is, I'm just going to run mime type on period, which is, of course, alias for this directory, right, the directory we're in. It's inode-directory. And because we only want to open directories in Emacs-dir-ed, that's exactly what I needed here. I need inode-directory for the mime type. And then what we need to do is we need to go ahead and give it a command to execute. So what is this action actually going to do if we choose it? Well, it needs to open this current directory, right, the directory we chose. It needs to open that in Emacs-dir-ed. And I'm going to use the Emacs client. So I use Emacs-client space dash c, so open it in its own Emacs window, not in an existing Emacs window that maybe I'm already working in, dash a for an alternative editor. If the Emacs server has crashed, Emacs client will not be available for us. So an alternative editor you could use is simply Emacs, right, the non-server-client Emacs. And then what I want you to do is after you've launched the Emacs client, I want you to do dash dash eval. So I want you to evaluate this code here. And the code we want to evaluate is really just run the Emacs-program-dir-ed. And we have to pass it an argument, right, a directory to open. And that's going to be this variable here, %f. So you always use %f in these context menus for PC-MAN-FM. %f is simply the file or directory that you clicked on inside the file manager. Now let me write that. And if I did this correctly, now let me launch PC-MAN-FM. I'll once again go to my GitLab repos. And if I want to click the DM scripts directory here, if I right click on it, now I have open an Emacs-dir-ed listed in the context menu. If I click on it, wow, it opens that inside a Emacs buffer, this dir-ed, and this is the actual contents of that DM scripts directory. So it does exactly what it was supposed to do. And as an Emacs user, I could see some other interesting things I could do. For example, I'm going to actually just copy this. So I'm going to do a colon W for right. I'm going to write this to emacs-maggot.desktop. So now when I do find file, I have emacs-maggot.desktop. And this time, we're just going to write something very, very similar anytime I click a directory. Now I'm going to have open an Emacs-maggot, so if it happens to be a Git repository, that's cool. MIME type, once again, should be inode-directory, except this time I want to run the command emacs-client-c-a-emacs-avel and maggot on percentf, I believe is the command. So let's actually try that out, because I could have got that command wrong. I don't use maggot inside emacs that often. But if I right-click on dmenu-distro-tube, open an emacs-maggot. Let's see if that actually works. It does. It opens that Git repository that's locally on my system. It opens that in maggot. Very cool. One interesting thing that I think a lot of people would probably find useful is being able to open a certain file or a certain directory maybe on your system as root with sudo privileges. So once again, I'm going to do a colon w to just copy this file. So I'm going to write and I'm going to do open as root dot desktop. And now let me switch over to that file. And now that I think about it, I think instead of opening a terminal in the current directory as root, I just want to open another instance of pcmanfm as root. So I'm going to say open as root for the name. The icon can just be the pcmanfm icon in this case. It will be directories that will be opening as root. But the executable command here will not be an emacs command. So let me just delete that. And I think the way I want to do this as far as getting sudo privileges is gksudu, which is a graphical box that will appear asking you for a sudo password. You typically use this with graphical applications, such as a graphical file manager. Then I want you to run pcmanfm and then percentf. And I'm hoping that works. I'm not positive that that's going to work. But if it does, there's only one way to find out. I'm going to go into my root file system slash etsy. Now to do anything in slash etsy, I'm going to need root privileges. So let's open as root and we get the gksudu box. Let me give my sudo password. And now it just opened my slash etsy directory in pcmanfm with root privileges. So that would probably be something a lot of people would be interested in having as a custom action in their file manager. Now every file manager is going to do these custom actions, these context menu things a little differently. They're all pretty similar, but they're all a little different. And I can't go over every single file manager out there because there's dozens of them. But another very popular one that I know a lot of you guys are running is Thunar. And Thunar actually makes this pretty easy because Thunar has in the edit menu, configure custom actions. And that's a graphical way of you creating custom actions. So here are some of the custom actions that are already enabled right with Thunar. I didn't create these myself by the way. These are just here as part of Thunar or part of some of the extensions for Thunar that have maybe been installed by Arco Linux, which is what I run. But if I wanted to create my own custom action, I hit the plus symbol here. And for example, if I wanted to, you know, be able to open a directory in EmacsDeerEd, open in EmacsDeerEd would be the name, the description. I'll just copy that for the description. Does it need its own submenu? Well, if I have several custom Emacs actions like open also directories and EmacsMaggot, for example, it might be interesting to have a submenu. So I will create a submenu. And then we need to create the command and the command I'll just paste from earlier, what I used in PCManFM because it uses a similar format. Except the percent F is the path to the first selected file or directory. Yeah, that's the one we want. But there are some other variables you can use with Thunar that are not available with PCManFM. But I think this is the one I actually need. So let me hit OK. There is open in EmacsDeerEd. It didn't ask us about an icon. So let me actually go back, no icon. So let me click on the no icon button. And I would actually have to search for a Emacs icon here, just use the first one there. I noticed that everything I had written before is no longer here. I guess it didn't save it probably because I didn't choose the icon earlier. But let's go ahead and I'll put everything back. Open in EmacsDeerEd. Copy, copy. Let's hit OK. Close that. And now when I right click on a directory, let's make it one of my GitLab repos. If I right click, I should have had that Emacs subfolder here. But may need to restart. You can tell I don't use Thunar that often. There's another tab here. Other than the basics, which is all the stuff you've already seen me fill out, appearance conditions and file pattern range. That could be anything. The it appears if the selection contains directories. Right? So this is all we want this to actually be available in. So that is the reason. And now let's restart Thunar. And now if I go onto a directory and right click on it. Now I have this Emacs sub directory. It says open and Emacs gear ed. So let's see if that actually happens. It does. So that is how you add that in Thunar. You go into edit, configure custom actions. And just make sure that you don't do like me and fill out the first page. You know, the basic tab here, you also have to go into the appearance conditions because by default, everything is ticked off. Meaning that new context menu is never going to appear on any files or directories until you make the appropriate selections. So that is for Thunar. I'm sure that's very similar to the way Nautilus does these things and the various other file managers out there. Nemo and Kaja and Dolphin. And the sky's the limit to what you can do with this. And that's one of the great things about Linux, especially a lot of the software we use for an open source software, is you can configure these things, you can extend these things to do whatever is appropriate for your use case. And before I go, I need to thank a few special people. And of course, I'm talking about Gabe James, Maxim, Matt Mitchell, Paul, Role, Wes, Armoredragon, Bash Potato, Chuck Commander, Angry George, Lee Methos, Nate Erion, Paul, Pete Sgtr, Polytech Realities for Less Red Profit, Roland Tools, Devler, and Willie. These guys, they're my high steered patrons over on Patreon without these guys. This episode about context menus inside a file manager, it wouldn't have been possible. The show's also brought to you by each and every one of these fine ladies and gentlemen. All these names you're seeing on the screen right now, these are all my supporters over on Patreon. I don't have any corporate sponsors, I'm sponsored by you guys, the community. If you like my work and want to see more videos about Linux and free and open source software, subscribe to DistroTube over on Patreon. Peace.