Personalization for Terminal,Shell & Prompt

Personalization for Terminal,Shell & Prompt

前言

工欲善其事必先利其器,终端算是编程中经常接触的“器”,下面记录个人针对 terminal,shell 和 prompt 的一些个性化设置,仅供参考

作者沉迷折腾,永远在利器,从来不做事;带伙引以为戒 😵‍💫
不过折腾的过程雀食有意思(也可能是我太闲了)

📚Related Glossary

MeaningExamples
TerminalA text input and output environment.Windows Terminal,Alacritty,Kitty,Wezterm…
ConsoleA physical terminal.
Shellcommand-line interpreternushell,bash,zsh,fish,powershell…
PromptThe text that appears at the beginning of a command line形如$>>>

如果想更细致的了解这几个概念的区别,可参考What is the difference between Terminal, Console, Shell, and Command Line?


⌨️Terminal

个人体验的终端有 Windows Terminal,Wezterm;

Wezterm 可自定义的程度比较高,可以自定义 tab 的位置&样式,但体验过程中,Wezterm 的启动速度相对较慢,目前主要使用 Windows Terminal

我的一些个性化设置

  • 字体: Maple Mono NF CN,对 Ligatures,中文支持比较好的 Nerd Font;
  • toggle focus mode: 选择开启 Focus Mode,可以隐藏上方 tab 栏,让界面更简洁
  • Color Schemes:Tokyo Night 或 Catppuccin Mocha;比较美观的两款夜间主题
  • Appearance: 在 Transparency 中选择 Enable acrylic material,启用亚克力效果;透明度 80%

效果图


🐚Shell

个人觉得比较好用的 shell 有 Nushell,fish;但 pwsh 在个人机器上加载速度略慢,对使用体验有些影响。

对于 Windows 环境,fish 安装略麻烦,Nushell 可以通过wingetscoop或者cargo等进行安装;对于 linux,wsl 环境,两者安装都是比较容易的。

1
2
3
4
5
6
# windows
winget install nushell
# macos
brew install nushell
# arch linux
sudo pacman -S nushell

Nushell 社区比较活跃,插件市场&配置分享在 github 可以找到很多,感觉是比 powershell 活跃的。

nu_scripts仓库中维护了社区用户分享的 nushell 配置,喜欢折腾的可以参考本仓库。

下面是个人的 Nushell config,配置文件比较长,考虑篇幅,这里只附上关键内容。有需要完整配置的可以联系作者

Keybindings

我下载了 fuzzy finder 以及 bat 这两个命令行工具,配合以下配置可以方便的检索历史命令以及文件;此外还有一些比较实用的命令行工具,放在文末

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
# keybindings.nu
$env.config.keybindings = (
    $env.config.keybindings
    | append {  # history_menu using fzf
        name: fzf_history_menu_fzf_ui
        modifier: control
        keycode: char_r
        mode: [emacs, vi_normal, vi_insert]
        event: {
            send: executehostcommand
            cmd: "commandline edit --insert (cat $nu.history-path | fzf --height 70% --layout reverse --border +s --tac | str trim)"
        }
    }
    | append {
        name: fuzzy_file
        modifier: control
        keycode: char_t
        mode: emacs
        event: {
        send: executehostcommand
        cmd: "commandline edit --insert (fzf --layout=reverse --preview 'bat --color=always --style=numbers --line-range=:500 {}')"
        }
    }
)
# 在你的config.nu里
source <path_to_keybind.nu>

Completions

nu_scripts仓库中提供了部分命令的补全脚本,一定程度上可以节省搜命令的时间,比如下面这些

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 ls ./custom-completions/
╭────┬───────────────────────────┬──────┬──────┬─────────────╮
  # │           name            │ type │ size │  modified   │
├────┼───────────────────────────┼──────┼──────┼─────────────┤
  0  custom-completions\adb     dir    0 B  2 weeks ago 
  1  custom-completions\bat     dir    0 B  3 weeks ago 
  2  custom-completions\btm     dir    0 B  3 weeks ago 
  3  custom-completions\cargo   dir    0 B  3 weeks ago 
  4  custom-completions\curl    dir    0 B  3 weeks ago 
  5  custom-completions\docker  dir    0 B  3 weeks ago 
  6  custom-completions\eza     dir    0 B  2 weeks ago 
  7  custom-completions\gh      dir    0 B  2 weeks ago 
  8  custom-completions\git     dir    0 B  3 weeks ago 
  9  custom-completions\mvn     dir    0 B  3 weeks ago 
 10  custom-completions\npm     dir    0 B  2 weeks ago 
 11  custom-completions\pytest  dir    0 B  2 weeks ago 
 12  custom-completions\scoop   dir    0 B  3 weeks ago 
 13  custom-completions\ssh     dir    0 B  2 weeks ago 
 14  custom-completions\tar     dir    0 B  3 weeks ago 
 15  custom-completions\vscode  dir    0 B  2 weeks ago 
 16  custom-completions\winget  dir    0 B  3 weeks ago 
╰────┴───────────────────────────┴──────┴──────┴─────────────╯

使用效果如下图所示


💲Prompt

Prompt 的美化方案有很多,比较知名的有 starship,ohmyzsh,ohmyposh;这里采用 ohmyposh

此前 omp 采用 Powershell 编写,后来为了兼容性用 go 重写,现在适用于 powershell,nushell,fish,zsh,bash 等等,甚至在安卓上也可以食用;

oh my posh 对自定义的支持程度非常高,支持的 Prompt 内容也很广泛,包括但不限于

  • OS
  • docker
  • git
  • java、python、golang、rust…
  • path
  • Execution Time

添加一些 prompt 对开发体验有一定提升。我在官方提供的主题上进行了自定义,配置文件放在后面仅供参考

显示效果如下图所示

Theme

Oh My Posh 提供了非常丰富的主题,通过Get-PoshThemes命令可以查看浏览效果

我根据已有主题进行了自定义,配置文件如下所示,可使用 json,yaml 等语言编写主题;

找 icon 可以去nerd-font cheat-sheet

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// File: themes\omp.json
  {
    "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json"
    "blocks": [
      {
        "alignment": "left"
        "segments": [
          {
            "type": "text"
            "style": "powerline"
            "template": "<#92b0ed>╭─</>"
          }
          {
            "background": "#29315a"
            "foreground": "#92b0ed"
            "style": "diamond"
            "leading_diamond": "\ue0b6"
            "template": "<b>{{ .Icon }} </b>"
            "type": "os"
            "properties": {
              "windows": " Windows"
              "arch": " Arch"
              "ubuntu": " Ubuntu"
            }
          }
          {
            "background": "#8891e0"
            "foreground": "#090909"
            "powerline_symbol": "\ue0b0"
            "properties": {
              "folder_icon": "󱧬 "
              "folder_separator_icon": "/"
              "home_icon": ""
              "style": "letter"
              "mixed_threshold": 4
              "mapped_locations": {
                "E:/OneDrive - 商业版/home/vluv": "Vluv's Space "
                "E:/OneDrive - 商业版/home/markdown": "My Notes 󰍔"
                "C:/Users/24138/AppData/Local": "LOCAL APPDATA "
              }
            }
            "style": "powerline"
            "template": " <#000>  </><b>{{ .Path }}</b> "
            "type": "path"
          }
          {
            "background": "#29315A"
            "foreground": "#43CCEA"
            "foreground_templates": [
              "{{ if or (.Working.Changed) (.Staging.Changed) }}#e0af68{{ end }}"
              "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#ff4500{{ end }}"
              "{{ if gt .Ahead 0 }}#bb9af7{{ end }}"
              "{{ if gt .Behind 0 }}#bb9af7{{ end }}"
            ]
            "powerline_symbol": "\ue0b0"
            "properties": {
              "branch_max_length": 25
              "fetch_stash_count": true
              "fetch_status": true
              "fetch_upstream_icon": true
            }
            "style": "powerline"
            "template": " {{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working
  .Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .S
  taging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} "
            "trailing_diamond": "\ue0b4"
            "type": "git"
          }
          {
            "background": "#9ece6a"
            "foreground": "#ffffff"
            "powerline_symbol": "\ue0b0"
            "style": "powerline"
            "template": " \ue718 {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }} "
            "type": "node"
          }
          {
            "type": "rust"
            "style": "powerline"
            "powerline_symbol": ""
            "background": "#f7768e"
            "foreground": "#c31414"
            "template": " <b> Rust </b>{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
          }
          {
            "type": "java"
            "style": "powerline"
            "powerline_symbol": "\ue0b0"
            "background": "#f7768e"
            "foreground": "#c31414"
            "template": " <b> JDK</b>{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
          }
          {
            "type": "python"
            "style": "powerline"
            "powerline_symbol": "\ue0b0"
            "background": "#bb9af7"
            "foreground": "#100e23"
            "template": " \ue235 {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}{{ e
  nd }} "
          }
          {
            "type": "go"
            "style": "powerline"
            "powerline_symbol": "\ue0b0"
            "background": "#90d8f7"
            "foreground": "#100e23"
            "template": "<b> 󰟓 Go{{ .Full }} </b>"
          }
          {
            "background": "#bb9af7"
            "foreground": "#100e23"
            "style": "powerline"
            "powerline_symbol": "\ue0b0"
            "template": " \ue620 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
            "type": "lua"
          }
        ]
        "type": "prompt"
      }
      {
        "alignment": "right"
        "segments": [
          {
            "foreground": "#8aadf4"
            "properties": {
              "style": "dallas"
              "threshold": 0
            }
            "style": "diamond"
            "template": " {{ .FormattedMs }}s in "
            "type": "executiontime"
          }
          // {
          //   "foreground": "#e0af68",
          //   "style": "diamond",
          //   "template": "  RAM {{ (div ((sub .PhysicalTotalMemory .PhysicalFreeMemory)|float64) 1073741824.0) }}/{{
  (div .PhysicalTotalMemory 1073741824.0) }}GB "
          //   "type": "sysinfo"
          // },
          {
            "foreground": "#bb9fee"
            "style": "diamond"
            "template": "\uf489  {{ .Name }} "
            "type": "shell"
          },
          {
            "foreground": "#ddcca4"
            "properties": {
              "time_format": "3:04 PM"
            },
            "style": "diamond"
            "template": "󰅐 {{ .CurrentDate | date .Format }}"
            "type": "time"
          }
          // {
          //   "type": "owm"
          //   "style": "diamond"
          //   "foreground": "#7dcfff"
          //   "template": "{{.Weather}} {{.Temperature}}{{.UnitIcon}}"
          //   "properties": {
          //     "api_key": "🤖"
          //     "location": "Chengdu,CN"
          //     "units": "metric"
          //     "enable_hyperlink": true,
          //     "http_timeout": 400,
          //     "cache_timeout": 15
          //   }
          // }
        ],
        "type": "prompt"
      },
      {
        "alignment": "left"
        "newline": true,
        "segments": [
          {
            "type": "text"
            "style": "powerline"
            "template": "<#92b0ed>\u2514 </>"
          },
          {
            "foreground": "#86e1fc"
            "foreground_templates": [
              "{{ if gt .Code 0 }}#ef5350{{ end }}"
            ],
            "properties": {
              "always_enabled": true
            },
            "style": "plain"
            "template": " </>"
            "type": "status"
          }
        ],
        "type": "prompt"
      }
    ],
    "final_space": true,
    "version": 2
  }

🛠️CLI & TUI

下面是一些比较实用/有意思的命令行工具

TUI

  • gitui
    Blazing 💥 fast terminal-ui for git written in rust 🦀
    类似于 lazygit,在终端中管理 git repo,适合不想动鼠标的人
  • btop
    A monitor of resources
    任务管理器 TUI 版,同样适合纯键盘操作
  • yazi
    💥 Blazing fast terminal file manager written in Rust, based on async I/O.
    支持 windows 平台的 terminal file manager,除此之外 midnight commander 也支持 windows;适合纯键盘操作

CLI

  • bat
    A cat clone with syntax highlighting and Git integration.
    相比 cat 多了语法高亮

  • zoxide
    A smarter cd command. Supports all major shells.
    快速切换目录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    z foo              # cd into highest ranked directory matching foo
    z foo bar          # cd into highest ranked directory matching foo and bar
    z foo /            # cd into a subdirectory starting with foo
    
    z ~/foo            # z also works like a regular cd command
    z foo/             # cd into relative path
    z ..               # cd one level up
    z -                # cd into previous directory
    
    zi foo             # cd with interactive selection (using fzf)
    
    z foo<SPACE><TAB>  # show interactive completions (zoxide v0.8.0+, bash 4.4+/fish/zsh only)

下面三个都是搜索类的工具,ripgrep 是 grep 的替代品(项目名意为 R.I.P grep),VSCode 的快速搜索便是用的 ripgrep,fzf 可以与其它的工具相结合,用于模糊搜找文件,历史记录等等..

  • ripgrep
    ripgrep recursively searches directories for a regex pattern while respecting your gitignore
  • fd
    A simple, fast and user-friendly alternative to find.
  • fzf
    🌸 A command-line fuzzy finder;

MISC

Personalization for Terminal,Shell & Prompt

https://vluv.space/Dev/Others/terminal/

作者

Jiaxing Gao

发布于

2024-10-19

更新于

2024-10-19

许可协议

评论

}