Vim เป็นหนึ่งใน command-line text editors ที่ยอดนิยมและมีอิทธิพลมากที่สุด มาพร้อม functionality และ features มากมายสำหรับ terminal geeks แต่ Vim อาจ discourage new users – ไม่ใช่เพราะซับซ้อนต่อการเรียนรู้และ master แต่เพราะมี unconventional way ในการทำสิ่งต่างๆ เช่น Vim ใช้ shortcuts, modes, และ bindings ที่ต้องใช้เวลาปรับตัว และคุณต้องการบทความเต็มๆ แค่เพื่อแสดงวิธีออกจาก Vim
คู่มือนี้อธิบายวิธีทำให้คุ้นเคยกับ Vim โดยเรียนรู้ quick และ easy steps เพื่อ comment multiple code blocks
ก่อนอื่น ก่อน comment multiple lines ใน Vim คุณต้องรู้วิธี comment single line ก่อน มา focus ที่นี่เป็น starting point
วิธี Comment Single Line ใน Vim
เพื่อ comment out single line ใน Vim 进入 Visual Mode โดยกด Ctrl + V ไปที่บรรทัดที่ต้องการ comment กด C
ขึ้นอยู่กับ Vim configuration นี้ควร comment out บรรทัดที่เลือก เสร็จแล้ว – ง่ายใช่ไหม?
ตอนนี้ลองลึกลงไป
วิธี Comment Multiple Lines
เรียนรู้วิธี comment out multiple lines ใน Vim โดยใช้ python script 简单
vim comments.py
Method 1 – ใช้ Line Numbers
วิธีหนึ่ง comment out multiple lines ใน Vim คือใช้ line numbers Vim อนุญาตให้ระบุ start และ finish line และ comment พวกมันออก
- เปิดไฟล์ที่ต้องการ edit ใน Vim
- กด ESC เพื่อเข้าสู่ Vim Command Mode
- พิมพ์คำสั่ง:
:[start],[end]s/^/#
start คือ line number เริ่มต้น end คือ line number ที่จบ comment block
Screenshot ต่อไป illustrate การใช้คำสั่ง comment จาก line 15 ถึง 25
:15,25s/^/#
Method 2 – Highlight Block
สมมติคุณไม่รู้ line numbers ที่จะ comment ใช้ highlight method highlight target block และ comment ออก
ใช้ highlight method: ไปที่บรรทัดที่ต้องการเริ่ม comment กด Shift + V
สิ่งนี้จะ highlight ทั้งบรรทัดที่ cursor อยู่
ใช้ up/down arrow keys เลือกบรรทัดที่ต้องการ comment
หมายเหตุ: ใช้ mouse scroll wheel เลือกบรรทัดที่ต้องการ comment ได้ด้วย
หลัง highlight target block แล้ว กด RETURN ออกจาก Visual Line
กด ESC เข้า command mode พิมพ์คำสั่ง:
:s/^/#
กด Enter รันคำสั่ง comment highlighted lines
Method 3 – Regular Expressions
Vim รองรับ regular expressions comment out lines ที่มีคำเฉพาะ คำสั่ง comment specific words:
:g/\keyword/s/^/#
ในคำสั่ง "keyword" แทนคำเฉพาะที่ต้องการ comment
ตัวอย่างด้านล่าง comment ทุกบรรทัดที่มี keyword True:
:g/\True/s/^/#
Vim จะ highlight target word อัตโนมัติ ก่อน execute คำสั่ง
สุดท้าย กด ENTER execute คำสั่ง
Method 4 – Visual Mode
ใช้ Vim visual mode comment out block of code ได้
เริ่มที่บรรทัดที่ต้องการเริ่ม comment กด Ctrl + V เปิด visual mode
ใช้ up/down arrow keys เลือกทุกบรรทัดที่ต้องการ comment Vim จะ insert highlight ที่ต้นแต่ละบรรทัดบ่งบอกว่า selected
เมื่อ target lines selected แล้ว กด Shift + I เข้า insert mode
พิมพ์ comment symbol สำหรับ target file กรณีนี้ต้อง insert pound (#) symbol เพื่อ comment Python code
สุดท้าย กด ESC Vim จะ comment out ทุกบรรทัดที่ selected
วิธี Uncomment Multiple Lines
อาจต้อง uncomment multiple lines ทำได้ด้วย visual mode
เริ่มที่บรรทัดที่ต้องการ uncomment กด Ctrl + V activate highlight mode
ใช้ up/down arrow keys เลือกทุกบรรทัดที่ต้องการ uncomment หลัง select region แล้ว กด X ลบ comments
อีกวิธี ใช้คำสั่งลบทุกบรรทัดที่ขึ้นต้นด้วย comment symbol
คำสั่งสำหรับนั้น:
:%s/^//
จำได้ว่าแทนที่ comment symbol ด้วย actual comment symbol ที่จะลบ
สรุป
Vim เป็น text editor ยอดเยี่ยม ให้ทำงานกับ text files ใน terminal ง่ายและสะดวก แม้ tutorial นี้แสดงวิธี comment multiple lines ใน Vim แบบต่างๆ แต่เพิ่งขูดผิวความสามารถของ Vim เช่น ทำให้ Vim กลายเป็น Word processor หากคุณเป็น experienced Vim user อย่าลืมดู Vim tips และ tricks อื่นๆ