Command to open a file

Hello,

I am trying to set up a keyboard shortcut to open a specific txt file. I found that we can do xdg-open <file path and name>, but when I insert the same command in Keyboard Shortcut, it does not run. Would you have any suggestion?

Thanks.

You need to specify a Document Manager to open the file. It may be a text editor like Gedit or Xed...
Or if it is for a coding file you are working on, vscode
So on and so on.

Using xdg-open is good for launching a process. For example, you can create a kb shortcut to launch The Text Editor - where the path would be to the /bin file to launch the text editor. But not to launch a specific file, as that path does not contain the bin for the process launcher.

There are multiple ways to achieve what you want, usually involving a bash script. You create the script that contains all the needed commands, then assign a shortcut to launch the script.

The more specifics you can share, the more easily others can offer suggestions to narrow down a working script or shortcut.

Ah, I see. I will try this out. Specifically, I'm trying to build a keyboard shortcut to open /home/said/Desktop/Scratch.txt

I will use the default Text Editor from Zorin. I will check out how to launch it. At worst, if I can't open the file, I will see how to launch the Text Editor.

Thanks a lot for your suggestion :slight_smile:

No problem.

Open a new text file in your text editor and in it put ( I notice your posted path has a capital S in 'scratch'):

#/bin/bash
gedit /home/said/Desktop/Scratch.txt

Save the file as scratch.sh in a location you want to keep it. For now, I will choose Documents - you can change the "Documents" I am using below for the location that you choose.
Now, right click that new scratch.sh file, move to permissions and check the box for "Allow opening as executable" (Yours may be worded differently in Nautilus. I use Nemo File Manager. But the concept is the same.)

Once you have set it to open as executable, open the Settings for adding a new keyboard shortcut.
The command will be the path to the .sh file:

/home/said/Documents/scratch.sh

Now set the keyboard shortcut you wish to use.

Test.
(I just did a test with a text document on my machine and it worked perfectly on my arbitrary shortcut ctrl+alt+i)

1 Like

This works perfectly! Thanks so much for being so helpful. I really appreciate that.

1 Like