6

For example, let's say I have defined an alias under my .bash_aliases,

alias gotoheaven="xdg-open /path/to/heaven"

now I wish to create a clickable icon for it on the desktop. [I know make link to create folder shortcut, but this I am asking is just for an example so that I can extend it to create any other type of shortcuts]. Edited: The shortcut is bind to the alias. Something like open Terminal and run gotoheaven. In this way I do not have to re-write alias into the Exec entry.

jaydeepsb
  • 163

4 Answers4

6

To be exact there is a method, i.e. running the alias in an interactive shell;

I.e., create a desktop file with the following Exec= line:

Exec=bash -c 'exec bash -i <<<"gotoheaven"'
  • bash -c '[...]': starts a non-interactive shell (this is required to take advantage of the <<< redirection) and runs [...] in it;
  • exec bash -i <<<"gotoheaven": replaces the non-interactive shell with an interactive shell and redirects gotoheaven to the interactive shell's STDIN, which sources ~/.bashrc and runs the alias.

However since this requires more or less the same effort of copy-pasting the command from the alias into the new desktop file but runs an additional shell, I suggest you to just go for copy-pasting the command from the alias into the new desktop file as outlined in one of the answers to this question.

You seem to be stuck on the fact that the shortcut must run the alias and not an identical command, which just doesn't make sense unless you need to run the command in the environment set by ~/.bashrc (which doesn't seem to be the case). Just create a desktop file that runs the same command.

kos
  • 41,268
5

Aliases belong to shell, they aren't external commands. So it's impossible to create shortcut for alias.

However, it is possible to take the command you reference in alias and create .desktop file which is basically shortcut and after Exec= put the command you wish to run

Here's an example of what it would look like:

[Desktop Entry]
Name=MyAppName
Type=Application
Exec=xdg-open /path/to/heaven
Terminal=false

Icon=/path/to/image.png field is optional. Terminal= part must be placed to indicate whether the output needs to be shown on terminal or not. In your case, I assume it's not necessary since you are using xdg-open to open some file

You may need to make the file executable by right clicking the file , opening Properties, and checking "Execute" under Permissions tab

Or alternatively through chmod +x /path/to/MyAppName.desktop

3

You can't have a shortcut for alias instead you can use this tricky to create a launcher named same as your alias name . In your terminal run the command:

gnome-desktop-item-edit --create-new ~/Desktop

This will launch a GUI Dialog to create a launcher on your Desktop:

enter image description here

Give a name such same as your alias name "gotohell" and in the command insert your command: xdg-open "/path/to/hell"

Now you can find a .desktop file on your desktop named "gotohell"

Maythux
  • 87,123
0

this worked for me

in the command use bash -i -c "your-alias-name"

this also works when used for shortcuts

if you run into problem replace bash with /usr/bin/bash

-i makes it intractable which is needed if alias is from ~/.bashrc file (according to @NotTheDr01ds)

-c makes it accept what comes after as the code for a bash prompt

Usermaxn
  • 247
  • 2
  • 10