Linux ปรับแต่ง shell และ dotfiles
หลังใช้ Linux สักพัก คุณจะสะสม alias, function, การตั้งค่าเล็ก ๆ น้อย ๆ บทนี้จะสอนจัดระเบียบมัน ทำ prompt ให้มีประโยชน์ และเก็บทุกอย่างไว้บน Git เพื่อย้ายไปเครื่องใหม่ได้ทันที
จัดระเบียบ .bashrc
~/.bashrc มักบวมจนอ่านยาก แยกเป็นไฟล์ตามหน้าที่
# ~/.bashrc — เพิ่มท้ายไฟล์
for f in ~/.bashrc.d/*.sh; do
[ -r "$f" ] && source "$f"
donemkdir ~/.bashrc.d# ~/.bashrc.d/aliases.sh
alias ll='ls -alh'
alias la='ls -A'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
alias df='df -h'
alias du='du -h'
alias ports='sudo ss -tulpn'
alias myip='curl -s ifconfig.me; echo'
alias update='sudo apt update && sudo apt upgrade'
alias serve='python3 -m http.server'# ~/.bashrc.d/functions.sh
mkcd() { mkdir -p "$1" && cd "$1"; }
extract() {
case "$1" in
*.tar.gz|*.tgz) tar xzf "$1" ;;
*.tar.xz) tar xJf "$1" ;;
*.tar.bz2) tar xjf "$1" ;;
*.tar) tar xf "$1" ;;
*.zip) unzip "$1" ;;
*.gz) gunzip "$1" ;;
*) echo "ไม่รู้จักนามสกุลของ $1" ;;
esac
}
backup() { cp -a "$1" "$1.$(date +%Y%m%d-%H%M%S).bak"; }Ubuntu มี ~/.bash_aliases ที่ .bashrc เรียกให้อยู่แล้ว ถ้าอยากง่ายสุดใส่ alias ที่นั่นก็ได้
prompt (PS1) — ให้บอกข้อมูลที่มีประโยชน์
prompt เก็บใน $PS1 ปรับให้แสดง exit code, เวลา, git branch
# ~/.bashrc.d/prompt.sh
# แสดง git branch ถ้าอยู่ใน repo
git_branch() {
git branch 2>/dev/null | sed -n 's/^\* //p'
}
set_prompt() {
local last=$?
local reset='\[\e[0m\]'
local green='\[\e[32m\]'
local blue='\[\e[34m\]'
local yellow='\[\e[33m\]'
local red='\[\e[31m\]'
# สี user@host: เขียวถ้าคำสั่งก่อนหน้าสำเร็จ แดงถ้าล้มเหลว
local uh
if [ $last -eq 0 ]; then uh="${green}\u@\h"; else uh="${red}\u@\h"; fi
local branch
branch=$(git_branch)
[ -n "$branch" ] && branch=" ${yellow}(${branch})"
PS1="${uh}${reset}:${blue}\w${reset}${branch}${reset}\$ "
}
PROMPT_COMMAND=set_promptescape ที่ใช้บ่อยใน PS1: \u user, \h hostname, \w full path, \W แค่ชื่อโฟลเดอร์, \t เวลา, \$ เป็น $/# ตามสิทธิ์
สำคัญ: ครอบรหัสสี escape ด้วย \[ ... \] ไม่งั้น bash นับความยาวบรรทัดผิด แล้วการแก้บรรทัดเพี้ยน
bash-completion — Tab ที่ฉลาดขึ้น
sudo apt install bash-completionUbuntu เปิดให้อยู่แล้ว — ทำให้ Tab เติม option ของคำสั่ง (git <Tab> แสดง subcommand, systemctl <Tab> แสดง service, ssh <Tab> แสดง host จาก ~/.ssh/config)
ถ้าไม่ทำงาน เช็กว่า .bashrc มี
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
fiทางเลือก: ย้ายไป zsh
zsh เป็น shell ที่เข้ากันได้กับ bash เกือบหมด แต่มี completion ดีกว่า, แก้พิมพ์ผิด, glob ทรงพลัง
sudo apt install zsh
chsh -s $(which zsh) # เปลี่ยน login shell (มีผลตอน login ครั้งหน้า)ครั้งแรกจะถามตั้งค่า — เลือกอะไรก็ได้ แล้วติดตั้ง framework
Oh My Zsh — ชุดแต่ง zsh ยอดนิยม
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"แก้ ~/.zshrc: เลือก theme (ZSH_THEME="agnoster"), เปิด plugin
plugins=(git docker ssh-agent sudo z fzf)plugin เด็ด: zsh-autosuggestions (เดาคำสั่งจากประวัติ แสดงเป็นสีเทา กด → รับ), zsh-syntax-highlighting
starship — prompt สวย ใช้ได้ทั้ง bash/zsh/fish
ถ้าอยากได้แค่ prompt สวย ไม่อยากย้าย shell
curl -sS https://starship.rs/install.sh | sh
echo 'eval "$(starship init bash)"' >> ~/.bashrc # หรือ .zshrcแสดง git branch, สถานะ, เวอร์ชันภาษา, เวลา ให้อัตโนมัติ ปรับใน ~/.config/starship.toml
บนเซิร์ฟเวอร์ production ส่วนใหญ่คนไม่ย้าย shell —
bashเป็นมาตรฐาน ทุกคนอ่านออก สคริปต์ระบบพึ่งมัน ปรับแต่ง zsh/starship ไว้เครื่อง dev ของตัวเอง
dotfiles บน Git — ย้ายเครื่องในคำสั่งเดียว
เก็บไฟล์ตั้งค่าไว้ใน repo แล้ว symlink กลับเข้าที่ (บทที่ 12)
mkdir ~/dotfiles && cd ~/dotfiles
git init
# ย้ายไฟล์เข้ามา
mv ~/.bashrc bashrc
mv ~/.bash_aliases bash_aliases
mv ~/.gitconfig gitconfig
mv ~/.tmux.conf tmux.conf 2>/dev/null || true
# symlink กลับ
ln -sf ~/dotfiles/bashrc ~/.bashrc
ln -sf ~/dotfiles/bash_aliases ~/.bash_aliases
ln -sf ~/dotfiles/gitconfig ~/.gitconfig
git add -A && git commit -m "initial dotfiles"
git remote add origin git@github.com:somchai/dotfiles.git
git push -u origin mainสคริปต์ติดตั้งบนเครื่องใหม่
# ~/dotfiles/install.sh
#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")"
for f in bashrc bash_aliases gitconfig tmux.conf nanorc; do
[ -e "$f" ] || continue
target="$HOME/.$f"
[ -e "$target" ] && [ ! -L "$target" ] && mv "$target" "$target.orig"
ln -sfn "$PWD/$f" "$target"
echo "linked ~/.$f"
done
echo "เสร็จ — เปิดเทอร์มินัลใหม่ หรือ source ~/.bashrc"เครื่องใหม่
git clone git@github.com:somchai/dotfiles.git ~/dotfiles
~/dotfiles/install.shเครื่องมือช่วย: GNU Stow
sudo apt install stow
# โครงสร้าง: ~/dotfiles/bash/.bashrc, ~/dotfiles/git/.gitconfig
cd ~/dotfiles
stow bash git tmux # สร้าง symlink ให้อัตโนมัติตามโครงสร้าง
stow -D bash # ถอน.inputrc — ปรับพฤติกรรม readline
# ~/.inputrc
set completion-ignore-case on # Tab ไม่สนพิมพ์เล็กใหญ่
set show-all-if-ambiguous on # กด Tab ครั้งเดียวเห็นตัวเลือกทั้งหมด
set colored-stats on # ลงสีผลลัพธ์ completion
"\e[A": history-search-backward # ↑ ค้นประวัติที่ขึ้นต้นด้วยที่พิมพ์ไว้
"\e[B": history-search-forwardhistory-search-backward เปลี่ยน ↑ ให้ฉลาด: พิมพ์ ssh แล้วกด ↑ = ไล่เฉพาะคำสั่ง ssh ... เก่า ๆ
เรื่องที่คนมักพลาด
- แก้
.bashrcพังจนเปิด shell ไม่ได้ — สำรองcp ~/.bashrc ~/.bashrc.bakก่อนแก้; ถ้าพัง login อีก session แล้วbash --norcหรือกู้จาก dotfiles repo - สี PS1 ไม่ครอบ
\[ \]— bash นับความยาว prompt ผิด การกด ↑/↓ และแก้บรรทัดเพี้ยน chshเป็น zsh แล้วสคริปต์ระบบพัง — ไม่พัง เพราะสคริปต์มี shebang#!/bin/bashของตัวเอง; แต่~/.profileอาจไม่ถูกอ่าน ตรวจ.zprofile/.zshrc- ก๊อป
.bashrcจากอินเทอร์เน็ตทั้งไฟล์ — อาจมีของที่ไม่เข้ากับ Ubuntu หรือ override สิ่งที่ระบบตั้งไว้ อ่านก่อนใช้ เพิ่มทีละส่วน - dotfiles repo เก็บไฟล์ลับ — อย่า commit
~/.ssh/id_*,~/.aws/credentials,.env, history ที่มี token; ใส่.gitignore - symlink dotfiles แล้วลืมว่าแก้ที่ไหน — แก้ไฟล์ใน
~/dotfiles/(ไฟล์จริง) ไม่ใช่ตาม symlink;git statusใน~/dotfilesจะเห็นการเปลี่ยนแปลง - ติดตั้ง Oh My Zsh บนเซิร์ฟเวอร์ production ทุกเครื่อง — เพิ่ม startup time และ dependency ที่คนอื่นในทีมอาจไม่คุ้น เก็บไว้เครื่องตัวเอง
สรุป
- จัดระเบียบ
.bashrc: แยกไฟล์ใน~/.bashrc.d/(aliases, functions, prompt) หรือใช้~/.bash_aliases - prompt
PS1:\u@\h:\w\$+ git branch + exit code; ครอบสีด้วย\[ \] bash-completionทำให้ Tab เติม subcommand/service/host (Ubuntu เปิดให้แล้ว)- ทางเลือก:
zsh+ Oh My Zsh (zsh-autosuggestions), หรือstarship(prompt สวย ใช้กับ bash ได้) — เก็บไว้เครื่อง dev, เซิร์ฟเวอร์ใช้ bash - dotfiles บน Git:
~/dotfiles/+ symlink กลับ +install.sh→ เครื่องใหม่git clone+./install.sh; หรือใช้stow ~/.inputrc:completion-ignore-case on,↑=history-search-backward(ฉลาดขึ้น)- อย่า commit ไฟล์ลับเข้า dotfiles repo; สำรอง
.bashrcก่อนแก้
บทถัดไปเราจะรวมเทคนิคแก้ปัญหาที่เจอบ่อย — permission denied, command not found, disk full, เครื่องช้า, เน็ตล่ม