Files
helpers/BLESH_BASH.md

335 lines
4.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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
```bash
echo $SHELL
ps -p $$ -o comm=
```
Kết quả hiện tại:
```text
/bin/bash
bash
```
→ Đang sử dụng **Bash**.
---
## 2. Custom Bash Prompt
### Mục tiêu
Thay prompt dài mặc định:
```text
jovyan@coder-huythai855-dev:~/projects/ivp-core$
```
thành:
```text
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:
```text
projects ❯
```
### Cấu hình
Mở:
```bash
nano ~/.bashrc
```
Thêm:
```bash
# ===== 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:
```bash
source ~/.bashrc
```
---
## 3. Bash Completion
Cài:
```bash
sudo apt update
sudo apt install -y bash-completion
```
Thêm vào `~/.bashrc`:
```bash
# Enable bash completion
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
```
Reload:
```bash
source ~/.bashrc
```
Ví dụ:
```text
git sta<Tab>
```
→ autocomplete thành:
```text
git status
```
Hoặc:
```text
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:
```bash
git status
```
Lần sau chỉ cần gõ:
```text
git s
```
terminal sẽ gợi ý phần còn lại của:
```text
git status
```
### Cài ble.sh
Clone:
```bash
git clone --recursive --depth 1 https://github.com/akinomyoga/ble.sh.git ~/.local/share/blesh
```
ble.sh cần `gawk` để build:
```bash
sudo apt update
sudo apt install -y gawk
```
Build:
```bash
make -C ~/.local/share/blesh
```
Kiểm tra:
```bash
ls ~/.local/share/blesh/out/ble.sh
```
Thêm cuối `~/.bashrc`:
```bash
# ble.sh - autosuggestion + syntax highlighting
source ~/.local/share/blesh/out/ble.sh
```
Reload:
```bash
source ~/.bashrc
```
### Cách sử dụng
* `→` → accept autosuggestion
* `Tab` → 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:
```text
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:
```bash
echo $PATH
```
Kiểm tra Codex:
```bash
which codex
command -v codex
```
Tìm binary:
```bash
find ~/.local ~/.npm ~/ -type f -name codex 2>/dev/null | head -20
```
Nếu Codex được cài bằng npm:
```bash
npm config get prefix
```
Ví dụ npm prefix là:
```text
/home/jovyan/.npm-global
```
thì cần:
```bash
export PATH="$HOME/.npm-global/bin:$PATH"
```
Nếu binary nằm trong `~/.local/bin`:
```bash
export PATH="$HOME/.local/bin:$PATH"
```
Kiểm tra các cấu hình PATH hiện có:
```bash
grep -n "PATH" ~/.bashrc
```
và:
```bash
grep -n "PATH" ~/.profile ~/.bash_profile ~/.bash_login 2>/dev/null
```
**Không nên overwrite PATH** kiểu:
```bash
export PATH="/usr/local/bin:/usr/bin:/bin"
```
vì sẽ làm mất các path đã có.
Nên prepend/append:
```bash
export PATH="$HOME/.local/bin:$PATH"
```
hoặc:
```bash
export PATH="$HOME/.npm-global/bin:$PATH"
```
---
## 6. Cấu hình mong muốn cuối cùng
Terminal:
```text
ivp-worker customer/phuquoc ✓ ❯
```
Khi source code thay đổi:
```text
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`