Colorful Commands

导言

电脑玩家经常说RGB是最重要的,对于程序员来说,彩色的terminal有助于快速的分辨输出的有效信息。为此有一些有意思的彩色输出命令。

Cheat sheet (命令行小抄/备忘录)

最经典 tdlr

使用时需要网络

1
2
npm install -g tldr     
pip3 install tldr

tealdeer - simplest example

a RUST fast version oftdlr

Tips: OpenSSL development headers
get a “failed to run custom build command for openssl-sys” error message. The package is calledlibssl-dev on Ubuntu.

1
2
3
4
5
# install
cargo install tealdeer

# 使用
tdlr <>

部分支持中文,支持多平台

支持在线网页版

  1. Linux Command Line Cheat Sheet
  2. devhints.io
    1. 各种的选项,不止命令,包括bash, vim, mysql, git
    2. 语言 go, python, java, JS, NodeJS, Ruby 3.
  3. navi
    1. 支持语意转换的补全 🔥
  4. cheat.sh 🔥
    1. 支持 curl命令直接访问或者交互式
    2. 支持补全
    3. 返回内容集成cheat cheat.sheets tdlr
  5. bropages.org
    1. 用户自发投票排序的命令用例

navi installation & usage

1
2
3
4
5
6
7
8
9
# install
cargo install --locked navi

# download default cheatsheet
navi repo add denisidoro/cheats

# 使用
navi
# 基于fzf寻找需要指令

SHELL

awesome-shell里多看看。

set_tsj.sh

1
2
3
4
5
6
HOME=/home/t00906153
export SHELL=zsh
export HOME=$HOME
export HISTFILE=$HOME/.zsh_history
cd
zsh

oh my zsh

虽然这个ohmyzsh好用,但是我用惯了hyq的模板, 从github上下载后解压就安装了zsh模板。(之后可以考虑传到云盘或者cloudflare)

1
2
3
4
5
HOME=/home/xxx
export SHELL=zsh
export PATH=$HOME/.local/bin:$PATH
export HISTFILE=$HOME/.zsh_history
zsh
1
2
3
4
# git clone https://github.com/Kirrito-k423/QuickStartLinux.git --depth=1
# cd QuickStartLinux/resources
wget https://raw.githubusercontent.com/Kirrito-k423/QuickStartLinux/main/resources/zsh.tar
tar xvf zsh.tar -C $HOME

oh my zsh

有时候hyq模版有些兼容性问题,比较老的zsh不支持(大约是5.0.2到5.5.1的版本)。这时候只能手动安装了,包括主题和插件。请看zsh一文。

zsh_history 支持多端口同步,实时保存

title
1
2
3
4
5
6
7
8
9
HISTFILE=~/.zsh_history
SAVEHIST=30000 # 最多 10000 条命令的历史记录
setopt APPEND_HISTORY # 退出 zsh 会话时,命令不会被覆盖式地写入到历史文件,而是追加到该文件的末尾
setopt INC_APPEND_HISTORY # 会话进行中也会将命令追加到历史文件中
setopt SHARE_HISTORY # 所有会话中输入的命令保存到一个共享的历史列表中
export HIST_SAVE_FREQ=10 # 每10次命令保存一次
ulimit -c unlimited
echo '$HOME/core-%e.%p.%h.%t' > /proc/sys/kernel/core_pattern
cd ~

窗口管理器 tmux-like

大部分Linux 系统自带的 screen 命令来多终端控制

外部控制:

  • 创建: screen -S name
  • 恢复: screen -r name
  • 列表: screen -ls
  • 删除: screen -X -S name kill

内部控制:

  • CtrlA + d 分离
  • CtrlA + x 终端上锁,CtrlA + k 是kill
  • CtrlA + S 上下分屏,CtrlA + | 左右分屏。
    • screen -S name创建会话后,需要在新窗口中执行screen来创建额外的终端;这样分屏之后才有两个以上的终端可以使用。
    • CtrlA + w 查看终端
    • CtrlA + [数字] 切换到第几个
    • CtrlA + Tab 返回上一个
    • CtrlA + X 关闭分屏
    • CtrlA + : 进入命令模式,输入resize -v 100或者resize -h 100调整大小。

oh my tmux 🔥

1
2
3
4
5
6
7
8
9
10
11
12
## Install
cd
git clone https://github.com/gpakosz/.tmux.git
ln -s -f .tmux/.tmux.conf
cp .tmux/.tmux.conf.local .

# or
cd ~/resources
# wget https://gitee.com/shaojiemike/oh-my-tmux/repository/blazearchive/master.zip?Expires=1629202041&Signature=Iiolnv2jN6GZM0hBWY09QZAYYPizWCutAMAkhd%2Bwp%2Fo%3D
unzip oh-my-tmux-master.zip -d ~/
ln -s -f ~/oh-my-tmux-master/.tmux.conf ~/.tmux.conf
cp ~/oh-my-tmux-master/.tmux.conf.local ~/.tmux.conf.local

基于Rust的zellij

开发编辑器 vim-like

vimrc 🔥

1
2
git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime
sh ~/.vim_runtime/install_awesome_vimrc.sh

emacs

针对不同语言有许多可选插件

Ubuntu install emacs27

1
2
3
add-apt-repository ppa:kelleyk/emacs
apt-get update
apt-get install emacs27

问题:

1
2
3
4
dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)
Errors were encountered while processing:
/var/cache/apt/archives/emacs27-common_27.1~1.git86d8d76aa3-kk2+20.04_all.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

版本解决,强制安装 sudo apt-get -o Dpkg::Options::="--force-overwrite" install emacs27-common

1
2
3
4
5
6
7
sudo apt --purge remove emacs27
sudo apt --purge remove emacs
sudo apt --purge remove emacs-common
sudo apt --fix-broken install
sudo apt autoremove
sudo apt install emacs27
emacs --version

常用命令的”Colorful”版本

cd

z.lua - learning cd 🔥

Install LUA

1
2
3
4
5
curl -R -O http://www.lua.org/ftp/lua-5.4.4.tar.gz       
tar zxf lua-5.4.4.tar.gz
cd lua-5.4.4
make all test
sudo make install # usr/bin

Install

1
2
3
4
5
6
7
8
9
10
cd ~/github
git clone https://github.com/skywind3000/z.lua.git

# vim ~/.zshrc
alias zz='z -c' # 严格匹配当前路径的子路径
alias zi='z -i' # 使用交互式选择模式
alias zf='z -I' # 使用 fzf 对多个结果进行选择
alias zb='z -b' # 快速回到父目录
#eval "$(lua /path/to/z.lua --init zsh)" # ZSH 初始化
eval "$(lua ~/github/z.lua/z.lua --init zsh)"

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
# 弹出栈顶 (cd 到上一次的老路径),和 "z -0" 相同
$ z -

# 显示当前的 dir stack
$ z --

# 交互式
z -i foo # 进入交互式选择模式,让你自己挑选去哪里(多个结果的话)
z -I foo # 进入交互式选择模式,但是使用 fzf 来选择

# 匹配
z foo$
1
2
3
4
5
6
7
8
9
10
z foo       # 跳转到包含 foo 并且权重(Frecent)最高的路径       
z foo bar # 跳转到同时包含 foo 和 bar 并且权重最高的路径
z -r foo # 跳转到包含 foo 并且访问次数最高的路径
z -t foo # 跳转到包含 foo 并且最近访问过的路径
z -l foo # 不跳转,只是列出所有匹配 foo 的路径
z -c foo # 跳转到包含 foo 并且是当前路径的子路径的权重最高的路径
z -e foo # 不跳转,只是打印出匹配 foo 并且权重最高的路径
z -i foo # 进入交互式选择模式,让你自己挑选去哪里(多个结果的话)
z -I foo # 进入交互式选择模式,但是使用 fzf 来选择
z -b foo # 跳转到父目录中名称以 foo 开头的那一级

缺点:

  • 没去过的路径,每级文件夹的补全没有了
  • 可以和cd结合使用

ls

exa 🔥 - better ls

1
2
3
4
5
6
7
8
9
# Manual installation from GitHub. Ubuntu 20.10才支持
wget https://github.com/ogham/exa/releases/download/v0.10.1/exa-linux-x86_64-musl-v0.10.1.zip
unzip exa-linux-x86_64-musl-v0.10.1.zip
mv bin/exa ~/.local/bin

# 使用
exa -l
# 文件夹大小
du -d 1 -h .

grep

rg (Fast & Good multi-platform compatibility) > ag > ack(ack-grep) 🔥

[repgrep(rg)](https

1
2
3
4
5
6
# ripgrep(rg) 但是readme说这样有bugs       
sudo apt-get install ripgrep
# 可执行文件 (推荐)
wget https://github.com/BurntSushi/ripgrep/releases/download/13.0.0/ripgrep-13.0.0-x86_64-unknown-linux-musl.tar.gz
tar -zxvf ripgrep-13.0.0-x86_64-unknown-linux-musl.tar.gz
mv ./ripgrep-13.0.0-x86_64-unknown-linux-musl/rg ~/.local/bin

repgrep(rg) 常用选项

  • --no-ignore 忽略.gitignore之类的文件,也搜索忽略的文件。(默认是不搜索的)
  • -t txt 指定搜索类型
  • rg 'content' ABC/*.cpp搜索和正则ABC/*.cpp匹配的文件

ag

1
2
3
4
# ag 2020年就不维护了       
apt-get install silversearcher-ag
# It ignores file patterns from your .gitignore and .hgignore.
# use -u or -U option to reinclude these files to search scoop

find

find . -name "*xxx*"

fzf 🔥带预览的find

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ubuntu
sudo apt install fzf

# GIT install
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
source ~/.zshrc
# Vim-plugin
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }

# 使用
fzf --preview 'less {}'

# 安装了bat
fzf --preview "batcat --style=numbers --color=always --line-range :500 {}"

telescope.nvim 也带预览的find

官网

1
2
3
4
5
6
7
8
9
10
# 先安装vim-plug
curl -fLo~/.vim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
# 修改~/.vimrc
call plug#begin()
Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-telescope/telescope.nvim'
call plug#end()

# 还需要 Neovim
to do

cat

bat 🔥 - colorful cat

1
2
3
4
5
6
7
8
# Install
sudo apt install bat

# 使用
batcat filename
# 指定行号
alias cat="batcat"
cat -r 35:42 /etc/hosts

git

gitui 🔥 - fast Rust lazygit

https://github.com/extrawurst/gitui/releases

1
2
3
4
5
6
7
8
9
# 建议Rust,三句命令,安装Rust,source,gitui
curl https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env
cargo install gitui

# 安装(由于还在开发中建议去官网 , 现在不支持armV7
wget https://github.com/extrawurst/gitui/releases/download/v0.20.1/gitui-linux-musl.tar.gz
tar -zxvf gitui-linux-musl.tar.gz
mv gitui ~/.local/bin

lazygit

1
2
3
4
5
6
# install go
wget https://go.dev/dl/go1.18.1.linux-amd64.tar.gz

git clone https://github.com/jesseduffield/lazygit.git
cd lazygit
go install

高亮终端输出/log文件

bash脚本输出颜色文本示例

1
2
3
4
5
RED='\033[0;31m'       
NC='\033[0m'
# No Color
printf "I ${RED}love${NC} Stack Overflow\n"
echo -e "\033[5;36m Orz 旧容器(镜像)已清理\033[0m"

颜色编号如下

颜色 编号
Black 0;30
Dark Gray 1;30
Red 0;31
Light Red 1;31
Green 0;32
Light Green 1;32
Brown/Orange 0;33
Yellow 1;33
Blue 0;34
Light Blue 1;34
Purple 0;35
Light Purple 1;35
Cyan 0;36
Blue 0;37
Light Cyan 1;36
Light Gray 0;37
White 1;37

hl 🔥自定义高亮各种log文件

通过regular expressions自定义高亮各种log文件

install需要 lex

1
2
3
4
5
git clone https://github.com/mbornet-hl/hl       
make clean;
make
cp hl /usr/local/bin
# move

颜色支持(3浅中深 * 6颜色 * 背景色反转)

1
2
3
# 前面 123 是深浅 , 4是下划线       
# 字母大写是背景色反转
-r : red -g : green -y : yellow -b : blue -m : magenta -c : cyan -w : white

正则标记log关键词

绿色和红色

1
cat exemple.log | hl -g start -r stop       

正则表示

1
2
3
4
5
6
7
-e : extended regular expressions
-i : ignore case

hl -ei -g '(start(|ing|ed))' -r '(stop(|ping|ped))'

## ip 匹配
curl --trace-ascii - www.baidu.com|hl -ei -1R '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

命令配置文件 hl_ha.cfg

默认设置

1
2
export HL_CONF=/staff/shaojiemike/github/hl/config_files       
echo $HL_CONF │/staff/shaojiemike/github/hl/config_files
1
2
-%c : specifies the beginning of a range colorized in color 'c'       
-. : specifies the end of the previous range

Colorize hl configurations :

1
hl -vop '*' | hl --hl_conf       

example Commands

1
2
3
4
5
lD # ls by date       
lW # ls by week
ifconfig -a | hl --ifconfig
# ping tcpdump fdisk apt-get diff
# ip ibstat iptables passwd

errors

常用方式

~/.zshrc 里如下配置:

1
2
3
4
5
6
export HL_CONF=/home/shaojiemike/github/hl/config_files       
function my_hl(){ hl -eg '\$\{?[A-Z_]+\}?' -ec ' ([A-Z_-]+) ' -eic '(nothing|note)' -eiy ' (-([a-z_-]+))' -eiy '0x[0-9a-z]+' --errors -eig '(yes)' -eir '((^| )no($| ))|(none)|(not)|(null)|(please)|( id )' -ir error -ir wrong -ib '(line)|(file)' -eiy '(warn(|ing))|(wait)|(idle)|(skip)' -im return -ic '(checking)' -eiy ' (__(.*)__) ' -ei1W '((\w*/[.A-Za-z0-9_/-]*[A-Za-z0-9_/-]*)("|$)?)|((\w*/[.A-Za-z0-9_/-]*[A-Za-z0-9_/-]*)(")? ) ' -3B '[0-9][0-9.]+' -3B ' ([0-9])|(#[0-9]+)' -eig '(start(|ing))' -eir '(end(|ing))' }
alias ifconfig='ifconfig | hl --ifconfig'
alias ip='ip a|hl --ip '
alias df='df -h |hl --df'
alias ibstat='ibstat |hl --ibstat'

编译时如此使用make 2>&1|my_hl

系统信息

dua-cli - best disk space viewer 🔥

资源监控

资源监控软件netdata

  • netdata 默认挂载在http://127.0.0.1:19999/。想要WebUI运行 sudo netdata -i node5.acsalab.com
  • cpu, disk, memory, network,温度都有记录
  • arm下有问题,需要自己编译

资源监控命令bottom(htop like)

1
2
3
4
5
6
# install
curl -LO https://github.com/ClementTsang/bottom/releases/download/0.6.8/bottom_0.6.8_amd64.deb
sudo dpkg -i bottom_0.6.8_amd64.deb

# 使用
btm

类似s-tui可以观察CPU 温度,频率

网络监控 bmon

bmon是类 Unix 系统中一个基于文本,简单但非常强大的网络监视和调试工具

Compile yourself

Install libconfuse

sh wget https://github.com/martinh/libconfuse/releases/download/v2.8/confuse-2.8.zip unzip confuse-2.8.zip && cd confuse-2.8 PATH=/usr/local/opt/gettext/bin:$PATH ./configure make make install Install bmon

sh git clone https://github.com/tgraf/bmon.git cd bmon ./autogen.sh ./configure make make install bmon

文件管理器

ranger 基于vi的支持预览的横向多级显示 🔥

https://github.com/ranger/ranger

1
2
3
pip install ranger-fm

# renger直接使用,方向键或者hjkl,可以直接跳转到vim修改

参考文献

Author

Shaojie Tan

Posted on

2023-10-08

Updated on

2025-01-30

Licensed under