首頁·終端機命令

vim

文字處理

用法

vim [file]

帶模式的編輯器:不按 i 之前打字什麼都不會輸入,離開要先按 Esc,再用 :wq 儲存或 :q! 丟棄。

常用選項

選項含義
:wqWrite the file and quit; :x does the same
:q!Quit and throw every change away
iEnter insert mode; Esc goes back to normal mode
ddDelete the current line; u undoes the last change
/wordSearch forward; n jumps to the next hit
:%s/old/new/gReplace throughout the file
-ROpen read-only so you cannot save by accident

範例

vim +100 config.yml

Opens at line 100.

vim -d a.txt b.txt

Opens the two files side by side as a diff.

vim -R big.log

Reads a log without any risk of writing to it.

它們本來就是用管線串起來用的。串三條往往比讓一條命令做完所有事更短。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. vim 是做什麼的?

帶模式的編輯器:不按 i 之前打字什麼都不會輸入,離開要先按 Esc,再用 :wq 儲存或 :q! 丟棄。

Q. 怎麼寫?

vim [file] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man vim。這條命令屬於文字處理。

相關命令

man 手冊: man vim