认识
官网:https://www.vim.org/
文档:https://www.vim.org/docs.php
仓库:https://www.vim.org/download.php
Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as “vi” with most UNIX systems and with Apple OS X. 简而言之,Vim 是个扩展性非常强的编辑器。
# 2025-10-28 除了编辑远程服务器内的文件,我们基本不会使用 Vim 编辑器。
构建
在 Contaienr 中,我们可能会用到静态连接版本:https://github.com/dtschan/vim-static
应用
基本操作
| _KEY_ | _DESCRIPTION_ |
|---|---|
| D、d$ | 从当前光标位置开始,删除到行尾 |
| v | 进入 Visual 模式,可以选择 |
| y | 复制 |
| p | 粘贴 |
| V、d、p | 剪切 |
| i、a | 进入编辑模式;我们经常使用 a 键,因为我们希望光标移动到后一个字符。 |
全选:ggVG
从文件中读取内容,然后插入当前位置::-1.read<filename>
行号::set number
移动光标
| 功能 | 命令 |
|---|---|
| 第一行开始 | gg |
| 最后一行结束 | G |
执行命令
| 功能 | 命令 |
|---|---|
| 使用 XMLINT 格式化 | ggVG:、!xmllint –format – |
| 重复输入 | Ctrl+O、80、i、-、ESC |
替换操作
| 功能 | 命令 |
|---|---|
| 替换命令 | :%s/search/replace/g |
| 将字符替换为换行 | :%s/char/r/g |
!!!替换操作是正则替换,因此特殊字符需要转义。
文本删除
| 功能 | 命令 |
|---|---|
| 从光标位置开始,删除到行尾 | d $ |
场景 | 编辑 YAML 文件
Wrong indentation when editing Yaml in Vim – Stack Overflow
# 修复错误的自动缩进 echo 'autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab' >> ~/.vimrc
参考
Wikipedia/Vim (text editor)
Delete from cursor to end of line on VI
what is the command for “Select All” in vim and VsVim?
Vim indent xml file
Vim tips: The basics of search and replace
Repeating characters in VIM insert mode
How To Show or Hide Line Numbers In vi / vim Text Editor
Vim: insert text from a file at current cursor position
How to replace a character by a newline in Vim
Find and replace using regular expressions
Delete from cursor to end of line in `vi`
How to set cursor to after last character in vim?