ในโพสต์ก่อนหน้าเกี่ยวกับ Setup Ubuntu ด้วย Bash Scripts ผมกล่าวถึง "Magic ของ .bashrc" แตไม่ได้รายละเอียดพอ — บทความนี้จะเจาะลึก Aliases, Functions, Prompt Configs ที่ช่วย Workflow ให้มีประสิทธิภาพมากขึ้นด้วย Keystrokes น้อยลง
Config ปัจจุบันของผมลด Keystrokes ลง 50%+ — มาดูตัวอย่าง
Bash Aliases
.bashrc ที่เขียนดี ประหยัด Keystrokes มาก — ใช้ Aliases (String ขยายเป็นคำสั่งยาว)
ตัวอย่าง: Copy Files
# Copy Recursive (-r) และ Verbose (-v) เสมอ
alias cp='cp -rv'
alias กำหนด String ที่พิมพ์ → ขยายเป็นคำสั่งเต็ม — Override คำสั่งเดิมได้
ปกติ cp คัดลอกไฟล์อย่างเดียว ไม่คัดลอกโฟลเดอร์ และ Silent — Alias นี้ทำให้ cp คัดลอกโฟลเดอร์ได้ และบอกว่าคัดลอกอะไรไปไหน (ไม่ต้อง cd/ls เช็คซ้ำ)
Aliases เพิ่มเติมสำหรับ Flags พื้นฐาน
# ls: สี, Almost-all (ไม่รวม . ..), Columns, Class Indicators
alias ls='ls --color=auto -ACF'
# ll: Long Listing, All Hidden, Class Indicators
alias ll='ls --color=auto -alF'
# mv: Verbose
alias mv='mv -v'
# mkdir: Parents + Verbose
alias mkdir='mkdir -pv'
# wget: Continue Partial Download
alias wget='wget -c'
Aliases สำหรับ Python Environments
alias pym='python3 manage.py'
alias mkenv='python3 -m venv env'
alias startenv='source env/bin/activate && which python3'
alias stopenv='deactivate'
แรงบันดาลใจเพิ่มเติม: บทความอื่นๆ ที่มี Aliases เยอะกว่านี้
Bash Functions
Aliases Static — ขยายคงที่ ต้องการ Arguments → ต้องใช้ Functions
Function: cd + ls อัตโนมัติ
function cd() {
builtin cd "$1"
ls -ACF
}
พิมพ์ cd แค่ครั้งเดียว → เข้าโฟลเดอร์ + แสดงเนื้อหา — builtin จำเป็นเพื่อ Override Built-in cd
Function: Install/Upgrade Hugo
function gethugo() {
wget -q -P tmp/ https://github.com/gohugoio/hugo/releases/download/v"$@"/hugo_extended_"$@"_Linux-64bit.tar.gz
tar xf tmp/hugo_extended_"$@"_Linux-64bit.tar.gz -C tmp/
sudo mv -f tmp/hugo /usr/local/bin/
rm -rf tmp/
hugo version
}
$@ = ทุก Arguments — รัน gethugo 0.57.2 ดาวน์โหลดเวอร์ชันนั้น
Function: Install/Upgrade Golang
function getgolang() {
sudo rm -rf /usr/local/go
wget -q -P tmp/ https://dl.google.com/go/go"$@".linux-amd64.tar.gz
sudo tar -C /usr/local -xzf tmp/go"$@".linux-amd64.tar.gz
rm -rf tmp/
go version
}
Function: Add GitLab Remote
function glab() {
git remote set-url origin --add git@gitlab.com:"$@"/"${PWD##*/}".git
git remote -v
}
glab username → Add GitLab Remote URL สำหรับ Repo ปัจจุบัน — Push ไป GitLab สร้าง Private Repo เอง (Backup ดี!)
Functions จำกัดได้แค่จินตนาการ Scripting — ทุกอย่างที่ทำบ่อยใน Terminal สร้าง Function ได้!
Bash Prompt
นอกจากเนื้อหาโฟลเดอร์ Full Path และ Git Branch ก็มีประโยชน์ — ใช้สีแยกส่วนให้อ่านง่าย
ตั้งค่า Prompt ใน .bashrc
# Color Names (Escape Sequences)
txtcyn='\[\e[0;96m\]' # Cyan
txtpur='\[\e[0;35m\]' # Purple
txtwht='\[\e[0;37m\]' # White
txtrst='\[\e[0m\]' # Reset
# Assign Colors to Prompt Parts
pathC="${txtcyn}"
gitC="${txtpur}"
pointerC="${txtwht}"
normalC="${txtrst}"
# Git Branch Function
gitBranch() {
git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
# Build PS1
export PS1="${pathC}\w ${gitC}\$(gitBranch) ${pointerC}\$${normalC} "
ผลลัพธ์:
~/github/myrepo (master) $
แยกส่วน:
\w= Current Working Directory\$(gitBranch)= เรียก Function แสดง Git Branch\$= $ (User) หรือ # (Root)
Escape Sequences อื่นๆ: เวลา, วันที่, Username, Hostname, ฯลฯ — Prompt ปรับแต่งได้ไม่มีขีดจำกัด
ตัวเลือก Prompt เพิ่มเติม
สำหรับคนไม่อยากเฉื่อย: Username + Time (HH:MM:SS)
export PS1="${userC}\u ${normalC}at \t >"
user at 09:35:55 >
สำหรับคนอยากรู้ตำแหน่ง: Full Path บรรทัดแยก + Username
export PS1="${pathC}\w${normalC}\n\u:"
~/github/myrepo
user:
Minimalist
export PS1=">"
>
Escape Sequences พื้นฐานสร้าง Prompt ใช้งานได้จริง — ผสาน Functions เข้าไป (เช่น Git Branch) จะซับซ้อนขึ้น ว่าจะช่วยหรือรบกวน Productivity ขึ้นอยู่กับแต่ละคน
Fancy Prompts เยอะมากในเน็ต — ผมไม่รวมไว้เพราะอาจทำให้คุณใช้เวลาหลายชั่วโมงเล่น Prompt แทนงานจริง! 😄
หวังว่า .bashrc นี้จะช่วยคืน Keystrokes ให้คุณ — ใช้ความสามารถที่ได้มาทำดีๆ นะครับ!