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

相關文章:

翻譯: