我的Linux生活日記 03-建立一個友善的終端機人機介面

2024-02-28 我的Linux生活日記 linux

我的Linux生活日記 03-建立一個友善的終端機人機介面

既然都安裝Lunx而且畢生都得跟Linux 為伍,必須得跟文字介面為伍。但是我記性偏偏不好,大腦記憶體只有256KB少的可憐,所以安裝完Linux第一件事情是優化文字介面。

安裝必要套件

sudo apt -y install curl wget git

下載字型

下載 Meslo Nerd Font 讓 terminal 可以正常顯示icon

sudo mkdir -p /usr/local/share/fonts/custom
sudo wget -P /usr/share/fonts/custom https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf
sudo wget -P /usr/share/fonts/custom https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf
sudo wget -P /usr/share/fonts/custom https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf
sudo fc-cache -v -f

安裝 Tabby terminal

curl -s https://packagecloud.io/install/repositories/eugeny/tabby/script.deb.sh | sudo bash
sudo apt install tabby-terminal

設定字型為 MesloLGS NF

https://ithelp.ithome.com.tw/upload/images/20220916/20128528YOPUN3owII.png

安裝 zsh

  • 安裝指令
# Debian
sudo apt install zsh
# Redhat
sudo dnf install zsh
  • 預設 zsh 為殼層
chsh -s $(/usr/bin/zsh)
sudo  chsh -s /usr/bin/zsh

安裝 autojump

# Debian
sudo apt install autojump
# Redhat
sudo dnf install autojump

wting/autojump

安裝 Oh-my-zsh

下載並安裝 Oh-my-zsh

重新登入後生效

  • 安裝指令
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

下載plugin

tab 自動完成: zsh-completions

  • 安裝指令
git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/zsh-completions
  • 更新指令
git -C ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/zsh-completions pull

自動補全: zsh-autosuggestions

會依據歷史紀錄預覽顯示,效果如圖

https://ithelp.ithome.com.tw/upload/images/20220916/20128528SSqYq0OajM.png

  • 安裝指令
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  • 更新指令
git -C ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/zsh-autosuggestions pull

高亮度語法: zsh-syntax-highlighting

可以輕易識別一行指令中的組成成份,上面是原始,下面是套用過的效果。

https://ithelp.ithome.com.tw/upload/images/20220916/20128528yEb0ZYLmOv.png

https://ithelp.ithome.com.tw/upload/images/20220916/20128528p6SeTDvwYF.png

  • 安裝指令
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  • 更新指令
git -C ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/zsh-syntax-highlighting pull

powerlevel10k

可以漂亮顯示當前目錄的狀態。

https://ithelp.ithome.com.tw/upload/images/20220916/20128528cHagoDenaM.png

  • 安裝指令
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
  • 更新指令
git -C ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k pull

設定 oh-my-zsh

下載完成之後,接下來就是配置設定啦。首先我們先編輯 .zshrc 文件。

  • ~/.zshrc

... 部份省略

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
# ZSH_THEME="robbyrussell"
# 異動內容
ZSH_THEME="powerlevel10k/powerlevel10k"

... 部份省略

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
# 異動內容
plugins=(
        git
        zsh-syntax-highlighting
        zsh-autosuggestions
        zsh-completions
        autojump
)

... 部份省略

alias history="history -i"

# How many commands to store in history
HISTSIZE=10000
SAVEHIST=100000
# History file for zsh
HISTFILE=~/.zsh_history
setopt INC_APPEND_HISTORY
# Share history in every terminal session
setopt SHARE_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_SPACE
setopt EXTENDED_HISTORY
  • 套用設定

配置完 .zshrc 文件之後就是跑 powerlevel10k 設定啦!

source ~/.zshrc

接下來就會自動引導設定

https://ithelp.ithome.com.tw/upload/images/20220916/20128528Zown4LXSt1.png

經過一系列喜好設定這就是我自己喜歡的樣式

https://ithelp.ithome.com.tw/upload/images/20220916/20128528HAtWEax6uw.png

如果之後想改樣powerlevel10k樣式設定執行下列命令就可以了。

p10k configure

有趣的小功能

sudo apt install -y cowsay sl cmatrix sysvbanner figlet
  • ~/.zshrc
alias cowsay=/usr/games/cowsay
alias sl=/usr/games/sl
cowsay 測試
sl
cmatrix -r 
banner Test
figlet Test

我的設定

 Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH

# Path to your Oh My Zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time Oh My Zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
#ZSH_THEME="robbyrussell"
ZSH_THEME="powerlevel10k/powerlevel10k"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
HIST_STAMPS="%F_%T"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
#plugins=(git)
plugins=(
        git
        zsh-syntax-highlighting
        zsh-autosuggestions
        zsh-completions
        autojump
)
alias history="history -i"
HISTFILE=~/.zsh_history
HISTSIZE=100000000
SAVEHIST=100000000

setopt appendhistory
setopt sharehistory
setopt hist_expire_dups_first
setopt hist_ignore_dups
setopt hist_ignore_space
setopt hist_verify
setopt hist_save_no_dups
setopt hist_no_store


source $ZSH/oh-my-zsh.sh
# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='nvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch $(uname -m)"

# Set personal aliases, overriding those provided by Oh My Zsh libs,
# plugins, and themes. Aliases can be placed here, though Oh My Zsh
# users are encouraged to define aliases within a top-level file in
# the $ZSH_CUSTOM folder, with .zsh extension. Examples:
# - $ZSH_CUSTOM/aliases.zsh
# - $ZSH_CUSTOM/macos.zsh
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
#


# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

參考資料

【分享】Oh My Zsh + powerlevel10k 快速打造好看好用的 command line 環境

Powerlevel10k