Ubuntu – Use aliases in the Alt-F2 dialog


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.

Best Answer

  • 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

    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:

    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.