บทความนี้เขียนโดย SathiyaMoorthy
บทความนี้เป็นส่วนหนึ่งของชุด Vi / Vim Tips and Tricks Series ในฐานะ Linux Sysadmin หรือ Programmer คุณอาจทำงานซ้ำๆ ขณะเขียน Bash Shell Script:
- เพิ่ม File Header
- เพิ่ม Function/Frame Comment
- รวม Default Code Snippet
- ตรวจสอบ Syntax (Syntax Check)
- อ่าน Documentation เกี่ยวกับ Function
- แปลง Code Block เป็น Comment และกลับกัน
Vim Plugin bash-Support ให้วิธีที่ง่ายที่สุดในการทำทั้งหมดข้างต้น ประหยัดเวลาและ Keystrokes มาก
Plugin นี้เขียนโดย Fritz Mehner อธิบายวัตถุประสงค์ว่า: "Write and run BASH-scripts using menus and hotkeys."
บทความนี้อธิบายการติดตั้ง Plugin ใน 3 ขั้นตอนง่ายๆ และ 8 ฟีเจอร์อันทรงพลัง
3 ขั้นตอนติดตั้ง Plugin bash-support
ขั้นตอนที่ 1: ดาวน์โหลด Plugin bash-support
ดาวน์โหลด Plugin จากเว็บไซต์ vim.org
$ cd /usr/src $ wget -O bash-support.zip https://www.vim.org/scripts/download_script.php?src_id=9890
ขั้นตอนที่ 2: ติดตั้ง Vim Plugin bash-support
$ mkdir ~/.vim # ถ้ายังไม่มี Directory นี้ $ cd ~/.vim $ unzip /usr/src/bash-support.zip
ขั้นตอนที่ 3: เปิดใช้งาน Plugin ใน ~/.vimrc
เพิ่มบรรทัดต่อไปนี้ใน ~/.vimrc เพื่อเปิดใช้งาน Plugin สำหรับ Vim Editor
$ vim ~/.vimrc filetype plugin on
8 ฟีเจอร์อันทรงพลังของ Bash Vim Plugin
Feature 1: เพิ่ม Header อัตโนมัติให้ไฟล์ *.sh
เมื่อเปิดไฟล์นามสกุล .sh จะเปิดพร้อม Header ตามด้านล่าง และวาง Cursor ไว้ที่ Field Description ใน Insert Mode
#!/bin/bash #============================================================ # # FILE: myscript.sh # # USAGE: ./myscript.sh # # DESCRIPTION: # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: --- # AUTHOR: (), # COMPANY: # VERSION: 1.0 # CREATED: 02/14/09 15:42:08 IST # REVISION: --- #============================================================
เพื่อเปลี่ยนค่า Default ของ AUTHOR และ COMPANY ให้เพิ่มบรรทัดต่อไปนี้ใน ~/.vimrc
let g:BASH_AuthorName = 'SathiyaMoorthy' let g:BASH_Email = 'subscribe@thegeekstuff.com' let g:BASH_Company = 'Open Source Corporation'
ตอนนี้เมื่อสร้าง Bash Script ใหม่ จะแสดงค่าที่ปรับแล้วสำหรับ AUTHOR และ COMPANY
#!/bin/bash #============================================================ # # FILE: myscript.sh # # USAGE: ./myscript.sh # # DESCRIPTION: # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: --- # AUTHOR: SathiyaMoorthy (), subscribe@thegeekstuff.com # COMPANY: Open Source Corporation # VERSION: 1.0 # CREATED: 02/14/09 15:39:58 IST # REVISION: --- #============================================================
หมายเหตุ: เพื่อเพิ่ม Custom Fields ใน Header ให้แก้ไขไฟล์ ~/.vim/perl-support/templates/bash-file-header และเพิ่ม Custom Field ของคุณเอง
Feature 2: เพิ่ม Bash Function ด้วย \sfu
เพื่อเขียน Subroutine ให้พิมพ์ \sfu ใน Normal Mode จะ Prompt ขอ Function Name (ดู Fig 1) และแทรก Subroutine พร้อม Default Function Content (ดู Fig 2)

Fig 1: พิมพ์ \sfu เพื่อเพิ่ม Bash Function ภายใน Shell Script

Fig 2: Bash Function ถูกเพิ่มอัตโนมัติภายใน Shell Script
Feature 3: แทรก Function Header ด้วย \cfu
เพื่อแทรก Function Header ให้พิมพ์ \cfu ใน Normal Mode จะแสดง Comment ตาม Fig 3

Fig 3: พิมพ์ \cfu เพื่อแทรก Function Header ภายใน Shell Script
Feature 4: เพิ่ม Frame Comment ด้วย \cfr
เพื่อเพิ่ม Frame Comment ให้พิมพ์ \cfr ใน Normal Mode จะได้ Comment ที่จัดรูปแบบแล้วตาม Figure 4

Fig 4: พิมพ์ \cfr เพื่อแทรก Frame Comment ภายใน Shell Script
Feature 5: แทรก Bash Statements ภายใน Shell Script
Shortcut Keys สำหรับแทรก Statements:
- \sc case in … esac
- \sl elif then
- \sf for in do done
- \sfo for ((…)) do done
- \si if then fi
- \sie if then else fi
- \ss select in do done
- \st until do done
- \sw while do done
- \sfu function
- \se echo -e "\n"
- \sp printf "\n"
ตัวอย่าง: แทรก Case Statement ภายใน Shell Script อัตโนมัติ
\sc จะแทรก Case Statements และวาง Cursor ถัดจาก Case Statement ใน INSERT Mode ตาม Figure 5 เหมือนกัน สามารถใช้ Shortcut Keystrokes ทั้งหมดที่ระบุใน Table 1 ได้

Fig 5: พิมพ์ \sc เพื่อแทรก Case Statement ภายใน Bash Shell Script
Feature 6: แทรก Predefined Code-snippet ด้วย \nr
Code Snippets สามารถอ่าน/เขียนได้ด้วย \nr และ \nw ตามลำดับ Plugin มาพร้อม Pre-defined Code Snippets หลายตัว ที่สามารถแทรกได้ ด้านล่างคือ Default Code Snippets ที่มาพร้อม Plugin
$ ls -1 ~/.vim/bash-support/codesnippets/ assert basename+pathname basename-function check-number-of-command-line-arguments create-tempfile create-tempfile-with-trap free-software-comment read-and-split-into-array timestamp usage-and-command-line-arguments.noindent use-file-descriptor-read use-file-descriptor-write well-behaved-script
เพื่อรวม Code Snippet check-number-of-command-line-arguments ให้กด \nr จะ Prompt ขอ File Name ให้ใส่ชื่อว่า check-number-of-command-line-arguments โค้ดต่อไปนี้จะถูกแทรกอัตโนมัติใน Shell Script
#-----------------------------------------------------------------------
# Check number of command line arguments
#-----------------------------------------------------------------------
if [ $# -lt 1 ]
then
echo -e "\n\tUsage: ${0##/*/} File\n"
exit 1
fi
หมายเหตุ: คุณสามารถกำหนด Code Snippets ของตัวเองและวางไว้ที่ ~/.vim/bash-support/codesnippets/ สร้าง Code Snippets ของตัวเองจาก Code ที่มีอยู่ได้: Select ส่วนของ Code ที่ต้องการทำเป็น Snippet กด \nw ใส่ File Name ตั้งแต่ครั้งต่อไป พิมพ์ \nr และ File Name เพื่อดึง Custom Code Snippet ของคุณ
Feature 7: ได้ Quick Help เกี่ยวกับ Bash Builtin Commands
เมื่อต้องการอ่าน Help Page สำหรับ Bash Builtins ให้ใช้ \hh เมื่อ Cursor อยู่ที่คำสั่ง
ในตัวอย่างต่อไปนี้ (Fig 6) คำสั่ง Built-in read ถูก Select และพิมพ์ \hh ซึ่งแสดง Quick Help เกี่ยวกับคำสั่ง read ใช้วิธีเดียวกันเพื่อได้ Quick Help สำหรับ Bash Builtin Commands ทั้งหมด

Fig 6: พิมพ์ \hh เพื่อได้ Help เกี่ยวกับ Bash Builtin Command ที่ Select
Feature 8: Featured Commenting (Commenting แบบมีคีย์เวิร์ด)
คำสั่งต่อไปนี้จะเพิ่ม Keyword Comments ตามลำดับ เช่น พิมพ์ \ckb เพื่อเพิ่ม Comment Line ด้วย Keyword BUG ภายใน Shell Script
- \ckb Keyword BUG
- \ckt Keyword TODO
- \ckr Keyword Tricky
- \ckw Keyword WARNING
พิมพ์ \ckt เพื่อเพิ่ม Comment Line ด้วย Keyword "# :TODO: mm/dd/yy:: " นี่คือ Comment Line ที่ทำหน้าที่เป็น TODO สำหรับรายการที่ต้องการทำภายหลัง

Fig 7: พิมพ์ \ckt เพื่อเพิ่ม TODO ภายใน Bash Shell Script
Plugin bash-support มีฟีเจอร์อันทรงพลังอีกมากมาย อ่าน Documentation เพื่อข้อมูลเพิ่มเติม Documentation อยู่ที่:
- README:
~/.vim/README.bashsupport - PDF:
~/.vim/bash-support/doc/bash-hot-keys.pdf - Online bash-support vim plugin documentation
- Plugin มาพร้อม Help File (
bashsupport.txt) ดูได้ด้วย:h bashsupport - [ Generate Help Tags ด้วย
:helptags ~/.vim/docแล้วใช้:h bashsupport] - Additional Screenshots ของ Plugin นี้
Recommended Reading
Vim 101 Hacks, by Ramesh Natarajan ผมเป็น Command-line Junkie จึงเป็นแฟนใหญ่ของ Vi และ Vim Editors หลายปีก่อน เมื่อเขียน C Code มากบน Linux เคยอ่าน Vim Editor Tips and Tricks ทั้งหมด จากประสบการณ์ Vim Editor เขียน Vim 101 Hacks eBook ที่มี 101 ตัวอย่างปฏิบัติเกี่ยวกับ Advanced Vim Features ต่างๆ ที่จะทำให้คุณเร็วและ Productive ใน Vim Editor แม้จะใช้ Vi และ Vim Editors มาหลายปีแล้วแต่อ่านหนังสือเล่มนี้ไม่ได้ โปรดทำความดีใจตัวเองและอ่านเล่มนี้ จะตกใจกับความสามารถของ Vim Editor