3.5 安装Vim
Vim 是一个功能强大的基于控制台的文本编辑器。为了快速开发,请将双手放在键盘上的主页键上使用 Vim。快捷方式和命令触手可及。
开箱即用的 Vim 只有基本功能,其只不过是像记事本这样的文本编辑器。然而,通过一些配置和扩展,Vim 可以成为编辑 Python 和 C 的强大工具。
Vim 的扩展位于不同的位置,包括 GitHub。为了简化 GitHub 插件的配置和安装,你可以安装插件管理器,如 Vundle。
要安装 Vundle,请在终端运行此命令:
$ git clone https://github.com/VundleVim/Vundle.vim.git \
~/.vim/bundle/Vundle.vim
下载 Vundle 后,你需要配置 Vim 以加载 Vundle 引擎。
你将安装两个插件:
Fugitive:Git 的状态栏,带有许多 Git 任务的快捷方式;
Tagbar:用于更容易跳转到函数、方法和类的窗格。
要安装这些插件,首先更改 Vim 配置文件(通常是 HOME/.vimrc
)的内容以包含以下行:
cpython-book-samples/11/.vimrc
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()}
要下载并安装这些插件,请运行以下命令:
$ vim +PluginInstall +qall
你应该会看到下载和安装配置文件中指定的插件的输出。
在编辑或浏览 CPython 源代码时,你会希望在方法、函数和宏之间快速跳转。基本的文本搜索不会区分对函数的调用或其定义与实现。但是你可以使用名为 ctags8 的应用程序将多种语言的源文件索引到纯文本数据库中。
要为标准库中的所有 C 文件和 Python 文件索引 CPython 的头文件,请运行以下代码:
$./configure
$ make tags
现在在 Vim 中打开 Python/ceval.c
文件:
$ vim Python/ceval.c
你将在底部看到 Git 状态,在右侧窗格中看到函数、宏和变量:
截图不译。
接下来,打开一个 Python 文件,例如 Lib/subprocess.py
:
$ vim Lib/subprocess.py
Tagbar 将显示你的 import、类、方法和函数:
截图不译。
在 Vim 中,你可以使用 Ctrl
+ W
在窗口之间切换,使用 L
移动到右侧窗格,并使用箭头键在标记的函数之间上下移动。
按 Enter
跳到任何函数实现。要返回编辑器窗格,请按 Ctrl
+ W
,然后按 H
。
Last updated