Guide ส่วนใหญ่เกี่ยว Bash History Shortcuts แสดงทุกตัวที่มี ปัญหาคือ ฉันใช้ Shortcut ครั้งเดียว แล้ว Glaze Over พยายามลองทุกความเป็นไปได้ แล้วไปทำงานต่อ และลืมหมด เหลือแค่ !! Trick ที่เรียนตอนเริ่มใช้ Bash
ส่วนใหญ่ไม่ได้ Committed to Memory
บทความนี้ Outline Shortcuts ที่ ใช้จริง ทุกวัน 基于 เนื้อหาบางส่วนจากหนังสือ Learn Bash the hard way (อ่าน Preview ได้)
เวลาคนเห็นฉันใช้ Shortcuts เหล่านี้ มักถาม "คุณทำอะไรนั่น!?" ไม่ต้องความพยายามหรือ Intelligence มาก แต้อยากให้เรียนจริง แนะนำใช้ทีละตัวต่อวันเป็นสัปดาห์ แล้วค่อยไปตัวถัดไป คุ้มค่าที่ใช้เวลาทำให้อยู่ใต้นิ้ว เวลาที่ประหยัดจะมากในระยะยาว
1. Shortcut "Last Argument": !$
ถ้าเอา Shortcut เพียงตัวเดียวจากบทความ ให้เป็นตัวนี้ มันแทน Last Argument ของ Last Command เข้า Line ของคุณ
พิจารณาสถานการณ์:
$ mv /path/to/wrongfile /some/other/place
mv: cannot stat '/path/to/wrongfile': No such file or directory
อ๊ะ ผมใส่ Filename wrongfile ผิด ควรเป็น rightfile
อาจ Retype Last Command แทน wrongfile ด้วย rightfile แทนได้ แต่พิมพ์:
$ mv /path/to/rightfile !$
mv /path/to/rightfile /some/other/place
คำสั่งจะทำงาน
มีวิธีอื่นทำให้เหมือนกันใน Bash ด้วย Shortcuts แต่ Trick นี้ Reuse Last Argument ของ Last Command เป็นที่ใช้บ่อยสุด
2. Shortcut "nth Argument": !:2
เคยทำแบบนี้ไหม?
$ tar -cvf afolder afolder.tar
tar: failed to open
เหมือนหลายคน ผมสลับ Arguments ของ tar (และ ln) ผิดบ่อยกว่าที่จะยอมรับ

ตอนสลับ Arguments แบบนั้น รัน:
$ !:0 !:1 !:3 !:2
tar -cvf afolder.tar afolder
ชื่อเสียงคุณรอด
Items ของ Last Command Zero-indexed แทนได้ด้วย Number หลัง !:
ชัดเจน ใช้ Reuse Specific Arguments จาก Last Command แทน All ได้
3. Shortcut "All Arguments": !:1-$
ลองจินตนาการ รัน Command แบบ:
$ grep '(ping|pong)' afile
Arguments ถูกต้อง แต่อยาก Match ping หรือ pong ในไฟล์ ใช้ grep แทน egrep
เริ่มพิมพ์ egrep แต่ไม่อยาก Retype Arguments อื่น ใช้ Shortcut !:1$ ขอ Arguments ทั้งหมดจาก Command ก่อนหน้า ตัวที่ 2 (Zero-indexed) ถึงตัวสุดท้าย (แทนด้วย $):
$ egrep !:1-$
egrep '(ping|pong)' afile
ping
ไม่บังคับต้อง 1-$ เลือก Subset ได้ เช่น 1-2 หรือ 3-9 (หากมี Arguments มากพอใน Command ก่อน)
4. Shortcut "Last but n": !-2:$
Shortcuts ข้างบนดีตอนรู้ทันทีว่าจะแก้ Last Command ยังไง แต่บ่อยครั้งรัน Commands หลัง Original Command ทำให้ Last Command ไม่ใช่ที่ต้องการอ้างอิง
ตัวอย่าง ใช้ mv ตัวอย่างก่อน หาก Follow Up Mistake ด้วย ls Check Folder Contents:
$ mv /path/to/wrongfile /some/other/place
mv: cannot stat '/path/to/wrongfile': No such file or directory
$ ls /path/to/
rightfile
ตอนนี้ไม่ใช้ !$ ได้
กรณีนี้ ใส่ -n: (ที่ n คือ จำนวน Commands ย้อนกลับใน History) หลัง ! เพื่อดึง Last Argument จาก Command เก่า:
$ mv /path/to/rightfile !-2:$
mv /path/to/rightfile /some/other/place
อีกครั้ง จะประหลาดใจได้ว่าต้องใช้บ่อยแค่ไหน
5. Shortcut "Get Me The Folder": !$:h
ดูไม่น่าสนใจ แต่ใช้หลายสิบครั้งต่อวัน
ลองจินตนาการ รัน Command แบบ:
$ tar -cvf system.tar /etc/system
tar: /etc/system: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors.
สิ่งแรกอยากทำคือ ไป Folder /etc ดูว่ามีอะไร และหาว่าผิดที่ไหน
ทำได้ทีเดียวด้วย:
$ cd !$:h
cd /etc
Shortcut นี้บอกว่า: "เอา Last Argument ของ Last Command (/etc/system) ตัด Filename ส่วนท้ายทิ้ง เหลือแค่ /etc"
6. Shortcut "The Current Line": !#:1
หลายปี ผมเคยสงสัยว่า Reference Argument บน Current Line ก่อนได้ไหม จนค้นหาและเรียนรู้ มาก่อนแล้วควรทำ นิยมใช้สำหรับ Backup Files:
$ cp /path/to/some/file !#:1.bak
cp /path/to/some/file /path/to/some/file.bak
แต่ไว้ใต้นิ้วแล้วเป็น Alternative เร็วสำหรับ …
7. Shortcut "Search and Replace": !!:gs
Shortcut นี้ Search ทั่ว Referenced Command แทนที่สิ่งใน / สองตัวแรก ด้วยสิ่งใน / สองตัวถัดไป
สมมติอยากบอกว่า Key s ของผมเสีย พิมพ์ f แทน:
$ echo my f key doef not work
my f key doef not work
ตระหนักว่าพิมพ์ f ผิด แทน f ทั้งหมดด้วย s พิมพ์:
$ !!:gs/f /s /
echo my s key does not work
my s key does not work
ไม่ได้ทำงานแค่ Single Characters แทนคำหรือประโยคได้:
$ !!:gs/does/did/
echo my s key did not work
my s key did not work
ลองทดสอบ
แสดงว่า Shortcuts รวมกันได้ ยิงโจทย์ Toenail Clippings นี้ Output อะไร?
$ ping !#:0:gs/i/o
$ vi /tmp/!:0.txt
$ ls !$:h
$ cd !-2:h
$ touch !$!-3:$ !! !$.txt
$ cat !:1-$
สรุป
Bash เป็นแหล่ง Shortcuts ที่ประณีตสำหรับ Command-line User ประจำวัน แม้ Tips และ Tricks หลายพันอย่าง 7 ตัวนี้คือโปรดที่ใช้บ่อย
หากอยากลึกลงไปใน Bash อ่านหนังสือ Learn Bash the hard way หรือ Online Course Master the Bash Shell
บทความนี้โพสต์ต้นทางบน Blog ของ Ian Zwischenzugs.com ใช้ด้วย Permission