Tuesday, April 30, 2024
10
rated 0 times [  10] [ 0]  / answers: 1 / hits: 3990  / 1 Year ago, fri, april 28, 2023, 11:12:00

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.


More From » shortcut-keys

 Answers
0

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.


[#26057] Saturday, April 29, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brasiplacar

Total Points: 314
Total Questions: 124
Total Answers: 97

Location: Dominican Republic
Member since Wed, Mar 17, 2021
3 Years ago
;