4.7 KiB
Custom Bash Terminal
Ghi chú cấu hình terminal Bash trên môi trường dev để có prompt ngắn gọn, Git status, màu sắc và auto-completion/autosuggestion.
1. Kiểm tra shell đang sử dụng
echo $SHELL
ps -p $$ -o comm=
Kết quả hiện tại:
/bin/bash
bash
→ Đang sử dụng Bash.
2. Custom Bash Prompt
Mục tiêu
Thay prompt dài mặc định:
jovyan@coder-huythai855-dev:~/projects/ivp-core$
thành:
ivp-core customer/phuquoc ✓ ❯
Quy ước:
- Folder hiện tại → cyan
- Git branch → tím
✓→ xanh, Git working tree clean*→ vàng, có thay đổi chưa commit❯→ xanh
Ngoài Git repository:
projects ❯
Cấu hình
Mở:
nano ~/.bashrc
Thêm:
# ===== Custom Bash Prompt =====
update_prompt() {
local branch
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
branch=$(git branch --show-current 2>/dev/null)
# Detached HEAD
if [ -z "$branch" ]; then
branch=$(git rev-parse --short HEAD 2>/dev/null)
fi
if [ -z "$(git status --porcelain 2>/dev/null)" ]; then
# Git clean
PS1='\[\e[36m\]\W\[\e[0m\] \[\e[35m\]'"$branch"'\[\e[0m\] \[\e[32m\]✓\[\e[0m\] \[\e[32m\]❯\[\e[0m\] '
else
# Git modified
PS1='\[\e[36m\]\W\[\e[0m\] \[\e[35m\]'"$branch"'\[\e[0m\] \[\e[33m\]*\[\e[0m\] \[\e[32m\]❯\[\e[0m\] '
fi
else
# Outside Git repo
PS1='\[\e[36m\]\W\[\e[0m\] \[\e[32m\]❯\[\e[0m\] '
fi
}
PROMPT_COMMAND=update_prompt
Reload:
source ~/.bashrc
3. Bash Completion
Cài:
sudo apt update
sudo apt install -y bash-completion
Thêm vào ~/.bashrc:
# Enable bash completion
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
Reload:
source ~/.bashrc
Ví dụ:
git sta<Tab>
→ autocomplete thành:
git status
Hoặc:
git s<Tab><Tab>
→ hiển thị các Git subcommand bắt đầu bằng s.
4. Autosuggestion với ble.sh
Mục tiêu là có suggestion dựa trên history.
Ví dụ đã từng chạy:
git status
Lần sau chỉ cần gõ:
git s
terminal sẽ gợi ý phần còn lại của:
git status
Cài ble.sh
Clone:
git clone --recursive --depth 1 https://github.com/akinomyoga/ble.sh.git ~/.local/share/blesh
ble.sh cần gawk để build:
sudo apt update
sudo apt install -y gawk
Build:
make -C ~/.local/share/blesh
Kiểm tra:
ls ~/.local/share/blesh/out/ble.sh
Thêm cuối ~/.bashrc:
# ble.sh - autosuggestion + syntax highlighting
source ~/.local/share/blesh/out/ble.sh
Reload:
source ~/.bashrc
Cách sử dụng
→→ accept autosuggestionTab→ shell completion↑ / ↓→ command history
Khuyến nghị giữ cách hoạt động này thay vì override Tab.
5. Lưu ý về PATH
Trong quá trình sửa .bashrc, có thể vô tình xóa dòng cấu hình PATH.
Triệu chứng:
codex
bash: codex: command not found
[ble: exit 127]
exit 127 nghĩa là command not found, không phải ble.sh bị lỗi.
Kiểm tra PATH:
echo $PATH
Kiểm tra Codex:
which codex
command -v codex
Tìm binary:
find ~/.local ~/.npm ~/ -type f -name codex 2>/dev/null | head -20
Nếu Codex được cài bằng npm:
npm config get prefix
Ví dụ npm prefix là:
/home/jovyan/.npm-global
thì cần:
export PATH="$HOME/.npm-global/bin:$PATH"
Nếu binary nằm trong ~/.local/bin:
export PATH="$HOME/.local/bin:$PATH"
Kiểm tra các cấu hình PATH hiện có:
grep -n "PATH" ~/.bashrc
và:
grep -n "PATH" ~/.profile ~/.bash_profile ~/.bash_login 2>/dev/null
Không nên overwrite PATH kiểu:
export PATH="/usr/local/bin:/usr/bin:/bin"
vì sẽ làm mất các path đã có.
Nên prepend/append:
export PATH="$HOME/.local/bin:$PATH"
hoặc:
export PATH="$HOME/.npm-global/bin:$PATH"
6. Cấu hình mong muốn cuối cùng
Terminal:
ivp-worker customer/phuquoc ✓ ❯
Khi source code thay đổi:
ivp-worker customer/phuquoc * ❯
Có:
- Bash prompt ngắn gọn
- Màu sắc
- Git branch
- Git clean/dirty status
- Bash completion
- Git completion
- Command history
- Autosuggestion qua ble.sh
- Giữ nguyên PATH cho các CLI như
codex