iT邦幫忙

2021 iThome 鐵人賽

DAY 8
1
Modern Web

尋覓網站開發的神兵利器系列 第 8

07 - Zim - Zsh 配置框架與它的插件

Zsh 可以讓使用者利用配置客製各種不同的功能,像是命令的自動補全、提示、高亮與縮寫等。但是要自己設定這些需要花費大量的時間,因此找一個好的框架,並且使用大家公認的大眾配置會是個比較省力的方式。

這些框架除了預配置的功能外,都會提供擴充的插件,藉由引入這些插件,我們可以輕鬆地實現各式功能,而不需要自己去做大量的配置。

Zsh 配置框架 Zim

https://ithelp.ithome.com.tw/upload/images/20210923/20107789vKPoPOiuZS.jpg

Zim 是個 Zsh 配置框架,它提供了豐富的預設功能,並且以高效著稱。

除了預設的功能外,使用者還可以使用 Modules 對功能進行擴充,並選擇自己喜愛的 Themes 來使用。

安裝 Zim

使用 Curl 安裝 Zim :

curl -fsSL https://raw.githubusercontent.com/zimfw/install/master/install.zsh | zsh

安裝完成後,重新啟動終端機,就可以開始使用 Zim 所提供的各式功能了。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789RR6pt1E55r.png

Zim 的配置

Zim 的配置藉由一個配置檔 ~/.zimrc 來處理,安裝完成後他會有預設的配置,使用者可以自己決定是否要使用這些配置。

Zim 的 Module 與 Theme 的安裝方式是相同的, 接著以 theme eriner 為例,展示如何配置 Zim 。

安裝 Module

首先,在 ~/.zimrc 中將 eriner 加入到配置:

# Zim theme
zmodule eriner

儲存後,使用指令安裝 Module :

zimfw install

重啟終端後就可以看到新的主題效果了。

https://ithelp.ithome.com.tw/upload/images/20210923/201077894aiyCshETm.png

更新 Modules

使用指令更新所有在 ~/.zimrc 中的 Module :

zimfw update

刪除 Module

先將要刪除的 Module 從 ~/.zimrc 中刪去:

# Zim theme
#zmodule eriner

然後使用指令解安裝 Module :

zimfw uninstall

它會詢問是否要刪除,選擇是後,就會開始進行解安裝的動作。

Zim 的預設功能

Zim 在安裝時會預設配置多個不同的 Module ,其列表如下:

# Start configuration added by Zim install {{{
# -------
# Modules
# -------

# Sets sane Zsh built-in environment options.
zmodule environment
# Provides handy git aliases and functions.
zmodule git
# Applies correct bindkeys for input events.
zmodule input
# Sets a custom terminal title.
zmodule termtitle
# Utility aliases and functions. Adds colour to ls, grep and less.
zmodule utility

#
# Prompt
#
# Exposes to prompts how long the last command took to execute, used by asciiship.
zmodule duration-info
# Exposes git repository status information to prompts, used by asciiship.
zmodule git-info
# A heavily reduced, ASCII-only version of the Spaceship and Starship prompts.
zmodule asciiship

# Additional completion definitions for Zsh.
zmodule zsh-users/zsh-completions
# Enables and configures smart and extensive tab completion.
# completion must be sourced after zsh-users/zsh-completions
zmodule completion
# Fish-like autosuggestions for Zsh.
zmodule zsh-users/zsh-autosuggestions
# Fish-like syntax highlighting for Zsh.
# zsh-users/zsh-syntax-highlighting must be sourced after completion
zmodule zsh-users/zsh-syntax-highlighting
# Fish-like history search (up arrow) for Zsh.
# zsh-users/zsh-history-substring-search must be sourced after zsh-users/zsh-syntax-highlighting
zmodule zsh-users/zsh-history-substring-search
# }}} End configuration added by Zim install

接著說明幾個主要的 Module 所帶來的功能是什麼。

environment

GitHub 連結

此為官方的 Module ,作用為設定合理的 Zsh 的選項

關於 Zsh 的選項可以參考 Moving to zsh, part 3: Shell Options

git

GitHub 連結

此為官方的 Module ,設定 Git 相關的別名與功能。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789r1JlXhctJ1.png

utility

GitHub 連結

此為官方的 Module ,設定 lsgrepless 相關的配置,包含高亮與別名。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789kcREZxNnv5.png

duration-info

GitHub 連結

此為官方的 Module ,紀錄指令執行的時間。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789uZnQZLHuh1.png

git-info

GitHub 連結

此為官方的 Module ,導出 Git 庫相關的資訊。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789SfbIEA1OGJ.png

asciiship

GitHub 連結

此為官方的 Theme ,使用 git-infoduration-info 顯示相關的訊息。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789ajy1lYmaMr.png

completion

GitHub 連結

此為官方的 Module ,提供指令的相關提示,需要搭配 zsh-users/zsh-completions 來提供指令的資訊補完。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789cJ411z0v4j.png

zsh-users/zsh-autosuggestions

GitHub 連結

zsh-autosuggestions 會記憶使用者之前的指令,然後再輸入相似的指令時對使用者做提示,如果確認之後,可以使用右箭頭按鈕完成輸入。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789bWFpuTTOyJ.png

zsh-users/zsh-syntax-highlighting

GitHub 連結

zsh-syntax-highlighting 提供 Zsh 指令高亮的顯示。

https://ithelp.ithome.com.tw/upload/images/20210923/201077891QsxBmlGr1.png

zsh-users/zsh-history-substring-search

GitHub 連結

zsh-history-substring-search 讓使用者可以輸入指令的一部分,然後使用上下方向鍵選取之前與現在所輸入的字串有吻合的指令。

https://ithelp.ithome.com.tw/upload/images/20210923/20107789LdYFpNi7Fs.png

其他的 Modules

Zim 提供了許多的 ModulesThemes ,可以依照需求做安裝,像是 homebrew 提供了 brew 指令的別名。

另外也有許多第三方的套件也可以使用 Zim 來管理,只要是使用 {init.zsh,module_name.{zsh,plugin.zsh,zsh-theme,sh}} 的檔案安裝的套件都可以,如果不是的話,也可以使用 zmodule --source 指定安裝的檔案。

這些第三方套件,像是 Spaceshipalias-tips 等,都可以藉由 awesome-zsh-plugins 尋找。

本文重點整理

  • 使用 Zim 可以快速擴充 Zsh 的功能,並且避免撰寫細部的腳本。
  • Zim 的使用在於兩個關鍵字: zimfwzmodule
  • 指令 zimfw 可以安裝、更新與解安裝於 ~/.zimrc 設定的套件( Modules )。
  • zmodule~/.zimrc 中設定各種想要載入的套件。
  • Zim 預設載入的套件提供了:
    • 別名。
    • 當前資訊( Git 、指令執行時間)。
    • 自動提示與補完。
    • 語法高亮。
    • 歷史指令查找。
  • 除了預設套件, Zim 也提供其他許多的套件,供使用者選擇。
  • 第三方套件也可以藉由 Zim 安裝。

參考資料


上一篇
06 - TPM - Tmux Plugin Manager 與它的插件
下一篇
08 - fd - 快速查找檔案與目錄
系列文
尋覓網站開發的神兵利器40
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言