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
  • 35,891
  • 1
    That's clever, very clever ! – Sergiy Kolodyazhnyy Dec 18 '15 at 10:33
  • @Serg Not too useful though, that's why I upvoted your answer. It requires more or less the same effort of copy-pasting the command from ~/.bashrc and it runs an additional shell. I wrote this just for the sake of explaining that technically it's possible, but really it shouldn't be the way to go. – kos Dec 18 '15 at 10:52
  • 1
    @Kos, on your last concern, why it is not a good idea to re-write (copy and paste) all the individual (if I have many of them) aliases, is to avoid mistakes, morerover, if I had to edit I will have to edit at both places. Instead it will be very "clean" habit to point to the already defined aliases under .bash_aliases file. Your solution is appreciated and that is exactly what I need. thanks. – jaydeepsb Dec 18 '15 at 20:14
  • @jaydeepsb Right, you have a good point on the editing thing, honestly I didn't think of that. Glad that this solution helped. – kos Dec 18 '15 at 20:31
  • this works for "ssh blah" but with "sshpass blah" there's an extra added "exit" command. I know because I forced terminal to stay open after process finishes in it's options. how do I change the syntax to avoid this? – tatsu Feb 28 '19 at 16:40
  • bash -ic as suggested here is more straightforward. – stevesliva Apr 29 '19 at 19:16
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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
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
  • 84,289
  • @jaydeepsb you are welcome, if you find this helpful please consider to mark it as answer. check http://askubuntu.com/tour – Maythux Dec 18 '15 at 10:22
  • 2
    Just FYI on 14.04 Unity The program 'gnome-desktop-item-edit' is currently not installed. To install it sudo apt-get install gnome-panel. I guess it is alread there on Gnome desktop. – Mark Kirby Dec 18 '15 at 10:58
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
  • 207