Tùy biến Terminal trên macOS: Từ iTerm2 đến Zsh theme
Mục lục bài viết
1. Tại sao cần tùy biến Terminal?
Terminal mặc định trên macOS (Zsh) khá đơn giản: thiếu syntax highlighting, autocomplete, và theme đẹp. Một terminal được tùy biến tốt có thể tăng tốc thao tác hàng ngày lên 30-50% cho lập trình viên.
Lợi ích chính:
- Syntax highlighting: Lệnh sai hiện màu đỏ ngay khi gõ.
- Autosuggestions: Gợi ý lệnh từ lịch sử khi gõ.
- Git integration: Hiển thị branch, status ngay trên prompt.
- Theme đẹp: Dễ nhìn, giảm mỏi mắt.
- Productivity: Aliases, plugins, shortcuts.
2. Cài iTerm2: Terminal thay thế
Bước 1: Tải iTerm2
Tải từ iterm2.com hoặc brew install --cask iterm2.
Bước 2: Cấu hình cơ bản
- Preferences > Profiles > Default > Colors: Chọn theme (Solarized Dark, Dracula).
- Preferences > Profiles > Text: Font size 14-16pt, 'Use a different font for non-ASCII'.
- Preferences > Profiles > Window: Transparency 10-20%.
- Preferences > Profiles > Keys: 'Left Option key acts as +Esc'.
Bước 3: Hotkey window
Preferences > Keys > Hotkey > 'Show/hide all windows with a system-wide hotkey'.
Set Option+Space: terminal ẩn/hiện từ bất kỳ app nào.
3. Oh My Zsh và Powerlevel10k
Cài Oh My Zsh:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Cài theme Powerlevel10k:
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
Set ZSH_THEME="powerlevel10k/powerlevel10k" trong ~/.zshrc.
Cấu hình:
Sau khi restart terminal, Powerlevel10k chạy wizard hỏi bạn style prompt.
Chọn: Lean, Classic, Rainbow, Pure,...
Chạy lại wizard bất kỳ lúc nào:
p10k configure
Các element trong prompt:
- Time, OS icon, username, hostname.
- Current directory (truncated thông minh).
- Git branch, status (dirty/clean, ahead/behind).
- Node.js/Python/Python version (nếu có .nvmrc/.python-version).
- Command execution time.
4. Plugins thiết yếu
Mở ~/.zshrc, tìm plugins=(git) và thay bằng:
```zsh
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
zsh-completions
colored-man-pages
macos
vscode
docker
docker-compose
npm
node
brew
history
copyfile
copypath
web-search
)
```
Cài plugins bổ sung:
zsh-autosuggestions:git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestionszsh-syntax-highlighting:git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
5. Aliases và Functions hữu ích
Thêm vào ~/.zshrc:
```zsh
# Navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias ~='cd ~'
# LS enhancements
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'
# Git
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline --graph --all'
# Safety
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Common apps
alias vim='nvim'
alias cat='bat'
alias top='htop'
alias du='ncdu'
alias ps='procs'
# IP address
alias myip='curl -s ifconfig.me'
# Clear
alias c='clear'
# Quick edit
alias zshrc='code ~/.zshrc'
alias reload='source ~/.zshrc'
```
eza (brew install eza), cat bằng bat, top bằng htop: preview hiện đại hơn.6. Font Nerd Font và các mẹo cuối
Nerd Font: cần thiết cho icon trong prompt.
brew install --cask font-meslo-lg-nerd-font
Sau đó set iTerm2 font thành 'MesloLGS NF' (Size 14).
Color themes:
- Dracula: phổ biến, dark theme, contrast tốt.
- Catppuccin: pastel, dễ chịu cho mắt.
- Tokyo Night: dark blue, hiện đại.
- Solarized Dark: classic, ổn định.
Các mẹo khác:
- Natural text editing: Preferences > Profiles > Keys > Presets > Natural Text Editing.
- Enable character repeats:
defaults write -g ApplePressAndHoldEnabled -bool false(cho phép giữ phím lặp). - Dùng
tmux(terminal multiplexer) để quản lý nhiều session.
🙋 Câu hỏi thường gặp
iTerm2 có miễn phí không?
Có. iTerm2 hoàn toàn miễn phí, mã nguồn mở. Là bản nâng cấp mạnh mẽ của Terminal.app, được phát triển bởi George Nachman.
Cần cài Nerd Font không?
Có, nếu dùng Powerlevel10k hoặc bất kỳ theme nào có icon. Nếu không cài, icon hiện thành ô vuông (tofu). MesloLGS Nerd Font là lựa chọn phổ biến.
Oh My Zsh có làm chậm terminal không?
Có một chút (200-500ms khởi động).
Nếu khởi động chậm: disable plugins không dùng, dùng Powerlevel10k (nhanh nhất).
Kiểm tra: zsh -i -c '' và time zsh -i -c exit.