Ubuntu设置alacritty为默认终端

对Linux用户来说,终端是高频软件,使用快捷键快速呼出十分必要,Ubuntu Gnome环境默认的快捷键 Ctrl+Alt+T 会启动 gnome-terminal,由于更亲睐alacritty,因此有必要更改默认终端软件。

1. 查看 Gnome 环境默认终端快捷键

打开 Settings,搜索查看快捷键。也许读者不想用下图所示的快捷键,但是默认已经够好记,不用定制,个人推荐使用

Launch terminal

2. 设置默认终端

更换默认终端使用

1sudo update-alternatives --config x-terminal-emulator

运行后发现alacritty不在候选列表中,原因是alacritty 不是 apt install alacritty 安装的,cargo install alacritty 直接安装的不会做相关的关联操作,我猜测的。

 1➜  ~ sudo update-alternatives --config x-terminal-emulator
 2
 3
 4
 5There are 4 choices for the alternative x-terminal-emulator (providing /usr/bin/x-terminal-emulator).
 6
 7  Selection    Path                             Priority   Status
 8------------------------------------------------------------
 9* 0            /usr/bin/terminator               50        auto mode
10  1            /usr/bin/gnome-terminal.wrapper   40        manual mode
11  2            /usr/bin/terminator               50        manual mode
12  3            /usr/bin/tilix.wrapper            30        manual mode
13  4            /usr/bin/xfce4-terminal.wrapper   40        manual mode
14
15Press <enter> to keep the current choice[*], or type selection number:

3. 添加 alacritty 到候选列表

  • 首先查看 alacritty 安装路径
1➜  ~ which alacritty
2/home/mephisto/.cargo/bin/alacritty
  • 添加到候选列表
1sudo update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator /home/mephisto/.cargo/bin/alacritty 50

/home/mephisto/.cargo/bin/alacritty 为 alacritty 命令的路径,50 为优先级

下面为操作步骤,先添加列表,输入数字 1 选中目标,按回车即可,再次查看的时候发现已经生效了,Ctrl+C 退出即可。

 1➜  ~ sudo update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator /home/mephisto/.cargo/bin/alacritty 50
 2➜  ~ sudo update-alternatives --config x-terminal-emulator
 3There are 5 choices for the alternative x-terminal-emulator (providing /usr/bin/x-terminal-emulator).
 4
 5  Selection    Path                                 Priority   Status
 6------------------------------------------------------------
 7* 0            /usr/bin/terminator                   50        auto mode
 8  1            /home/mephisto/.cargo/bin/alacritty   50        manual mode
 9  2            /usr/bin/gnome-terminal.wrapper       40        manual mode
10  3            /usr/bin/terminator                   50        manual mode
11  4            /usr/bin/tilix.wrapper                30        manual mode
12  5            /usr/bin/xfce4-terminal.wrapper       40        manual mode
13
14Press <enter> to keep the current choice[*], or type selection number: 1
15update-alternatives: using /home/mephisto/.cargo/bin/alacritty to provide /usr/bin/x-terminal-emulator (x-terminal-emulator) in manual mode
16➜  ~ sudo update-alternatives --config x-terminal-emulator
17There are 5 choices for the alternative x-terminal-emulator (providing /usr/bin/x-terminal-emulator).
18
19  Selection    Path                                 Priority   Status
20------------------------------------------------------------
21  0            /home/mephisto/.cargo/bin/alacritty   50        auto mode
22* 1            /home/mephisto/.cargo/bin/alacritty   50        manual mode
23  2            /usr/bin/gnome-terminal.wrapper       40        manual mode
24  3            /usr/bin/terminator                   50        manual mode
25  4            /usr/bin/tilix.wrapper                30        manual mode
26  5            /usr/bin/xfce4-terminal.wrapper       40        manual mode
27
28Press <enter> to keep the current choice[*], or type selection number:

4. 确认

Ctrl+Alt+T 快捷键 确认 alacritty 是否能顺利弹出,不出意外系统将会运行一个名叫 x-terminal-emulator 的进程(并不叫alacritty)

1➜  ~ ps -ef |grep term
2mephisto  191336    5110  1 17:01 ?        00:00:01 x-terminal-emulator

好奇宝宝查看 x-terminal-emulator 到底是什么,如下所示:

 1➜  ~ which x-terminal-emulator
 2/usr/bin/x-terminal-emulator
 3➜  ~ file /usr/bin/x-terminal-emulator
 4/usr/bin/x-terminal-emulator: symbolic link to /etc/alternatives/x-terminal-emulator
 5➜  ~ file /etc/alternatives/x-terminal-emulator
 6/etc/alternatives/x-terminal-emulator: symbolic link to /home/mephisto/.cargo/bin/alacritty
 7➜  ~ ls -al /usr/bin/x-terminal-emulator
 8lrwxrwxrwx 1 root root 37 Apr 22  2022 /usr/bin/x-terminal-emulator -> /etc/alternatives/x-terminal-emulator
 9➜  ~ ls -al /etc/alternatives/x-terminal-emulator
10lrwxrwxrwx 1 root root 35 Jan  9 16:32 /etc/alternatives/x-terminal-emulator -> /home/mephisto/.cargo/bin/alacritty

可见 x-terminal-emulator 软链接到 /etc/alternatives/x-terminal-emulator, 而 /etc/alternatives/x-terminal-emulator 最终软链接到了 /home/mephisto/.cargo/bin/alacritty

sudo update-alternatives --config x-terminal-emulator 命令本质上是帮你按照系统规范创建好了软链接,最终指向你的目标终端。update-alternatives 命令就是处理这种事情的(man update-alternatives查看文档): update-alternatives - maintain symbolic links determining default commands

5. 回退删除

万一后悔了,怎么回退删除?

1sudo update-alternatives --remove "x-terminal-emulator" "/home/mephisto/.cargo/bin/alacritty"

和添加类似,能install就能remove,进退自如。

最后修改于: Monday, August 28, 2023
欢迎关注微信公众号,留言交流。

相关文章:

翻译: