快轉到主要內容

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

·2031 字·5 分鐘·
PolloChang
作者
PolloChang
我是一隻雞

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

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

安裝必要套件
#

1
sudo apt -y install curl wget git

下載字型
#

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

1
2
3
4
5
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
#

1
2
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
#

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

安裝 autojump
#

1
2
3
4
# Debian
sudo apt install autojump
# Redhat
sudo dnf install autojump

wting/autojump

安裝 Oh-my-zsh
#

下載並安裝 Oh-my-zsh
#

重新登入後生效

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

下載plugin
#

tab 自動完成: zsh-completions
#

  • 安裝指令
1
git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/zsh-completions
  • 更新指令
1
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

  • 安裝指令
1
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  • 更新指令
1
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

  • 安裝指令
1
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  • 更新指令
1
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

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

設定 oh-my-zsh
#

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

  • ~/.zshrc
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

... 部份省略

# 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 設定啦!

1
source ~/.zshrc

接下來就會自動引導設定

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

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

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

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

1
p10k configure

有趣的小功能
#

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

我的設定
#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
 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

好玩套件
#

imgcat 終端機瀏覽圖片
#

  • 安裝
1
2
3
wget https://github.com/danielgatis/imgcat/releases/download/v1.0.21/imgcat_1.0.21_linux_arm64.tar.gz
sudo tar -xzf imgcat_1.0.21_linux_arm64.tar.gz -C /usr/local/bin
sudo ln -s /usr/local/bin/imgcat /usr/bin/imgcat
  • 使用
1
imgcat image.png

imgls 列出圖片內容
#

  • 安裝
1
2
3
go install github.com/rsookram/imgls/cmd/imgls@latest
sudo mv $HOME/go/bin/imgls /usr/locat/bin
sudo chown root:root /usr/locat/bin/imgls
  • 使用方式
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
❯ imgls
Failed to decode /home/pollochang/Pictures/A050624-張舜全-鄧茹方-修片檔.zip. image: unknown format
Failed to decode /home/pollochang/Pictures/A050624-張舜全-鄧茹方-第2次修片檔.zip. image: unknown format
Failed to decode /home/pollochang/Pictures/A050624-張舜全-鄧茹方-第3次修片檔.zip. image: unknown format
Failed to decode /home/pollochang/Pictures/profileIcon_vybz6w3m742f1.webp. image: unknown format
NUM     FORMAT  WIDTH   HEIGHT  SIZE    FILENAME
1       jpeg    1024    1024    80K     Designer.jpeg
2       png     1024    1024    1M      Gemini_Generated_Image_656h0n656h0n656h.png
3       png     1024    1024    862K    Gemini_Generated_Image_80s2yc80s2yc80s2.png
4       png     1024    1024    1M      Gemini_Generated_Image_oq5r1ooq5r1ooq5r.png
5       png     1024    1024    934K    Gemini_Generated_Image_qrtjaqqrtjaqqrtj.png
6       jpeg    225     225     13K     images.jpeg
7       png     1024    1024    730K    unnamed.png
8       jpeg    194     259     14K     吶喊.jpeg

參考資料
#

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

Powerlevel10k