Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In the .bashrc file, one can add

alias geditm = 'gedit --display=D1'

For example, and then I can run "geditm" and have gedit open in display D1. I am curious if there is a way to give a similar kind of alias for running commands from the Alt-F2 menu, so that I could Alt-F2, geditm and have the same result. (Disclaimer-- I am interested in doing this generally, not for gedit).

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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 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.

share|improve this answer
    
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 at 16:05
    
@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 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 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 at 16:20
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 at 18:21

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 ..."

share|improve this answer
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 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 at 16:29
1  
I never use gedit either, perish the thought, I use a real editor! :) 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 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 at 16:40
    
@Terdon: Yes, I am curious if there is an alias system for the Alt-F2 dialog. –  Jonathan Gallagher Apr 14 at 20:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.