Post

Linux 命令行工具

Linux 命令行工具

介绍

命令行工具安装方式:

  • Linux 端:Ubuntu(apt、snap 等)、Arch Linux(pacman、yay 等)
  • Windows 端:scoop、winget 等
  • Mac 端:brew;
  • 程序端:Python(pip、pipx、conda),Rust(cargo),Nodejs(npm)
  • webinstall.dev 网站安装(后三者可以在无 root 权限情况下安装)
  • 源码编译安装

参考资料:


zsh

  • 提升终端使用体验
  • master、manager 上没有 zsh;Pi 和思源一号有 zsh,但版本较老
  • zsh 系列插件:awesome-zsh-plugins
  • 管理 zsh 配置:ohmyzsh
  • 管理 bash 配置:oh-my-bash(没 ohmyzsh 好用)

安装

  • 通过 Package Managers
1
2
3
sudo apt install zsh  # Ubuntu
sudo pacman -S zsh    # Arch Linux
brew install zsh      # macOS

编译 ncurses(构建 TUI(文本用户界面)的库)

1
2
3
4
5
wget https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.4.tar.gz --no-check-certificate

./configure --prefix=${HOME}/local CXXFLAGS="-fPIC" CFLAGS="-fPIC"

make -j && make install

编译 zsh

1
2
3
4
5
wget https://sourceforge.net/projects/zsh/files/zsh/5.9/zsh-5.9.tar.xz/download -O zsh-5.9.tar.xz --no-check-certificate

./configure --prefix="${HOME}/local" CPPFLAGS="-I${HOME}/local/include" LDFLAGS="-L${HOME}/local/lib"

make -j && make install

下载 zsh 包;复制 etc/usr/ 到 Git 安装目录中;打开 Git Bash,执行命令 zsh

1
2
3
wget https://mirror.msys2.org/msys/x86_64/zsh-5.9-2-x86_64.pkg.tar.zst

tar --zstd -xvf zsh-5.9-2-x86_64.pkg.tar.zst

设置 zsh 为默认 shell,在 .bashrc 添加:

1
2
3
4
 # Enable zsh
 if [ -t 1 ]; then
    exec zsh
 fi

修改 Windows Terminal 的 settings.json 内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
    // ...
    // 添加项 
    // 默认启动为 Git Bash
    "defaultProfile": "{5D1F95DF-36E8-56AD-C203-EA75CE06422C}",
    // "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    // ...
        "list": 
        [			
            // 添加项
	        {
                "guid" : "{5D1F95DF-36E8-56AD-C203-EA75CE06422C}",
                "name" : "Git Bash",
                "commandline" : "D:\\Scoop\\apps\\git\\current\\bin\\bash.exe --login -i",
                "icon" : "D:\\Scoop\\apps\\git\\current\\usr\\share\\git\\git-for-windows.ico",
                "startingDirectory": "C:\\Users\\XXX\\Desktop"
            },
		    // ...
        ],
    },
}

  • 设置 zsh 为默认 shell
1
2
3
4
5
6
7
# 有 root 权限
chsh -s /bin/zsh

# 无 root 权限 在 ~/.bashrc_profile 添加以下内容(不建议)
export PATH=$HOME/bin:$PATH
export SHELL=`which zsh`
[ -f "$SHELL" ] && exec "$SHELL" -l

配置

ohmyzsh 介绍:插件丰富;丰富的 git 命令 alias,git 状态可视化

zimfw(类似 ohmyzsh):GitHub - zimfw/zimfw: Zim: Modular, customizable, and blazing fast Zsh framework


  • 安装 ohmyzsh
1
2
3
4
5
6
7
# gitee 源
sh -c "$(curl -fsSL https://gitee.com/Devkings/oh_my_zsh_install/raw/master/install.sh)"  # via curl
sh -c "$(wget https://gitee.com/Devkings/oh_my_zsh_install/raw/master/install.sh -O -)"  # via wget

# github 源
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"  # via curl
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"  # via wget
  • 插件下载:
    • powerlevel10k(主题)
    • zsh-completions(自动补全)
    • zsh-syntax-highlighting(高亮)
    • zsh-autosuggestions(建议)
    • zsh prompt(可选):spaceship-prompt
    • starship: Shell prompt(支持多种 shell,与 ohmyzsh 的主题不兼容)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# github 源
git clone --depth=1 https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM}/plugins/zsh-completions && \
git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM}/plugins/zsh-autosuggestions && \
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM}/plugins/zsh-syntax-highlighting && \
git clone --depth=1 https://github.com/zdharma-continuum/fast-syntax-highlighting.git ${ZSH_CUSTOM}/plugins/fast-syntax-highlighting && \
git clone --depth=1 https://github.com/jeffreytse/zsh-vi-mode ${ZSH_CUSTOM}/plugins/zsh-vi-mode && \
git clone --depth=1 https://github.com/MichaelAquilina/zsh-you-should-use.git ${ZSH_CUSTOM}/plugins/you-should-use && \
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM}/themes/powerlevel10k && \
# 可选
# git clone --depth=1 https://github.com/spaceship-prompt/spaceship-prompt.git ${ZSH_CUSTOM}/themes/spaceship-prompt && \
# ln -s ${ZSH_CUSTOM}/themes/spaceship-prompt/spaceship.zsh-theme ${ZSH_CUSTOM}/themes/spaceship.zsh-theme

# gitee 源
git clone --depth=1 https://gitee.com/yuhldr/zsh-syntax-highlighting.git ${ZSH_CUSTOM}/plugins/zsh-syntax-highlighting && \
git clone --depth=1 https://gitee.com/yuhldr/zsh-autosuggestions ${ZSH_CUSTOM}/plugins/zsh-autosuggestions && \
git clone --depth=1 https://gitee.com/yuhldr/zsh-completions ${ZSH_CUSTOM}/plugins/zsh-completions && \
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM}/themes/powerlevel10k
  • 备份 ~/.zshrc(如果有)

  • 重新登录,会进入配置 powerlevel10k 的交互,按照指示自定义设置即可

1
2
omz update      # 更新 ohmyzsh
p10k configure  # 配置 powerlevel10k

插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 内置插件
x          # 解压任意格式压缩
z          # 目录自动跳转,模糊匹配最近进入过的目录
git        # 丰富的 git alias

# 外置插件
zsh-syntax-highlighting
zsh-autosuggestions
zsh-completions
zsh-fast-syntax-highlighting
zsh-you-should-use
zsh-vi-mode  # Crtl + [ 进入 Normal mode
zsh-lovers
zsh-git-prompt

# bash 插件
bash-git-prompt       # 效果还不错
bash-language-server  # 有 Bash IDE 的 VSCode 插件
bash-completion
bash-snippets         # 有 cheat 等可执行命令

相关问题

  • zsh 中的 [nyae] 的含义:What does nyae mean in Zsh? - Stack Overflow

  • zsh 安装后,Home / End 键可能会失效,对应快捷键:Home = Ctrl + AEnd = Ctrl + E

  • 添加 ~/.bash_profile 文件(内容可为空),可使登录时不直接使用 zsh

1
export PATH=$PATH:$HOME/bin
  • 超算 Pi 更换操作系统后, zsh 无需重新编译,只是缺少了部分动态库如 libncursesw.so.5libtinfo.so.5,可从 conda 环境中 lib 目录里找到创建其符号链接,在 ~/.bashrc 文件中添加动态库 PATH
1
2
3
4
ln -s ~/.conda/envs/XXX/lib/libncursesw.so.6 ~/yangsl/lib/libncursesw.so.5
ln -s ~/.conda/envs/XXX/lib/libtinfo.so.6 ~/yangsl/lib/libtinfo.so.5

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/XXX/lib

数据处理相关命令行工具

  • CSV 命令行工具:csvkit(Python)
1
2
3
4
5
6
7
8
9
10
in2csv data.xlsx | csvlook  # Excel 表格转 csv 表格查看

csvlook data.csv | head  # 以表格形式查看

csvcut -n data.csv  # 查看列名
csvscut -c 1,2,3 data.csv   # 查看特定列数据 1 2 3 也可以是具体列名

csvstat --count data.csv  # 统计行数
csvstat file.csv  # 统计所有列的情况
csvstat -c 1,2,3 data.csv  # 统计特定列
  • JSON 命令行工具:jq、jnv(交互式)
1
2
3
cat data.json | jq .  # 输出 json 文件内容

cat data.json | jq '.user.name'  # 获取特定键值
  • JSON、YAML、TOML、HCL 格式之间互相转换:yj
1
2
3
4
5
brew install yj          # macOS 安装

yj -jy < package.json    # JSON 转 YAML
yj -yj < deploy.yml      # YAML 转 JSON
yj -yy < deploy.yml      # 会删除 YAML 文件中多余的空行

其他命令行工具

ripgrep、lsd、sd、bat、git-delta、gitui 等由 Rust 编写的 CLI 均可通过 cargo 安装

系统相关


Markdown 相关


文件相关


编程相关

  • 命令纠正:thefuck
  • 统计代码文件行数:cloc

图片相关


其他

  • sshx:通过链接共享终端(可创建多个终端画布)
  • 富文本:rich
  • 字符 logo 制作:figlet、toilet:Linux 运维相关 — OnlineNote latest documentation
  • 文本编辑器:helix
  • 趣味小工具: cowsay、sl(火车)、fortune(幸运饼干;格言)、lolcat、boxes、cmatrix(黑客帝国)、asciiquarium(水族馆)

1
2
3
4
5
# 搜索整个 apt package;回车安装
apt-cache search '' | sort | cut --delimiter ' ' --fields 1 | fzf --multi --cycle --reverse \ --preview-window=right:70%:wrap \ --preview 'apt-cache show {1}' | xargs -r sudo apt install -y

# 用 bat 作为 previewer
fzf --preview "bat --color=always --style=numbers --line-range=:500 {}"
  • 用 fzf-tab 替代 zsh 的自动补全:GitHub - Aloxaf/fzf-tab
    • 需将 fzf-tab 写在 zsh-autosuggestions、fast-syntax-highlighting 插件前,compinit 后
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安装
git clone --depth 1 https://github.com/Aloxaf/fzf-tab ${ZSH_CUSTOM}/plugins/fzf-tab

# fzf-tab 配置;写入 ~/.zshrc 
# disable sort when completing `git checkout`
zstyle ':completion:*:git-checkout:*' sort false
# set descriptions format to enable group support
# NOTE: don't use escape sequences here, fzf-tab will ignore them
zstyle ':completion:*:descriptions' format '[%d]'
# set list-colors to enable filename colorizing
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# force zsh not to show completion menu, which allows fzf-tab to capture the unambiguous prefix
zstyle ':completion:*' menu no
# preview directory's content with eza when completing cd
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza -1 --color=always $realpath'
# switch group using `<` and `>`
zstyle ':fzf-tab:*' switch-group '<' '>'

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
# turm 安装
cargo install turm

# peco 安装与使用
brew install peco  # 安装

cat file | peco    # 基本使用


ncdu -o ncdu.txt  # 输出信息到文件中


# navi 使用
navi repo browse  # 按需添加 cheatsheet git repo 以增加丰富性


# 升级 fzf
cd ~/.fzf && git pull && ./install


# rg 使用
# -g 过滤搜索
rg 'content' -g '!docs/'  # 排除
rg 'content' -g '*.py'    # 包含


# eg 安装
pip install -U eg
brew install eg-examples


# figlet toilet 相关用法
showfigfonts   # 查看可用字体
figlet spt
figlet -c spt  # 居中 
figlet spt | toilet -f term --gay  # 彩色输出


# mcfly 安装与配置
brew install mcfly

curl -LSfs https://raw.githubusercontent.com/cantino/mcfly/master/ci/install.sh | sh -s -- --git cantino/mcfly

eval "$(mcfly init zsh)"


# fastfetch Ubuntu 安装
sudo add-apt-repository ppa:zhangsongcui3371/fastfetch
sudo apt update

sudo apt install fastfetch


# starship
# 安装
curl -sS https://starship.rs/install.sh | sh  # Linux
brew install starship  # macOS

# 配置
eval "$(starship init zsh)"   # zsh
eval "$(starship init bash)"  # bash
Invoke-Expression (&starship init powershell) # PowerShell


# primitive 安装与使用
go install github.com/fogleman/primitive@latest

export PATH=$(go env GOPATH)/bin:$PATH

primitive -i input.png -o output.png -n 100
This post is licensed under CC BY 4.0 by the author.