> For the complete documentation index, see [llms.txt](https://hai-shi.gitbook.io/cpython-internals/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hai-shi.gitbook.io/cpython-internals/3-de/3.5-vim.md).

# 3.5 安装Vim

Vim 是一个功能强大的基于控制台的文本编辑器。为了快速开发，请将双手放在键盘上的主页键上使用 Vim。快捷方式和命令触手可及。

{% hint style="info" %}
**注**

在大多数 Linux 发行版和 macOS 终端中，vi 是 vim 的别名。我们将在本书中使用 vim 命令，但如果你使用的环境中 vim 有别名 vi，那么也可以使用 vi。
{% endhint %}

开箱即用的 Vim 只有基本功能，其只不过是像记事本这样的文本编辑器。然而，通过一些配置和扩展，Vim 可以成为编辑 Python 和 C 的强大工具。

Vim 的扩展位于不同的位置，包括 GitHub。为了简化 GitHub 插件的配置和安装，你可以安装插件管理器，如 Vundle。

要安装 Vundle，请在终端运行此命令：

```bash
$ git clone https://github.com/VundleVim/Vundle.vim.git \
  ~/.vim/bundle/Vundle.vim
```

下载 Vundle 后，你需要配置 Vim 以加载 Vundle 引擎。

你将安装两个插件：

1. **Fugitive**：Git 的状态栏，带有许多 Git 任务的快捷方式；
2. **Tagbar**：用于更容易跳转到函数、方法和类的窗格。

要安装这些插件，首先更改 Vim 配置文件（通常是 `HOME/.vimrc`）的内容以包含以下行：

`cpython-book-samples/11/.vimrc`

```vim
syntax on
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
Plugin 'majutsushi/tagbar'
" All of your Plugins must be added before this line
call vundle#end() " required
filetype plugin indent on " required
" Open tagbar automatically in C files, optional
autocmd FileType c call tagbar#autoopen(0)
" Open tagbar automatically in Python files, optional
autocmd FileType python call tagbar#autoopen(0)
" Show status bar, optional
set laststatus=2
" Set status as git status (branch), optional
set statusline=%{FugitiveStatusline()}
```

要下载并安装这些插件，请运行以下命令：

```bash
$ vim +PluginInstall +qall
```

你应该会看到下载和安装配置文件中指定的插件的输出。

在编辑或浏览 CPython 源代码时，你会希望在方法、函数和宏之间快速跳转。基本的文本搜索不会区分对函数的调用或其定义与实现。但是你可以使用名为 ctags[8](http://ctags.sourceforge.net/) 的应用程序将多种语言的源文件索引到纯文本数据库中。

要为标准库中的所有 C 文件和 Python 文件索引 CPython 的头文件，请运行以下代码：

```bash
$./configure
$ make tags
```

现在在 Vim 中打开 `Python/ceval.c` 文件：

```bash
$ vim Python/ceval.c
```

你将在底部看到 Git 状态，在右侧窗格中看到函数、宏和变量：

截图不译。

接下来，打开一个 Python 文件，例如 `Lib/subprocess.py`：

```bash
$ vim Lib/subprocess.py
```

Tagbar 将显示你的 import、类、方法和函数：

截图不译。

在 Vim 中，你可以使用 `Ctrl` + `W` 在窗口之间切换，使用 `L` 移动到右侧窗格，并使用箭头键在标记的函数之间上下移动。

按 `Enter` 跳到任何函数实现。要返回编辑器窗格，请按 `Ctrl` + `W`，然后按 `H`。

{% hint style="info" %}
**参见**

查看 VIM Adventuresa，以有趣的方式学习和记忆 Vim 命令。

ahttps\://vim-adventures.com/
{% endhint %}
