Global

  • :h[elp] keyword - ขอความช่วยเหลือสำหรับ keyword
  • :sav[eas] file - บันทึกเป็นไฟล์
  • :clo[se] - ปิดหน้าต่างปัจจุบัน
  • :ter[minal] - เปิดหน้าต่างเทอร์มินัล
  • K - เปิด man page สำหรับคำใต้ cursor
Tip ใช้คำสั่ง vimtutor ในเทอร์มินัลเพื่อเรียนรู้คำสั่งแรกของ Vim

การเลื่อนตำแหน่งเคอร์เซอร์

  • h - ไปทางซ้าย
  • j - ลงด้านล่าง
  • k - ขึ้นข้างบน
  • l - ไปทางขวา
  • gj - ลงด้านล่าง (สำหรับข้อความหลายบรรทัด)
  • gk - ขึ้นข้างบน (สำหรับข้อความหลายบรรทัด)
  • H - ไปยังบรรทัดแรกของจอ
  • M - ไปยังกลางจอ
  • L - ไปยังบรรทัดสุดท้ายของจอ
  • w - ไปข้างหน้าทีละคำ โดยไปที่ตัวอักษรแรกของคำนั้น
  • W - ไปข้างหน้าทีละคำ โดยไปที่ตัวอักษรแรกของคำนั้น (แบบรวมเครื่องหมายวรรคตอนด้วย)
  • e - ไปข้างหน้าทีละคำ โดยไปที่ตัวอักษรสุดท้ายของคำนั้น
  • E - ไปข้างหน้าทีละคำ โดยไปที่ตัวอักษรสุดท้ายของคำนั้น (แบบรวมเครื่องหมายวรรคตอนด้วย)
  • b - ไปข้างหลังทีละคำ โดยไปที่ตัวอักษรแรกของคำนั้น
  • B - ไปข้างหลังทีละคำ โดยไปที่ตัวอักษรแรกของคำนั้น (แบบรวมเครื่องหมายวรรคตอนด้วย)
  • ge - ไปข้างหลังทีละคำ โดยไปที่ตัวอักษรสุดท้ายของคำนั้น
  • gE - ไปข้างหลังทีละคำ โดยไปที่ตัวอักษรสุดท้ายของคำนั้น (แบบรวมเครื่องหมายวรรคตอนด้วย)
  • % - ไปที่ตัวอักษรที่ตรงกัน (คู่เครื่องหมายที่รองรับ: '()', '{}', '[]' - ใช้คำสั่ง :h matchpairs ใน vim เพื่อดูข้อมูลเพิ่มเติม)
  • 0 - ไปยังจุดเริ่มต้นของบรรทัดนั้น
  • ^ - ไปยังตัวอักษรแรกสุด ที่ไม่ใช่ช่องว่าง ของบรรทัดนั้น
  • $ - ไปยังจุดท้ายสุดของบรรทัดนั้น
  • g_ - ไปยังตัวอักษรสุดท้าย ที่ไม่ใช่ช่องว่าง ของบรรทัดนั้น
  • gg - ไปยังบรรทัดแรกของไฟล์
  • G - ไปยังบรรทัดสุดท้ายของไฟล์
  • 5gg or 5G - ไปยังบรรทัดที่ 5
  • gd - move to local declaration
  • gD - move to global declaration
  • fx - ไปที่ตัวอักษร x ถัดไปหลังจากตำแหน่งเคอร์เซอร์ปัจจุบัน
  • tx - ไปที่ตัวอักษรก่อนหน้าตัวอักษร x ถัดไปหลังจากตำแหน่งเคอร์เซอร์ปัจจุบัน
  • Fx - ไปที่ตัวอักษร x ถัดไปก่อนหน้าตำแหน่งเคอร์เซอร์ปัจจุบัน
  • Tx - ไปที่ตัวอักษรหลังจากตัวอักษร x ถัดไปก่อนหน้าตำแหน่งเคอร์เซอร์ปัจจุบัน
  • ; - ทำคำสั่ง f, t, F หรือ T คำสั่งที่แล้ว
  • , - ทำคำสั่ง f, t, F หรือ T คำสั่งที่แล้ว แต่ย้อนกลับ
  • } - jump to next paragraph (or function/block, when editing code)
  • { - jump to previous paragraph (or function/block, when editing code)
  • zz - ทำให้เคอร์เซอร์อยู่กลางจอ
  • zt - position cursor on top of the screen
  • zb - position cursor on bottom of the screen
  • Ctrl + e - เลื่อนจอลงหนึ่งบรรทัด (โดยไม่ขยับเคอร์เซอร์)
  • Ctrl + y - เลื่อนจอขึ้นหนึ่งบรรทัด (โดยไม่ขยับเคอร์เซอร์)
  • Ctrl + b - เลื่อนขึ้นไปข้างบนหนึ่งจอเต็ม
  • Ctrl + f - เลื่อนลงไปข้างล่างหนึ่งจอเต็ม
  • Ctrl + d - เลื่อนลงไปข้างล่างครึ่งจอ
  • Ctrl + u - เลื่อนขึ้นไปข้างบนครึ่งจอ
Tip กดตัวเลขก่อนใช้คำสั่งการเลื่อนตำแหน่งเคอร์เซอร์ ซึ่งตัวเลขนั้นคือจำนวนคำสั่งที่ทำซ้ำ เช่น 4j คือลงมาด้านล่าง 4 บรรทัด

การใช้งาน Insert mode - การพิมพ์ข้อความ แบบแทรก(Insert) หรือแบบต่อท้าย(Append)

  • i - เริ่มพิมพ์ข้อความแทรกก่อนหน้าเคอร์เซอร์
  • I - เริ่มพิมพ์ข้อความที่ต้นบรรทัดนั้น (ไม่รวมเครื่องหมายวรรคตอน)
  • a - เริ่มพิมพ์ข้อความหลังเคอร์เซอร์ (ต่อท้ายเคอร์เซอร์)
  • A - เริ่มพิมพ์ข้อความที่ท้ายของบรรทัดนั้น
  • o - เริ่มพิมพ์ข้อความในบรรทัดใหม่ใต้บรรทัดเดิม
  • O - เริ่มพิมพ์ข้อความในบรรทัดใหม่ก่อนหน้าบรรทัดเดิม
  • ea - เริ่มพิมพ์ข้อความต่อจากข้อความนั้น
  • Ctrl + h - ลบตัวอักษรที่อยู่ด้านหน้าเคอร์เซอร์ขณะที่อยู่ใน insert mode
  • Ctrl + w - ลบคำที่อยู่ด้านหน้าเคอร์เซอร์ขณะที่อยู่ใน insert mode
  • Ctrl + j - เริ่มบรรทัดใหม่ขณะที่อยู่ใน insert mode
  • Ctrl + t - เลื่อนข้อความไปทางขวา 1 ย่อหน้า (tab) ขณะที่อยู่ใน insert mode
  • Ctrl + d - เลื่อนข้อความไปทางซ้าย 1 ย่อหน้า (tab) ขณะที่อยู่ใน insert mode
  • Ctrl + n - insert (auto-complete) next match before the cursor during insert mode
  • Ctrl + p - insert (auto-complete) previous match before the cursor during insert mode
  • Ctrl + rx - insert the contents of register x
  • Ctrl + ox - เข้าสู่โหมดปกติเพียงชั่วคราว เพื่อใช้คำสั่ง x ของโหมดปกติหนึ่งครั้ง
  • Esc or Ctrl + c - ออกจาก insert mode

การแก้ไข

  • r - แทนที่ตัวตัวอักษรนั้นหนึ่งตัว
  • R - แทนที่ตัวอักษรมากกว่า 1 ตัว (จนกว่าจะกดปุ่ม ESC)
  • J - ดึงเอาบรรทัดล่างขึ้นมาอยู่บรรทัดเดียวกันกับบรรทัดปัจจุบัน
  • gJ - ดึงเอาบรรทัดล่างขึ้นมาอยู่บรรทัดเดียวกันกับบรรทัดปัจจุบัน ไม่เว้นวรรค
  • gwip - ฟอร์แมตย่อหน้าใหม่
  • g~ - switch case up to motion
  • gu - change to lowercase up to motion
  • gU - change to uppercase up to motion
  • cc - ลบทั้งบรรทัดแล้วพิมพ์ใหม่
  • c$ or C - ลบไปจนถึงสุดบรรทัดแล้วพิมพ์ใหม่
  • ciw - เปลี่ยน (แทนที่) คำนั้นทั้งคำ
  • cw or ce - ลบทั้งคำแล้วพิมพ์ใหม่
  • s - ลบหนึ่งตัวอักษร แล้วเริ่มพิมพ์ใหม่
  • S - ลบทั้งบรรทัดแล้วพิมพ์ใหม่ (เหมือนกับใช้ cc)
  • xp - สลับตัวอักษร 2 ตัวที่ติดกัน (ลบแล้ววาง)
  • u - ย้อนกลับ (undo)
  • U - คืนค่า (undo) บรรทัดสุดท้ายที่เปลี่ยนแปลง
  • Ctrl + r - ยกเลิกคำสั่งย้อนกลับ (redo)
  • . - ทำคำสั่งสุดท้ายซ้ำ

การคลุมเลือกข้อความ (visual mode)

  • v - เริ่มเข้าใช้งาน visual mode, และใช้การเลื่อนเคอร์เซอร์เพื่อคลุมเลือก, จากนั้นก็ทำคำสั่งที่เราต้องการ ( เช่น y เพื่อคัดลอก (yank))
  • V - เริ่มเข้าใช้งาน visual mode แบบเลือกเป็นบรรทัดๆ
  • o - เลื่อนเคอร์เซอร์ไปยัง อีกด้านของพื้นที่โดนเลือก
  • Ctrl + v - เริ่มเข้าใช้งาน visual mode แบบเลือก เป็นกล่องสี่เหลี่ยม
  • O - เลื่อนเคอร์เซอร์ไปยัง อีกมุมของพื้นที่โดนเลือก
  • aw - เลือกทีละคำ
  • ab - เลือกคลุมทั้ง ()
  • aB - เลือกคลุมทั้ง {}
  • at - a block with <> tags
  • ib - เลือกคลุมเฉพาะใน ()
  • iB - เลือกคลุมเฉพาะใน {}
  • it - inner block with <> tags
  • Esc or Ctrl + c - ออกจาก visual mode
Tip แทนที่เราจะพิมพ์ b หรือ B ตอนที่จะเลือกคลุม ก็สามารถพิมพ์ ( หรือ { ได้เหมือนกันตามลำดับ

คำสั่งที่ใช้ใน Visual mode

  • > - เลื่อนข้อความไปทางขวา 1 ย่อหน้า (tab)
  • < - เลื่อนข้อความไปทางซ้าย1 ย่อหน้า (tab)
  • y - คัดลอก (copy) ข้อความที่คลุมไว้แล้ว
  • d - ลบ (cut) ข้อความที่เลือกไว้แล้ว (สามารถใช้ p เพื่อวางข้อความนั้นได้)
  • ~ - สลับตัวพิมพ์เล็ก/พิมพ์ใหญ่
  • u - เปลี่ยนข้อความที่คลุมไว้เป็นตัวพิมพ์เล็ก
  • U - เปลี่ยนข้อความที่คลุมไว้เป็นตัวพิมพ์ใหญ่

Registers

  • :reg[isters] - show registers content
  • "xy - yank into register x
  • "xp - paste contents of register x
  • "+y - yank into the system clipboard register
  • "+p - paste from the system clipboard register
Tip Registers are being stored in ~/.viminfo, and will be loaded again on next restart of vim.
Tip Special registers:

0 - last yank
" - unnamed register, last delete or yank
% - current file name
# - alternate file name
* - clipboard contents (X11 primary)
+ - clipboard contents (X11 clipboard)
/ - last search pattern
: - last command-line
. - last inserted text
- - last small (less than a line) delete
= - expression register
_ - black hole register

Marks

  • :marks - list of marks
  • ma - set current position for mark A
  • `a - jump to position of mark A
  • y`a - yank text to position of mark A
  • `0 - go to the position where Vim was previously exited
  • `" - go to the position when last editing this file
  • `. - go to the position of the last change in this file
  • `` - go to the position before the last jump
  • :ju[mps] - list of jumps
  • Ctrl + i - go to newer position in jump list
  • Ctrl + o - go to older position in jump list
  • :changes - list of changes
  • g, - go to newer position in change list
  • g; - go to older position in change list
  • Ctrl + ] - jump to the tag under cursor
Tip To jump to a mark you can either use a backtick (`) or an apostrophe ('). Using an apostrophe jumps to the beginning (first non-blank) of the line holding the mark.

มาโคร

  • qa - บันทึกมาโคร 'a'
  • q - หยุดบันทึกมาโคร 'a'
  • @a - ใช้งานมาโคร 'a'
  • @@ - ใช้งานมาโคร 'a' อีกครั้ง

การลบ (Cut) และการวาง (Paste)

  • yy - คัดลอก (copy) ทั้งบรรทัด
  • 2yy - คัดลอก (copy) 2 บรรทัด
  • yw - คัดลอก (copy) ทั้งคำ
  • yiw - yank (copy) word under the cursor
  • yaw - yank (copy) word under the cursor and the space after or before it
  • y$ or Y - คัดลอก (copy) ตั้งแต่ตำแหน่งเคอร์เซอร์ปัจจุบัน ถึงสุดบรรทัด
  • p - วาง (paste) หลังเคอร์เซอร์
  • P - วาง (paste) ก่อนเคอร์เซอร์
  • gp - put (paste) the clipboard after cursor and leave cursor after the new text
  • gP - put (paste) before cursor and leave cursor after the new text
  • dd - ลบ (cut) ทั้งบรรทัด
  • 2dd - ลบ (cut) 2 บรรทัด
  • dw - ลบ (cut) ทั้งคำ่
  • diw - delete (cut) word under the cursor
  • daw - delete (cut) word under the cursor and the space after or before it
  • :3,5d - delete lines starting from 3 to 5
Tip You can also use the following characters to specify the range:
e.g.

:.,$d - From the current line to the end of the file
:.,1d - From the current line to the beginning of the file
:10,$d - From the 10th line to the beginning of the file

  • :g/{pattern}/d - delete all lines containing pattern
  • :g!/{pattern}/d - delete all lines not containing pattern
  • d$ or D - ลบ (cut) ตั้งแต่ตำแหน่งเคอร์เซอร์ปัจจุบัน ถึงสุดบรรทัด
  • x - ลบ (cut) หนึ่งตัวอักษร

การย่อหน้าข้อความ

  • >> - ย่อหน้า (ไปทางขวา) บรรทัดหนึ่งย่อหน้า
  • << - ย้อนกลับย่อหน้า (ไปทางซ้าย) บรรทัดหนึ่งย่อหน้า
  • >% - indent a block with () or {} (cursor on brace)
  • <% - de-indent a block with () or {} (cursor on brace)
  • >ib - indent inner block with ()
  • >at - indent a block with <> tags
  • 3== - ย้อนกลับย่อหน้า 3 บรรทัด
  • =% - re-indent a block with () or {} (cursor on brace)
  • =iB - re-indent inner block with {}
  • gg=G - re-indent entire buffer
  • ]p - paste and adjust indent to current line

การออกจาก Vim

  • :w - บันทึก (write) ลงไฟล์อย่างเดียว ไม่ได้ออก
  • :w !sudo tee % - บันทึกไฟล์ปัจจุบันโดยใช้ sudo ร่วมด้วย
  • :wq or :x or ZZ - บันทึก (write) ลงไฟล์ และออกจาก Vim
  • :q - ออกจาก Vim (ออกไม่ได้ ถ้าไม่ได้บันทึกลงไฟล์)
  • :q! or ZQ - ออกจาก Vim ได้เลย โดยไม่ต้องบันทึกลงไฟล์
  • :wqa - บันทึก (write) ลงไฟล์ และปิด tab ทั้งหมด

การค้นหาและการแทนที่

  • /pattern - ค้นหาตาม pattern ที่ใส่ไป
  • ?pattern - ค้นหาย้อนกลับไป(ขึ้นไป) ตาม pattern ที่ใส่ไป
  • \vpattern - 'very magic' pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)
  • n - ค้นหาอันถัดไป
  • N - ค้นหาย้อนกลับไป
  • :%s/old/new/g - แทนที่ old ทั้งหมดด้วย new
  • :%s/old/new/gc - แทนที่ old ทั้งหมดด้วย new ก่อนทำการแทนที่จะถามก่อนทุกครั้ง
  • :noh[lsearch] - remove highlighting of search matches

Search in multiple files

  • :vim[grep] /pattern/ {`{file}`} - search for pattern in multiple files
e.g. :vim[grep] /foo/ **/*
  • :cn[ext] - jump to the next match
  • :cp[revious] - jump to the previous match
  • :cope[n] - open a window containing the list of matches
  • :ccl[ose] - close the quickfix window

Tabs

  • :tabnew or :tabnew {page.words.file} - เปิด filename บน tab ใหม่
  • Ctrl + wT - ย้ายหน้าต่างจากที่ถูกแบ่งไว้ ไปอีก tab
  • gt or :tabn[ext] - ย้ายไป tab ถัดไป
  • gT or :tabp[revious] - ย้ายไป tab ก่อนหน้า
  • #gt - ย้ายไป tab หมายเลข #
  • :tabm[ove] # - ขยับที่ tab ปัจจุบันไปยัง ตำแหน่งที่ # (โดยเริ่มนับจาก 0)
  • :tabc[lose] - ปิด tab ปัจจุบัน รวมทั้ง หน้าต่างที่ถูกแบ่งไว้ใน tab นั้นทั้งหมด
  • :tabo[nly] - ปิด tab ทั้งหมด ยกเว้น tab ปัจจุบัน
  • :tabdo command - run the command on all tabs (e.g. :tabdo q - closes all opened tabs)

การทำงานพร้อมๆ กันหลายไฟล์

  • :e[dit] file - เปิดไฟล์ filename ขึ้นมาแก้ไขบน buffer ใหม่
  • :bn[ext] - เปิดไฟล์ใน buffer ถัดไป
  • :bp[revious] - เปิดไฟล์ใน buffer ก่อนหน้านี้
  • :bd[elete] - ลบ buffer ที่กำลังทำงานอยู่ (ปิดไฟล์นี้)
  • :b[uffer]# - go to a buffer by #
  • :b[uffer] file - go to a buffer by file
  • :ls or :buffers - ดูทุก buffer ที่กำลังทำงานอยู่
  • :sp[lit] file - เปิดไฟล์ filename ใน buffer ใหม่ โดยแบ่งหน้าต่างตามแนวนอน
  • :vs[plit] file - เปิดไฟล์ filename ใน buffer ใหม่ โดยแบ่งหน้าต่างตามแนวตั้ง
  • :vert[ical] ba[ll] - edit all buffers as vertical windows
  • :tab ba[ll] - edit all buffers as tabs
  • Ctrl + ws - แบ่งหน้าต่าง ตามแนวนอน
  • Ctrl + wv - แบ่งหน้าต่าง ตามแนวตั้ง
  • Ctrl + ww - สลับไปใช้อีก หน้าต่าง
  • Ctrl + wq - ปิดหน้าต่างปัจจุบัน
  • Ctrl + wx - exchange current window with next one
  • Ctrl + w= - make all windows equal height & width
  • Ctrl + wh - เลื่อนเคอร์เซอร์ไปหน้าต่างทางซ้าย (การแบ่งตามแนวตั้ง)
  • Ctrl + wl - เลื่อนเคอร์เซอร์ไปหน้าต่างทางขวา (การแบ่งตามแนวตั้ง
  • Ctrl + wj - เลื่อนเคอร์เซอร์ไปหน้าต่างด้านล่าง (การแบ่งตามแนวนอน)
  • Ctrl + wk - เลื่อนเคอร์เซอร์ไปหน้าต่างด้านบน (การแบ่งตามแนวนอน)
  • Ctrl + wH - make current window full height at far left (leftmost vertical window)
  • Ctrl + wL - make current window full height at far right (rightmost vertical window)
  • Ctrl + wJ - make current window full width at the very bottom (bottommost horizontal window)
  • Ctrl + wK - make current window full width at the very top (topmost horizontal window)

Diff

  • zf - manually define a fold up to motion
  • zd - delete fold under the cursor
  • za - toggle fold under the cursor
  • zo - open fold under the cursor
  • zc - close fold under the cursor
  • zr - reduce (open) all folds by one level
  • zm - fold more (close) all folds by one level
  • zi - toggle folding functionality
  • ]c - jump to start of next change
  • [c - jump to start of previous change
  • do or :diffg[et] - obtain (get) difference (from other buffer)
  • dp or :diffpu[t] - put difference (to other buffer)
  • :diffthis - make current window part of diff
  • :dif[fupdate] - update differences
  • :diffo[ff] - switch off diff mode for current window
Tip The commands for folding (e.g. za) operate on one level. To operate on all levels, use uppercase letters (e.g. zA).
Tip To view the differences of files, one can directly start Vim in diff mode by running vimdiff in a terminal. One can even set this as git difftool.