bash实现类似zsh那样的历史命令自动补全,彩色终端的方法

Friday, Mar 13, 2026 | 2 minute read | Updated at Monday, Jun 15, 2026

@
bash实现类似zsh那样的历史命令自动补全,彩色终端的方法,是标准的bash shell,使用任何用户


# 1. 下载
curl -L https://github.com/akinomyoga/ble.sh/releases/download/nightly/ble-nightly.tar.xz | tar xJf -

# 2. 安装
bash ble-nightly/ble.sh --install ~/.local

# 3. 加入 ~/.bashrc 末尾
echo 'source ~/.local/share/blesh/ble.sh' >> ~/.bashrc

# 4. 立即生效
source ~/.bashrc


如果出现了下面的错误


source ~/.bashrc                                                                                                                                                                            
-bash: /home/user/.local/share/blesh/ble.sh: No such file or directory


安装路径不一样,文件装到了 /home/user/.local/blesh/ 而不是 /home/user/.local/share/blesh/,修正一下:


# 先删掉错误的那行
sed -i 's|source ~/.local/share/blesh/ble.sh||' ~/.bashrc

# 加入正确路径
echo 'source ~/.local/blesh/ble.sh' >> ~/.bashrc

# 生效
source ~/.bashrc


在 ~/.bashrc 里加上语法高亮配置,ble.sh 内置支持:

bashcat >> ~/.bashrc << 'EOF'

# ble.sh 语法高亮
bleopt syntax_highlight=1
ble-face command=fg=green,bold
ble-face keyword=fg=cyan,bold
ble-face argument=fg=white
ble-face filename=fg=yellow
ble-face error=fg=red,bold
EOF

source ~/.bashrc

按ctrl+J执行,

使用`ble-face | grep -E 'command|syntax|error'`可以检测正确的颜色


ble-face argument_error=fg=white,bg=red
ble-face command_alias=fg=teal
ble-face command_builtin=fg=cyan,bold
ble-face command_builtin_dot=fg=red,bold
ble-face command_directory=fg=33,underline
ble-face command_file=fg=green,bold
ble-face command_function=fg=99
ble-face command_jobs=fg=red,bold
ble-face command_keyword=fg=cyan,bold
ble-face command_suffix=fg=231,bg=28
ble-face command_suffix_new=fg=231,bg=124
ble-face menu_desc_quote=ref:syntax_quoted
ble-face menu_desc_type=ref:syntax_delimiter
ble-face syntax_brace=fg=37,bold
ble-face syntax_command=fg=green,bold
ble-face syntax_comment=fg=242
ble-face syntax_default=none
ble-face syntax_delimiter=bold
ble-face syntax_document=fg=100
ble-face syntax_document_begin=fg=100,bold
ble-face syntax_error=fg=white,bg=red
ble-face syntax_escape=fg=magenta
ble-face syntax_expr=fg=33
ble-face syntax_function_name=fg=99,bold
ble-face syntax_glob=fg=198,bold
ble-face syntax_history_expansion=fg=231,bg=94
ble-face syntax_param_expansion=fg=133
ble-face syntax_quotation=fg=yellow,bold
ble-face syntax_quoted=fg=yellow
ble-face syntax_tilde=fg=63,bold
ble-face syntax_varname=fg=orange



颜色显示用户名、主机名、路径等。
在 ~/.bashrc 里加上彩色 PS1:
bashcat >> ~/.bashrc << 'EOF'

# 彩色 prompt
PS1='\[\e[1;31m\]\u\[\e[0m\]@\[\e[1;33m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\$ '
EOF

source ~/.bashrc


完整的配置如下


cat ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.

# Note: PS1 is set in /etc/profile, and the default umask is defined
# in /etc/login.defs. You should not need this unless you want different
# defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
# umask 022
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

# 增大历史记录数量
HISTSIZE=10000
HISTFILESIZE=20000
HISTCONTROL=ignoredups:erasedups  # 去重
# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "$(dircolors)"
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
. "/root/.acme.sh/acme.sh.env"

source ~/.local/blesh/ble.sh

# ble.sh 语法高亮
ble-face syntax_command=fg=green,bold
ble-face command_file=fg=green,bold
ble-face command_builtin=fg=cyan,bold
ble-face command_keyword=fg=cyan,bold
ble-face syntax_quoted=fg=yellow
ble-face syntax_quotation=fg=yellow,bold
ble-face argument_error=fg=white,bg=red
ble-face syntax_error=fg=white,bg=red

# 彩色 prompt
PS1='\[\e[0;90m\]┌─[\[\e[0m\]\[\e[1;32m\]\u\[\e[0;90m\]@\[\e[1;36m\]\h\[\e[0;90m\]] \[\e[1;34m\]\w\[\e[0m\]
\[\e[0;90m\]└─\[\e[1;35m\]❯\[\e[0m\] '

© 2018 - 2026 vpslala

🌱 Powered by Hugo with theme Dream.