9

In the .bashrc file, one can add an alias for a command, say

alias geditm = 'gedit --display=D1'

Now I can run geditm in a terminal and have gedit open in display D1. I am curious if there is a way to define an alias for running commands from the Alt-F2 menu, so that I could Alt+F2, type geditm and have the same result.

I am interested in doing this generally, not just for gedit.

Zanna
  • 69,223
  • 56
  • 216
  • 327
  • According to [this Gitlab issue](//gitlab.gnome.org/GNOME/gnome-shell/-/issues/245) there’s a hidden feature of pressing `Ctrl`+`Enter` instead of just `Enter` for this use case. Try that. – Sebastian Simon May 11 '21 at 10:18

2 Answers2

10

Not really, no. I am not sure about the details but I imagine that the Alt+F2 is simply passing the commands you run to a non-interactive, non-login shell. This type of shell will not have access to aliases, as explained in man bash:

   When bash is started non-interactively, to  run  a  shell  script,  for
   example, it looks for the variable BASH_ENV in the environment, expands
   its value if it appears there, and uses the expanded value as the  name
   of  a  file to read and execute.  Bash behaves as if the following com‐
   mand were executed:
          if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
   but the value of the PATH variable is not used to search for  the  file
   name.

So, you might think that you can just set BASH_ENV to point to a file containing alias definitions. Unfortunately, aliases are not available to non-interactive shells. Again from man bash:

   Aliases are not expanded when the shell is not interactive, unless  the
   expand_aliases  shell option is set using shopt (see the description of
   shopt under SHELL BUILTIN COMMANDS below).

You might think that you could add shopt -s expand_aliases to the file defined by $BASH_ENV but that won't work because that file will be read but in a different shell.

I know this is confusing. Bottom line, you can't make aliases available to the Alt+F2 dialog.


A workaround

So, since you can't do this with aliases, what you can do is do it with scripts:

sudo -H gedit /usr/bin/geditm

That will bring up a new gedit window, add these lines to it and save the file:

#!/bin/bash
gedit --display=D1

Make the script executable:

sudo chmod a+x /usr/bin/geditm

Now, you can hit Alt+F2, write geditm and that script will be launched which in turn launches gedit with the desired options.

terdon
  • 98,183
  • 15
  • 197
  • 293
  • So, he would need to create this script for every possible program he would want to run? I must be confused, because that doesn't really seem to be feasible unless there are only a very few programs he runs. Or maybe you're confused by his question. Also, I don't think you need to make a script executable if you start with `#!/bin/bash` – Marty Fried Apr 13 '14 at 16:05
  • 1
    @MartyFried no, only for those programs he would want to alias. Just like he would if he were using aliases, you would need a separate line for each desired alias. Programs in the `PATH` are already available to the Alt+F2 dialog, you can just run them by name. If you want to run aliases however, passing certain options to these programs, things are different as I explain. – terdon Apr 13 '14 at 16:09
  • @MartyFried also, of course you need to make it executable. The shebang (the `#!/bin/bash`) is just to specify which interpreter should run the script, the shell needs the executable bit set to run it though, regardless. If the script is not executable, you can only run it with `bash /path/to/script` and this is not affected by the shebang in any way. – terdon Apr 13 '14 at 16:16
  • Sorry, you're right about the second part. I forgot about not running it from the terminal, which is what I normally would be doing for things similar to this. But I'm not sure about the first part. perhaps I just don't understand. I wrote it up as an answer, because I guess it's too complicated for a comment. – Marty Fried Apr 13 '14 at 16:20
  • After rereading his question a few times, I think your solution is probably the one he desires. As I often do, I answered too quickly. My solution might work, but it is probably not what he wants. It requires more typing, and is probably more general than required. I'd delete it, but there may be some good general information there – Marty Fried Apr 13 '14 at 18:19
  • 1
    @MartyFried no reason to delete it. Someone else could be looking for that approach, it is a perfectly valid solution for your reading of the question. – terdon Apr 13 '14 at 18:21
  • I'd suggest putting scripts like `geditm` in the user's private `~/bin`. This doesn't require root privileges and avoids clashes with system programs. Aliases in `~/.bashrc` (where the OP had them) and `~/.bash_aliases` are per-user so script replacements probably should be too. (Ubuntu's default `~/.profile` prepends `$HOME/bin` to `$PATH` if it exists. In theory--and in some OSes, in practice--graphical sessions needn't source `~/.profile`; but in Ubuntu, with any popular desktop environment, this works.) If `~/bin` didn't exist and was newly created, one must log out and back in, or reboot. – Eliah Kagan Jun 01 '20 at 15:39
  • @EliahKagan I think what I was thinking of at the time is that I cannot be sure that whatever shell Alt+F2 opens will read any of those files. Doing it this way seemed safer, especially since I doubt this is a multi-user system. If you can confirm that putting it in `~/bin` lets it work with Alt+F2, you're quite right and that's the better approach. – terdon Jun 01 '20 at 15:57
1

Note: after rereading your question and thinking about it longer, I think the answer by terdon is probably the one you want, not this one; this one is too general, and takes more typing.

If I understand correctly, it seems like you might want to append --display=D1 to any program you run from Alt-F2; is this correct? After some discussion, probably not, so be sure to see "EDIT2" along with this for a different take.

If so, it seems like you could create a simple script that would live in some folder in your path, like ~/bin, that would simply say

#!/bin/bash
$1 --display=D1

If you name the script "display", then from Alt-F2 you could run display gedit, and it should append "--display=D1".

EDIT: I guess you will need to make it executable, using chmod +x display

I can't really easily test that, but it should work (assuming this is what you want to do.

EDIT2: It seems I didn't fully understand your question, but I think you can still use a similar method. Instead of specifying the flag, you can use substitutions for both the command and the flags. I guess you could simply say

$*

instead of

$1 --display=D1

Then the Alt-F2 command would be "display xxxx yyyy ..."

Marty Fried
  • 18,026
  • 5
  • 51
  • 52
  • 1
    Ah, I see. That's not how I understood the question at all. I _think_ the OP is looking for the equivalent of the `alias` system for the Alt+F2 dialog. Not how to add this specific option to all programs but how to add any option to any program. Note the last parenthesis in the question: "(Disclaimer-- I am interested in doing this generally, not for gedit)". Since the `--display=D1` is a `gedit`-specific flag, I doubt this will work for anything else. By the way, I would also use `"$*"` instead of `$1`, that way `gedit --foo --bar` would be expanded to `gedit --foo --bar --displaye=D1`. – terdon Apr 13 '14 at 16:25
  • Oh, since I never use gedit for anything, I didn't know that was only a gedit flag. I will make a simple change using your suggestions. – Marty Fried Apr 13 '14 at 16:29
  • 1
    I never use `gedit` either, perish the thought, I use [a real editor!](https://www.gnu.org/software/emacs/) :) But the `--display` is not something I'd seen and anyway, `--` flags in general are specific to their program. Some, like `--help` or `--version` are POSIX and will be present in many cases but not all. So, as a general rule, whenever you see `--`, assume it is specific. In any case, the `--` flags are handled by the program they are passed to, not the shell, so they have to be specific to that program. – terdon Apr 13 '14 at 16:34
  • Yeah, I use a real editor too (unnamed, don't want to start a war); interesting about the double hyphens - I never thought of it that way; I only thought of it as used for long parameters vs single letter flags. I'm not really super experienced with shell programming; I did higher level programming mostly, and struggled through shell scripts when needed. – Marty Fried Apr 13 '14 at 16:40
  • @Terdon: Yes, I am curious if there is an alias system for the Alt-F2 dialog. – Jonathan Gallagher Apr 14 '14 at 20:22