03 - Alacritty - 终端机

macOS 的预设终端机为 Terminal.app ,对於使用终端频率较少的一般使用者是十分足够的,但对於开发者而言,它依然有许多可以改进的地方,像是执行效率、配置方式等。

为了提升开发时使用终端机时的效率,因此我们需要一个适合开发的终端。

高效的终端机 Alacritty

https://ithelp.ithome.com.tw/upload/images/20210919/20107789FzJ6lZj8Gd.png

Alacritty 是个执行速度极快的跨平台终端机,它专注在终端机本身的功能上,因此像是许多终端机都内置的分页功能, Alacritty 是没有的,取而代之的是建议使用者整合其他专注特定功能的工具来使用(例如分页与视窗功能可以藉由整合 Tmux 达成)。

因为其实作的功能单纯,因此使得它拥有高效率与高扩展的特性,成为合适开发者使用的终端机之一。

安装 Alacritty

使用 Homebrew 安装 Alacritty :

brew install alacritty

接着在「启动台」内按下 Alacritty 启动应用程序。

https://ithelp.ithome.com.tw/upload/images/20210919/20107789tc1k5O2dre.png

配置 Alacritty

Alacritty 的配置依赖一个 Yaml 档,预设并不会产生这个配置档,需要自行去下载。

GitHub 的发布页中可以找到名为 alacritty.yml 的档案,这就是 Alacritty 配置档的内容。请依照安装的版本,下载对应的档案。

https://ithelp.ithome.com.tw/upload/images/20210919/20107789nZwzEODxIH.png

这个配置档可以放於:

  1. $XDG_CONFIG_HOME/alacritty/alacritty.yml
  2. $XDG_CONFIG_HOME/alacritty.yml
  3. $HOME/.config/alacritty/alacritty.yml
  4. $HOME/.alacritty.yml

Alacritty 会依序由第一个往下找各个路径,如果都没有设置,则会使用预设的配置。

在放置好後,打开配置档,会看到有各个设定属性及说明,全部的属性都会是注解的状态,如果要做设定,请将属性反注解,部分属性需重启 Alacritty 才会显现效果,说明上会有提示,可以依照说明来决定是否重启。

举个例子,如果我现在想要把上方列给除去的话,我可以将 window.decorations 值改为 buttonless

window:
  # Window dimensions (changes require restart)
  #
  # Window decorations
  #
  # Values for `decorations`:
  #     - full: Borders and title bar
  #     - none: Neither borders nor title bar
  #
  # Values for `decorations` (macOS only):
  #     - transparent: Title bar, transparent background and title bar buttons
  #     - buttonless: Title bar, transparent background and no title bar buttons
  decorations: buttonless

依照指示 window 相关的属性变更都需要重启 Alacritty ,重启後就可以看到配置生效後的效果了。

https://ithelp.ithome.com.tw/upload/images/20210919/201077897JM3mR4XvK.png

接着介绍几个常用的配置。

视窗配置

window:
  # 动态调整边缘与内容的间距,避免切到内容
  dynamic_padding: true

  # 隐藏视窗上方的 Bar
  decorations: buttonless

  # 开启时的初始模式
  startup_mode: Maximized

字型配置

font:
  normal:
    family: Hack Nerd Font Mono
    style: Regular
  bold:
    family: Hack Nerd Font Mono
    style: Bold
  italic:
    family: Hack Nerd Font Mono
    style: Italic
  bold_italic:
    family: Hack Nerd Font Mono
    style: Bold Italic

  # 字元大小
  size: 16.0

  offset:
    x: 0
    # 行间距
    y: 8

  glyph_offset:
    x: 0
    # 保持 offset 的一半,确保字元会在行的正中间
    y: 4

  use_thin_strokes: true

字型来源是 NERD FONTS ,可以使用 Homebrew 安装

颜色主题

# Colors (One Dark)
colors:
  # Default colors
  primary:
    background: '0x1e2127'
    foreground: '0xabb2bf'

  # Normal colors
  normal:
    black: '0x1e2127'
    red: '0xe06c75'
    green: '0x98c379'
    yellow: '0xd19a66'
    blue: '0x61afef'
    magenta: '0xc678dd'
    cyan: '0x56b6c2'
    white: '0xabb2bf'

  # Bright colors
  bright:
    black: '0x5c6370'
    red: '0xe06c75'
    green: '0x98c379'
    yellow: '0xd19a66'
    blue: '0x61afef'
    magenta: '0xc678dd'
    cyan: '0x56b6c2'
    white: '0xffffff'

颜色主题参考 eendroroy/alacritty-theme 的 One Dark 。

启动时自动执行指令

在启动终端机时,如果有想要自动执行的指令,可以使用 shell 属性来设置。

# 启动时执行的指令
shell:
  program: /bin/zsh
  args:
    - -l
    - -c
    - 'tmux attach || tmux'

上面的设定会在启动 Alacritty 时自动执行 Tmux 。

完整的配置可以参考 peterhpchen/dotfiles

本文重点整理

  • macOS 预设的终端机 Terminal.app 於效能较差,配置不便利。
  • Alacritty 是个跨平台的终端机。
  • Alacritty 专注於终端机本身的功能优化与改良,额外功能需仰赖其他工具。
  • Alacritty 的配置为单个配置档 alacritty.yml
  • 在 GitHub 的发布页中可以找到对应版本的配置档内容,包含属性及说明。
  • 可以利用不同的属性配置来客制视窗字型颜色主题执行指令等。

参考资料


<<:  [Day 5] Reactive Programming - Java 9(Publisher、Subscribers)

>>:  [Lesson4] SharedPreferences

Day12 - 409. Longest Palindrome

今日题目:409. Longest Palindrome Given a string s whic...

[DAY 07] EC2 - 关於储存空间 EFS

回到储存空间部分, 因为有个 EFS也挺重要的 EFS (Elastic File System)...

Day10 - 基础篇总结 ,CI/CD 的功用为何 ?

如何实现一套基础开发流程? 在前面的章节里谈到了 GCP、Docker、Cloud Run 等技术,...

[Day18] Unreal Webcam Fun

[Day18] Unreal Webcam Fun 需要用到的技巧与练习目标 MediaDevice...

[Vue2] 从初学到放弃 Day8-绑定Class&&Style

在这个范例里,是用单一方式去验证active是否为true <div v-bind:class...