> For the complete documentation index, see [llms.txt](https://dofy.gitbook.io/learn-vim/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dofy.gitbook.io/learn-vim/zh-cn/chapter03.md).

# 第三章：文档的修改与保存

## 修改文档

你现在已经学会了控制光标、打开文件、切换文件、并在文件中查找内容，这些操作都是在 Vim 的 normal 模式下进行的。现在，是时候进入 Vim 的另外一种模式 —— insert 模式， 学习一下如何修改文件了。

### 插入

* `i` 当前字符前插入
* `a` 当前字符后插入
* `I` 行首插入
* `A` 行尾插入
* `o` 在下一行插入
* `O` 在上一行插入

*注意：以上任何一个命令都会使 Vim 进入 insert 模式，进入该模式后光标会发生变化， 这时输入的文字会直接出现在文档中，按 `Esc` 键或 `Ctrl-[` 或 `Ctrl-C` 退出 insert 模式。*

### 删除（并保存到 Vim 剪贴板）

* `s` 删除当前字符，并进入 `INSERT` 模式
* `S` 删除当前行并保存到 Vim 剪贴板，同时进入 `INSERT` 模式（等同于 `cc`）
* `x` 删除当前字符，相当于 insert 模式下的 `Delete`
* `X` 删除前一个字符，相当于 insert 模式下的 `Backspace`
* `dd` 删除当前行，并将删除的内容保存到 Vim 剪贴板
* `d<X>` 删除指定内容并保存到 Vim 剪贴板
* `cc` 删除当前行并保存到 Vim 剪贴板，同时进入 `INSERT` 模式
* `c<X>` 删除指定内容并保存到 Vim 剪贴板，同时进入 `INSERT` 模式

*说明： `<X>` 部分是对操作内容的描述，如果要删除一个单词，就输入 `dw` 或者 `de`，要复制当前位置到行尾的内容，就输入 `y$`，要删除后面 3 个字符并插入，就输入 `c3l` 诸如此类。*

### 复制

* `yy` 复制当前行到 Vim 剪贴板
* `y<X>` 复制指定内容到 Vim 剪贴板

### 粘贴

* `p` 在当前位置后粘贴
* `P` 在当前位置前粘贴

### 合并

* `J` 将当前行与下一行合并

> 尝试在下面的文本中进行复制粘贴练习

```
删除这一行
粘贴到这一行下面
剪切 ABC 并把它粘贴到 XYZ 前面，使这部分内容看起来像
剪切 并把它粘贴到 ABC XYZ 前面。
```

### 替换

* `r<X>` 将当前字符替换为 X
* `gu<X>` 将指定的文本转换为小写
* `gU<X>` 将指定的文本转换为大写
* `:%s/<search>/<replace>/` 查找 search 内容并替换为 replace 内容

> 尝试修改下列文本的大小写

```
Change this line to UPPERCASE, THEN TO lowercase.
```

> 还有个更好玩的命令 `g~<X>`，先将光标定位到上面那行文本，执行 `0g~$` 看看发生了 什么。

### 撤销、重做

* `u` 撤销
* `Ctrl-r` 重做

### 保存文件

* `:w` 保存当前文件
* `:wa` 保存全部文件
* `:wq` or `ZZ` 保存并退出
* `:q!` or `ZQ` 强制退出，不保存
* `:saveas <new filename>` 文件另存为
* `:w <new filename>` 文件另存一份名为 `<new filename>` 的副本并继续编辑原文件

> 你可以试着把当前（也许已经改得面目全非的）文件另存一份，然后继 续[下一章](/learn-vim/zh-cn/chapter04.md)的学习。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dofy.gitbook.io/learn-vim/zh-cn/chapter03.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
