在 Alt-F2 对话框中使用别名

Jon*_*her 8 gui shortcut-keys globalmenu alias

.bashrc文件中,可以为命令添加别名,例如

alias geditm = 'gedit --display=D1'
Run Code Online (Sandbox Code Playgroud)

现在我可以geditm在终端中运行并在显示 D1 中打开 gedit。我很好奇是否有办法为 Alt-F2 菜单中的运行命令定义别名,以便我可以Alt+ F2,键入geditm并获得相同的结果。

我有兴趣在一般情况下这样做,而不仅仅是为了 gedit。

ter*_*don 9

不是真的,不是。我不确定细节,但我想Alt+F2只是将您运行的命令传递给非交互式、非登录的 shell。这种类型的 shell 将无法访问别名,如中所述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.
Run Code Online (Sandbox Code Playgroud)

因此,您可能认为您可以设置BASH_ENV指向包含别名定义的文件。不幸的是,非交互式 shell 无法使用别名。再次来自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).
Run Code Online (Sandbox Code Playgroud)

您可能认为您可以添加shopt -s expand_aliases到由 定义的文件,$BASH_ENV但这将不起作用,因为该文件将被读取但在不同的外壳中。

我知道这令人困惑。最重要的是,您不能为Alt+F2对话框提供别名。


解决方法

所以,既然你不能用别名来做这件事,你可以做的是用脚本来做:

sudo -H gedit /usr/bin/geditm
Run Code Online (Sandbox Code Playgroud)

这将打开一个新gedit窗口,将这些行添加到其中并保存文件:

#!/bin/bash
gedit --display=D1
Run Code Online (Sandbox Code Playgroud)

使脚本可执行:

sudo chmod a+x /usr/bin/geditm
Run Code Online (Sandbox Code Playgroud)

现在,您可以点击Alt+ F2、 writegeditm并且该脚本将被启动,然后该脚本会以gedit所需的选项启动。