blog.dopana

Back

Terminal là công cụ chính của developer. Tối ưu nó — tối ưu cả ngày làm việc.

Shell — Chọn Đúng#

Zsh — Mặc Định Trên macOS#

Zsh là shell mặc định trên macOS và hầu hết Linux distro mới.

Oh My Zsh — framework quản lý plugin và theme:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
bash

Plugin nên có (trong ~/.zshrc):

plugins=(
  git # Git aliases: gst, gco, gaa, ...
  zsh-autosuggestions # Gợi ý lệnh từ history
  zsh-syntax-highlighting # Tô màu lệnh
  docker # Docker completion
  npm # Npm completion
  history # Tìm kiếm history
)
bash

Fish — User-Friendly#

Fish có syntax highlighting, autosuggestion, và web config ngay từ đầu:

# Cài Fish
brew install fish

# Mở web config
fish_config

# Prompt đẹp
fisher install matchai/spacefish
bash

tmux — Terminal Multiplexer#

tmux cho phép nhiều terminal trong một cửa sổ, session tồn tại cả khi mất kết nối SSH.

Basic Commands#

# Session
tmux new -s myproject # Tạo session mới
tmux ls # List session
tmux attach -t myproject # Attach vào session
tmux kill-session -t myproject

# Trong tmux (prefix: Ctrl+b)
Ctrl+b % # Split vertical
Ctrl+b "       # Split horizontal
Ctrl+b arrow # Di chuyển giữa các pane
Ctrl+b c # New window
Ctrl+b n/p # Window tiếp/trước
Ctrl+b d # Detach (session vẫn chạy)
Ctrl+b [ # Scroll mode
bash

Cấu Hình (~/.tmux.conf)#

# Mouse support
set -g mouse on

# Tăng history
set -g history-limit 50000

# Prefix đổi thành Ctrl+a (dễ với tay)
set -g prefix C-a

# 256 color
set -g default-terminal "screen-256color"
bash

Alias — Tiết Kiệm Gõ Phím#

History Tricks#

# Tìm kiếm history
Ctrl+r # Search history (fuzzy)
history | grep docker

# Chạy lại lệnh
!! # Lệnh cuối
!$ # Tham số cuối của lệnh trước
!docker # Lệnh docker gần nhất
!100 # Chạy lệnh số 100 trong history

# Sửa lệnh trước
^old^new # Thay thế
bash

CLI Tools Siêu Hữu Ích#

File & Navigation#

Process & System#

# htop — thay thế top
htop

# duf — thay thế df
duf

# procs — thay thế ps
procs

# bottom (btm) — thay thế htop + nhiều hơn
btm
bash

Network#

# httpie — thay thế curl cho API
http GET https://api.example.com/users
http POST https://api.example.com/users name=Alice

# ngrok — tunnel localhost ra internet
ngrok http 3000

# dog — thay thế dig
dog example.com
bash

JSON#

# jq — xử lý JSON trong terminal
cat data.json | jq '.users[] | {name, email}'
curl api.example.com/users | jq '.[0:3]'

# fx — JSON viewer tương tác
curl api.example.com/users | fx
bash

FZF — Fuzzy Finder#

FZF cho phép tìm kiếm mờ trong terminal:

# Cài
brew install fzf

# Tìm file và mở
vim $(fzf)

# Git branch switcher
gco $(git branch | fzf | tr -d ' *')

# Docker container kill
docker stop $(docker ps | fzf | awk '{print $1}')

# History search
history | fzf
bash

Keybindings Custom#

# VS Code style trong terminal
bindkey "^P" history-search-backward
bindkey "^N" history-search-forward
bindkey "^A" beginning-of-line
bindkey "^E" end-of-line
bindkey "^K" kill-line
bindkey "^U" kill-whole-line
bindkey "^W" backward-kill-word
bash

Workflow — Terminal IDE#

tmux session "myproject"
├── Window 1: Editor (Neovim)
│   ├── Pane 1: Code
│   └── Pane 2: Terminal
├── Window 2: Server
│   └── Pane 1: npm run dev
└── Window 3: Git
    └── Pane 1: git log, git status
plaintext

Cài Đặt Nhanh#

Kết Luận#

Terminal productivity là đầu tư một lần — lợi ích mỗi ngày. Bắt đầu với:

  1. Oh My Zsh + plugins autosuggestions + syntax-highlighting
  2. tmux cho session management
  3. Alias cho git và docker
  4. fzf cho fuzzy finding
  5. fd + rg + bat — thay thế find/grep/cat

Tài liệu tham khảo#