首页·终端命令

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