> 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/chapter02.md).

# 第二章：打开文件、查找内容

## 打开文件

哈，现在你已经在无意间学会了一种在 Vim 中打开文件的方式，虽然这种方式并不是最常 用的，但却是最直接的，尤其是当你的代码中 include 了某文件时，下面介绍另外两种常 用的打开方式。

### 在 Vim 中打开文件

* `:e <filename>` 打开名为 filename 的文件，若文件不存在则创建之
* `:Ex` 在 Vim 中打开目录树，光标选中后回车打开对应文件（提示：`-` 进入上级目录 ）

## 查找

### 文档内查找

* `*` 向后查找光标当前所在单词
* `#` 向前查找光标当前所在单词
* `/<search>` 向后查找指定字符串
* `?<search>` 向前查找指定字符串
* `n` 继续查找下一个
* `N` 继续查找上一个

*注意： `n` 和 `N` 是有方向性的，若你之前通过 `*` 查找，则 `n` 会继续向文档尾方 向查找，`N` 向文档首方向；反之，若你通过 `#` 查找，则 `n` 指向文档首，`N` 指向文 档尾*

### 行内查找

* `f<X>` 当前行内向行尾方向查找并定位到字符 `X`
* `t<X>` 当前行内向行尾方向查找并定位到字符 `X` 之前
* `F<X>` 当前行内向行首方向查找并定位到字符 `X`
* `T<X>` 当前行内向行首方向查找并定位到字符 `X` 之后
* `;` 继续向当前方向查找下一个字符
* `,` 向当前相反方向查找下一个字符

> 当前文档中有几个 “Vim” 单词，你可以尝试用 `*` 和 `#` 进行查找并感受 `n` 和 `N` 的方向性。
>
> 上面的 “注意” 中有几个字符 "n"，你可以在那试试行内查找并感受下 `;` 和 `,` 的方 向性。

### 匹配查找

Vim 中可以使用 `%` 对 `(` 和 `)`，`[` 和 `]`，`{` 和 `}` 进行匹配查找，当光标位 于其中一个符号上时，按下 `%`，光标会跳到与之匹配的另外一个符号上。

> 在下列文本中的 `()[]{}` 字符上按 `%` 看看效果，连续多按几次。

```javascript
const func = (win, doc) => {
  const SEVEN = ((1 + 2) * (3 + 4) * (5 - 6)) / 7;
  const YU = [1, 2, [[3, 4], 5, 6], 7];
  if (true) {
    return SEVEN;
  } else {
    return YU;
  }
};
```

[下一章](/learn-vim/zh-cn/chapter03.md)将介绍文档的修改，在这之前先简单介绍一下 Vim 的 buffer，简 单理解 buffer 就是当前 Vim session 的文件历史记录。

> 现在你的 buffer 中应该已经有两个文件了，你可以用 `:buffers` 或 `:ls` 命令查看 ，看到 buffer 列表了吧，大概是这个样子的：

```vim
:ls
  1 #h   "chapter01.md"                  line 47
  2 %a   "chapter02.md"                  line 1
Press ENTER or type command to continue
```

> 接下来你可以尝试通过以下命令在文件缓存中进行跳转了
>
> * `:bn` 打开缓存中下一个文件
> * `:bp` 打开缓存中上一个文件
> * `:b<N>` 打开缓存中第 N 个文件
>
> 你也可以使用 `:bdelete<N>` 来删除所要关闭的缓冲区，缩写 `:bd<N>`。
>
> 当然你也可以使用 `:Ex` 命令，选择 chapter03.md 并打开，进 入[第三章](/learn-vim/zh-cn/chapter03.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/chapter02.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.
