vim 输入特殊字符

vim 的编辑能力确实很强,今天说一下如何在vim中输入特殊字符,比如音标、unicode 字符。

http://vimhelp.appspot.com/digraph.txt.html
http://vimhelp.appspot.com/insert.txt.html#i_CTRL-V_digit

关键就在上面两篇,可能许多人不喜欢看复杂的文章,我这里简单介绍一下用法。

输入音标 ə

i 进入编辑模式 CTRL-vu0259
那如何知道 ə 的 unicode hex number 是 0259呢,在vim的normal 模式下,将光标移动到ə输入ga

输入 ⇒

i 进入编辑模式 CTRL-k=>
:dig 可以看到支持这种方法输入的unicode 字符

VIM + SVN 管理

公司使用svn管理源代码,避免不了要和svn打交道,有几个比较好的解决方案。

1. vcscommand.vim

http://www.vim.org/scripts/script.php?script_id=90

这个插件的特点是支持的版本管理工具多,支持git,svn等常见版本管理工具。
vcscommand.vim插件默认绑定了无差别的快捷键,使用起来非常方便。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|<Leader>|ca VCSAdd
|<Leader>|cn VCSAnnotate
|<Leader>|cN VCSAnnotate!
|<Leader>|cc VCSCommit
|<Leader>|cD VCSDelete
|<Leader>|cd VCSDiff
|<Leader>|cg VCSGotoOriginal
|<Leader>|cG VCSGotoOriginal!
|<Leader>|ci VCSInfo
|<Leader>|cl VCSLog
|<Leader>|cL VCSLock
|<Leader>|cr VCSReview
|<Leader>|cs VCSStatus
|<Leader>|cu VCSUpdate
|<Leader>|cU VCSUnlock
|<Leader>|cv VCSVimDiff

这个 <Leader> 得看你定义了什么快捷键,vim 默认的是 \

2. vim-unite-svn

小日本写的unite.vim的svn插件,unite.vim 是一个非常不错的vim插件,
它的作用是 为vim 写简单界面,要使用vim-unite-svn首先需要安装unite.vim 插件,
折腾这个插件的原因是vcscommand.vim有些不太好的地方。

  • buffer管理的不太好
  • status 比较弱
  • 羡慕emacs的vc mode的体验

https://github.com/Shougo/unite.vim

小日本写的 vim-unite-svn 插件太久没有更新了,有一些bug,我修改了一下,用了
一段时间暂时没有发现什么问题。

https://github.com/henices/vim-unite-svn

支持基本的svn命令, svn status, svn info, svn diff, svn commit, svn up

使用示例

1
2
3
:Unite svn/status
:Unite svn/diff
:Unite svn/blame

在 vim 中使用 LeaderF 和 gtags

☆ 缘由和一些 vim 历史

前些年 vim 没有子窗口的概念,日本的一个 vim 插件作者为了解决这个问题生撸了一套流程,当时 unite.vim 横空出世惊艳了我一把。后面随着技术发展,neovim 和 vim 先后引入了 floating window ,这才使得情况变得好了一点。

https://github.com/vim/vim/blob/master/runtime/doc/popup.txt
https://neovim.io/doc/user/api.html#api-floatwin

得益于新技术,在 vim 中使用 LeaderF 插件的交互体验上了一个台阶。

Demo

neovim 从 0.9 版本开始放弃对 cscope 的支持 https://twitter.com/Neovim/status/1580933880579641344

这给我阅读源码造成了一些问题,虽然 vim 仍然可以使用 cscope,但 vim 的启动速度不如 nvim 快,所以我开始寻找替代方案。同时,我发现 denite.nvim,unite.vim,作者也不怎么维护了,一想似乎是很多年没有维护 vim 配置了。

在网络上搜索在 neovim 上阅读源码的方法,无意发现有人提到用 LeaderF 替代 tagbar,于是有了现在的这篇文章。

☆ 安装vim 插件 LeaderF

插件的仓库地址:https://github.com/Yggdroot/LeaderF/tree/master

使用 Vundle.vim 安装,在 .vimrc 中添加下面一行

1
Plug 'Yggdroot/LeaderF', { 'do': ':LeaderfInstallCExtension' }

在 vim 中执行下面命令,即可成功安装,如果遇到网络问题,自己挂上线路即可。

1
2
:so %
:PlugInstall

☆ LeaderF 配置

1
2
3
4
5
6
7
8
9
10
11
12
let g:Lf_WorkingDirectoryMode = 'Ac'
let g:Lf_RootMarkers = ['.git', '.svn', '.hg', '.project', '.root']
let g:Lf_DefaultExternalTool='rg'
let g:Lf_WindowPosition = 'popup'

let g:Lf_UseCache = 0
let g:Lf_PreviewInPopup = 1
"let g:Lf_CacheDirectory = s:cachedir
let g:Lf_ShowHidden = 1 "show hidden files

" preview or not
let g:Lf_PreviewResult = {'Function': 0, 'BufTag': 0 }
  • g:Lf_WorkingDirectoryMode = ‘AF’ 设置工作目录,如果没有找到 RootMarker 以当前文件所在目录作为工作目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
g:Lf_WorkingDirectoryMode                       *g:Lf_WorkingDirectoryMode*
This option customizes LeaderF's working directory.
e.g., >
let g:Lf_WorkingDirectoryMode = 'Ac'
<
c - the directory of the current working directory.(default)
a - the nearest ancestor of current working directory that contains one of
directories or files defined in |g:Lf_RootMarkers|. Fall back to 'c' if
no such ancestor directory found.
A - the nearest ancestor of current file that contains one of directories
or files defined in |g:Lf_RootMarkers|. Fall back to 'c' if no such
ancestor directory found.
f - the directory of the current file.
F - if the current working directory is not the direct ancestor of current
file, use the directory of the current file as LeaderF's working
directory, otherwise, same as 'c'.
Note: if "f", "F" is included with "a" or "A", use the behavior of "f" or
"F"(as a fallback) when a root can't be found.

  • g:Lf_DefaultExternalTool 索引文件使用的外部工具,可选值为 ‘rg’, ‘pt’, ‘ag’, ‘find’,作者推荐使用 rg
  • g:Lf_UseCache 列文件时是否使用缓存,默认值为 1,设置为 0 不使用缓存
  • g:Lf_WindowPosition,LeaderF 窗口的位置,可选值有 top, bottom, left, right, popup,fullscreen,默认值为 bottom
  • g:Lf_PreviewInPopup 设置是否在 LeaderF 的 popup 窗口显示预览, 默认值为 0
  • g:Lf_ShowHidden 是否显示隐藏文件和目录,默认值为 0 不显示隐藏文件
  • g:Lf_PreviewResult 设置需要显示预览的内容,默认值为全 1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    let g:Lf_PreviewResult = {
    \ 'File': 1,
    \ 'Buffer': 1,
    \ 'Mru': 1,
    \ 'Tag': 1,
    \ 'BufTag': 1,
    \ 'Function': 1,
    \ 'Line': 1,
    \ 'Colorscheme': 1,
    \ 'Rg': 1,
    \ 'Gtags': 1
    \}

配置好了 LeaderF,在 vim/neovim 的命令行输入 :Leaderf tab 就可以看到相关的命令了,如果忘记了具体的命令也没有关系,使用 LeaderfSelf 即可列出所有的 Leaderf 命令。如何想查看具体命令的使用方法,在命令窗口输入 :Leaderf -h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
subcommands:

{file,tag,function,mru,searchHistory,cmdHistory,help,line,colorscheme,gtags,
self,bufTag,buffer,rg,filetype,command,window,quickfix,loclist,jumps}
file search files
tag navigate tags using the tags file
function navigate functions or methods in the buffer
mru search most recently used files
searchHistory execute the search command in the history
cmdHistory execute the command in the history
help navigate the help tags
line search a line in the buffer
colorscheme switch between colorschemes
gtags navigate tags using the gtags
self execute the commands of itself
bufTag navigate tags in the buffer
buffer search buffers
rg grep using rg
filetype navigate the filetype
command execute built-in/user-defined Ex commands.
window search windows.
quickfix navigate the quickfix.
loclist navigate the location list.

好了,我可以放弃 fzf.vim, ctrp.vim, tagbar, unite.vim, denite.vim 等一系列 vim 插件了。 在日常使用中,每次都输入那么长的命令不太方便,我们可以配置以下容易记忆的快捷键。

1
2
3
4
5
6
7
noremap <leader>fs :LeaderfSelf<cr>
noremap <leader>fm :LeaderfMru<cr>
noremap <leader>ff :LeaderfFunction<cr>
noremap <leader>fb :LeaderfBuffer<cr>
noremap <leader>ft :LeaderfBufTag<cr>
noremap <leader>fl :LeaderfLine<cr>
noremap <leader>fw :LeaderfWindow<cr>

值得一提的是 rg 命令,可以直接在目录文件内搜索字符串,ripgrep 的正则表达式比 vim 的正则要正常多了。vim 的正则表达式的用法可以参见:https://vimdoc.sourceforge.net/htmldoc/pattern.html
以前我为了规避 vim 的正则使用了一个插件 https://github.com/othree/eregex.vim 现在这个插件也不需要了。

要利用 LeaderF 调用 rg 命令,可以使用命令行 :Leaderf! rg, 为了方便使用使用下面的快捷键查询光标所在 word

1
nmap <leader>frb <Plug>LeaderfRgCwordLiteralBoundary

运行 LeaderF 之后的操作

在执行 Leaderf 命令弹出的界面后,需要使用特定的快捷键进行操作,这些快捷键可以使用 help leaderf-prompt 进行查看。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Once LeaderF is launched:                       *prompt* *leaderf-prompt*
<C-C>, <ESC> : quit from LeaderF.
<C-R> : switch between fuzzy search mode and regex mode.
<C-F> : switch between full path search mode and name only search mode.
<Tab> : switch to normal mode.
<C-V>, <S-Insert> : paste from clipboard.
<C-U> : clear the prompt.
<C-W> : delete the word before the cursor in the prompt.
<C-J>, <C-K> : navigate the result list.
<Up>, <Down> : recall last/next input pattern from history.
<2-LeftMouse> or <CR> : open the file under cursor or selected(when
multiple files are selected).
<C-X> : open in horizontal split window.
<C-]> : open in vertical split window.
<C-T> : open in new tabpage.
<F5> : refresh the cache.
<C-LeftMouse> or <C-S> : select multiple files.
<S-LeftMouse> : select consecutive multiple files.
<C-A> : select all files.
<C-L> : clear all selections.
<BS> : delete the preceding character in the prompt.
<Del> : delete the current character in the prompt.
<Home>: move the cursor to the begin of the prompt.
<End> : move the cursor to the end of the prompt.
<Left>: move the cursor one character to the left.
<Right> : move the cursor one character to the right.
<C-P> : preview the result.
<C-Up> : scroll up in the popup preview window.
<C-Down> : scroll down in the popup preview window.
  • tab : 切换输入模式和 normal 模式,进入 normal 模式后可以使用 jk 上下移动光标来查看文件。
  • q : normal 模式中,退出 LeaderF

在输入模式下,可以使用下面的快捷键进行分屏显示。

  • ctrl + x : 横向分割
  • ctrl + ] : 竖向分割
  • ctrl + T : tab 分割

☆ LeaderF + gtags 的配置

gtags 和 LeaderF 配置起来使用,实在是很不错。首先要配置好 gtags,关于 global 的安装详见 GNU Global 的使用,这里就不再详细说了,gtags 的配置添加下面两行即可。

1
2
let $GTAGSLABEL='native-pygments'
let $GTAGSCONF='/usr/local/share/gtags/gtags.conf'

配置好系统 gtags,global ,gtags.conf 的正确路径。

1
2
3
4
let g:Lf_Global = '/usr/local/bin/global'
let g:Lf_Gtags = '/usr/local/bin/gtags'
let g:Lf_Gtagsconf = '/usr/local/share/gtags/gtags.conf'
let g:Lf_Gtagslabel = 'native-pygments'

下面两行是配置gtags 自动生成 tags 文件,主要是三个文件 GPATH , GRTAGS GTAGS

1
2
let g:Lf_GtagsAutoGenerate = 1
let g:Lf_GtagsAutoUpdate = 1
  • g:Lf_GtagsAutoGenerate 是否自动生成 tags 文件。设置成 1 并且在 g:Lf_RootMarkers 定义的目录中,tags 会自动生成
  • g:Lf_GtagsAutoUpdate 是否自动更新 tags。如果设置成 0 ,需要先执行 Leaderf gtags --update 手动更新 tags 文件

tags 文件生成的目录和 g:Lf_CacheDirectory 的配置相关, g:Lf_CacheDirectory 用于设置缓存文件目录,默认的目录是 $HOME (Windows 上会有变化)使用 _ 替换了 /

1
2
ls ~/.LfCache/gtags/_home_henices_.vim 
GPATH GRTAGS GTAGS

完成了上面的配置后,我们就可以正常使用 LeaderF 来对 gtags 生成的 tags 文件进行浏览了,在命令行使用 :Leaderf gtags -h 可以看到详细的命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
:Leaderf gtags -h
usage:
Leaderf[!] gtags [-h] [--remove] [--recall]
Leaderf[!] gtags --update [--gtagsconf <FILE>] [--gtagslabel <LABEL>] [--accept-dotfiles]
[--skip-unreadable] [--skip-symlink [<TYPE>]] [--gtagslibpath <PATH> [<PATH> ...]]
Leaderf[!] gtags [--current-buffer | --all-buffers | --all] [--result <FORMAT>] [COMMON_OPTIONS]
Leaderf[!] gtags -d <PATTERN> [--auto-jump [<TYPE>]] [-i] [--literal] [--path-style <FORMAT>] [-S <DIR>]
[--append] [--match-path] [--gtagsconf <FILE>] [--gtagslabel <LABEL>] [COMMON_OPTIONS]
Leaderf[!] gtags -r <PATTERN> [--auto-jump [<TYPE>]] [-i] [--literal] [--path-style <FORMAT>] [-S <DIR>]
[--append] [--match-path] [--gtagsconf <FILE>] [--gtagslabel <LABEL>] [COMMON_OPTIONS]
Leaderf[!] gtags -s <PATTERN> [-i] [--literal] [--path-style <FORMAT>] [-S <DIR>] [--append]
[--match-path] [--gtagsconf <FILE>] [--gtagslabel <LABEL>] [COMMON_OPTIONS]
Leaderf[!] gtags -g <PATTERN> [-i] [--literal] [--path-style <FORMAT>] [-S <DIR>] [--append]
[--match-path] [--gtagsconf <FILE>] [--gtagslabel <LABEL>] [COMMON_OPTIONS]
Leaderf[!] gtags --by-context [--auto-jump [<TYPE>]] [-i] [--literal] [--path-style <FORMAT>] [-S <DIR>]
[--append] [--match-path] [--gtagsconf <FILE>] [--gtagslabel <LABEL>] [COMMON_OPTIONS]
[COMMON_OPTIONS]: [--reverse] [--stayOpen] [--input <INPUT> | --cword]
[--top | --bottom | --left | --right | --belowright | --aboveleft | --fullScreen]
[--nameOnly | --fullPath | --fuzzy | --regexMode] [--nowrap] [--next | --previous]

...

LeaderF gtags 最常用的 :Leaderf[!] gtags -d/-r/-s/-g, 具体的作用如下所示,! 表示直接进入 normal 模式。

1
2
3
4
5
6
7
8
9
--gtagslibpath <PATH> [<PATH> ...]
Specify the paths to search for library functions.
-d <PATTERN>, --definition <PATTERN>
Show locations of definitions.
-r <PATTERN>, --reference <PATTERN>
Show reference to a symbol which has definitions.
-s <PATTERN>, --symbol <PATTERN>
Show reference to a symbol which has no definition.
-g <PATTERN>, --grep <PATTERN>

日常使用中,同样可以配置快捷键方便使用,下面的快捷键将对光标所在单词进行 tag 查找。

1
2
3
4
nmap <leader>fgd <Plug>LeaderfGtagsDefinition
nmap <leader>fgr <Plug>LeaderfGtagsReference
nmap <leader>fgs <Plug>LeaderfGtagsSymbol
nmap <leader>fgg <Plug>LeaderfGtagsGrep

运行 LeaderF gtags 之后

使用 LeaderF + gtags 跳转到源码后,如果想继续跳转到下一个或者上一个 tag 可以使用 Leaderf gtags --nextLeaderf gtags --previous

1
2
3

noremap <leader>fgn :<C-U><C-R>=printf("Leaderf gtags --next %s", "")<CR><CR>
noremap <leader>fgp :<C-U><C-R>=printf("Leaderf gtags --previous %s", "")<CR><CR>

☆ LeaderF 的其他用法

LeaderF 还可以和 Gutentags 配合使用进行源码浏览,但已经超出本文内容,有兴趣者可以查阅网络上的其他资料。

☆ 参考资料

https://github.com/Yggdroot/LeaderF/blob/master/doc/leaderf.txt
https://retzzz.github.io/dc9af5aa/
https://github.com/Yggdroot/LeaderF/blob/master/README.md
https://zhuanlan.zhihu.com/p/36279445
https://github.com/vim/vim/blob/master/runtime/doc/popup.txt
https://neovim.io/doc/user/api.html#api-floatwin

GNU Global 的使用

☆ 1. GNU Global 简介

GNU Global 是一种源代码标记系统,可以帮助程序员在代码库中快速定位函数、变量、宏定义等。GNU Global 可以生成索引文件,用于快速跳转到定义、引用、函数调用等地方,从而提高代码阅读和编辑的效率。

GNU global 具有以下优势:

  1. 多语言支持:在默认情况 GNU Global 支持 C, C++, Yacc, Java, PHP4 和 assembly 语言,在安装拓展的情况下可以支持 50多种语言。
  2. 完整性:GNU global 可以标记更多符号的位置,如定义、引用、结构体、类,宏定义等。
  3. 高效性:GNU global 生成高效的 tag 数据库,节省了磁盘空间,使得查询速度更快,同时也支持增量更新索引文件,这意味着当源代码发生变化时,只需要重新生成变更的部分,而不需要重新生成整个索引文件。
  4. 可集成:可以在 Emacs, Vi /vim, Less viewer, Bash shell, 浏览器环境下正常使用
  5. 持续更新: https://www.gnu.org/software/global/whatsnew.html

更多信息请参考: https://www.gnu.org/software/global/globaldoc_toc.html 1.3 Features

☆ 2. GNU Global 的安装

下载地址: https://ftp.gnu.org/pub/gnu/global/global-6.6.9.tar.gz

https://www.gnu.org/software/global/download.html 中说明了源码编译方法:

1
2
3
% sh reconf.sh 
% ./configure
% make

Fedora/Debian 等系统可以使用仓库直接安装,下面以 Fedora Linux 为例

1
sudo dnf install global-ctags global cscope

如果不安装 global-ctags 在 Fedora 下可能会报 Cannot enable custom plug-in parser for GNU GLOBAL 的错误。

gtags-cscope 由 global 安装包提供不用另外安装了。

1
2
3
4
5
6
7
8
9
10
11
sudo dnf whatprovides gtags-cscope

global-6.6.5-7.fc37.x86_64 : Source code tag system
仓库 :@System
匹配来源:
文件名 :/usr/bin/gtags-cscope

global-6.6.5-7.fc37.x86_64 : Source code tag system
仓库 :fedora
匹配来源:
文件名 :/usr/bin/gtags-cscope

macOS 下可以使用 brew 安装 global,执行 brew install global 即可。
参见 https://www.gnu.org/software/global/download.html

如果需要gtags支持 C/C++/Java 之外的语言,需要安装 pygments

1
pip3 install pygments

☆ 3. GNU global 和 cscope, vim 集成

vim 的 cscope 支持可能需要重新编译 vim,可以通过下面的命令查询。

1
2
vim --version | grep cscope
+cscope +localmap +ruby/dyn +wildignore

GNU global 提供了两个 vim 插件用于和 vim 结合,分别为 gtags.vim 和 gtags-cscope.vim,可以直接拷贝到 vim 的插件目录使用。

1
2
cp /usr/local/share/gtags/gtags.vim $HOME/.vim/plugin
cp /usr/local/share/gtags/gtags-cscope.vim $HOME/.vim/plugin

如果是系统仓库安装路径可能有变化,Fedora 上的 gtags.vim 路径为 /usr/share/gtags/gtags.vim

gtags-cscope.vim 定义了一组快捷键和以前的 cscope_maps.vim 一致

1
2
3
4
5
6
7
8
:nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
:nmap <C-\>i :cs find i <C-R>=expand("<cfile>")<CR><CR>
:nmap <C-\>a :cs find a <C-R>=expand("<cword>")<CR><CR>

gtags-cscope.vim 的使用注释里有详细的说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
" Usage
" -----
" First of all, you must execute gtags(1) at the root of source directory
" to make tag files. Assuming that your source directory is '/var/src',
" it is neccessary to execute the following commands.
"
" [Load vim]
" $ cd /var/src
" $ gtags
" $ vim
" [Load gtags-cscope]
" :GtagsCscope <ENTER> (in vim command line)
"
" Basic command
" -------------
" Then you can use cs commands except for the 'd'(2) command.
" Profitable commands are assigned to keys like follows:
"
" explanation command
" ----------------------------------------------------------
" Find symbol :cs find 0 or s
" Find definition :cs find 1 or g
" Find functions called by this function (not implemented)
" Find reference :cs find 3 or c
" Find text string :cs find 4 or t
" Find egrep pattern :cs find 6 or e
" Find path :cs find 7 or f
" Find include file :cs find 8 or i
" Find assignments :cs find 9 or a
"
" You can move tag list using:
" Go to the next tag :tn
" Go to the previous tag :tp
" Pop tag stack :pop

除了不能使用 :cs find d 命令,可以使用其他所有的命令,gtags-cscope.vim 使用快捷键替代了输入命令,常用的快捷键的含义如下:

1
2
3
4
5
<C-\>g - 查看光标下符号的定义
<C-\>s - 查看光标下符号
<C-\>c - 查看光标下符号的引用
<C-\>f - 查找光标下的文件
<C-\>i - 查找哪些文件 include 了本文件

<C-\>g 是同时按下 Ctrl 和 \ ,接着再按 g , 其他的同理。

在 .vimrc 中添加下面的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if has("cscope")
" To use the default key/mouse mapping:
let GtagsCscope_Auto_Map = 1

" To deterring interruption:
let GtagsCscope_Keep_Alive = 1

set cscopetag
set csprg=/usr/bin/gtags-cscope
set csto=1
set nocsverb

" Set enviroment values
let $GTAGSLABEL='native-pygments'
let $GTAGSCONF='/etc/gtags.conf'

endif

☆ 4. GUN global 的使用方法

切换到源码目录,执行下面的命令将生成 Global 所需要的索引文件。

1
2
find . -type f -name "*.h" -o -name "*.hpp" -o -name "*.hh" -o -name "*.hxx" -o -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.cxx" > gtags.files
gtags -f gtags.files

上面的命令执行完毕后,会生成 GPATH、GRTAGS、GTAGS 几个索引文件。

在 vim 中执行 :GtagsCscope <ENTER> 即可加载索引文件,最后就是使用快捷键愉快地浏览源码文件了。

参考链接

vim中的python

1. 判断vim支持python

要判断你的vim是否支持python,可以执行下面命令:

1
$ vim --version | grep python

如果显示的结果中有 +python 的话,你的vim已经支持python,
如果没有的话,你需要重新编译vim,在windows中可以使用下面的命令行,

1
nmake -f Make_mvc.mak FEATURES=huge PYTHON=D:\Python27 PYTHON_VER=27 GUI=yes IME=yes

2. 为什么需要编译支持python的vim

主要的理由有两个,

  1. 可以使用python写的vim插件。
  2. vim 7.4 以后对python的支持进行了增强。

有很多不错的vim 插件,这里给大家推荐一个python-mode, 非常不错。https://github.com/klen/python-mode

vim 7.4 以后对python的支持进行了增强,这个是广大vim注册用户(捐钱的用户)呼声最
高的需求,原话为,

add integration with Python instead of inventing more Vim script

确实现在已经有太多的vim脚本了。vim的主页上的完整需求列表; http://www.vim.org/sponsor/vote_results.php

add IDE features (debugger integration, shell window)

这个是排行老二的需求,不知道什么时侯能实现,期待中…

3. 一些应用举例

3.1 python commands

1
:[range]py[thon] {stmt}

这个命令比较简单,详细解释如下:

range 是标准的vim的range 熟悉vim替换命令的同学应该都不会陌生。range的作用的是指
定python命令影响哪些行,写法为 [行号,行号],比如 1,2 影响第一行和第二行,1,$
影响整个文件。[] 的意思是这个range是可选的默认将影响整个文件。

py[thon]的意思为,python 是命令的全写,可以简写为py。

{stmt} python 语句

可以写上下面命令,输出 Hello

1
:python print "Hello python"

数学不好的同学可以像我一样使用

1
:py print 1+1

将显示结果2 :)

1
2
3
:[range]py[thon] << {endmarker}
{script}
{endmarker}

上面是这个命令是第二种用法,可以直接写python,相当爽,写法很象cat 命令

1
2
3
cat > /tmp/xx << EOF
....
EOF

想用vim查询天气的同学可以参考一下,github上的这个脚本 https://raw.github.com/liqunli/vim-plugin-dev/master/dairy.vim

3.2 pydo

这个命令的格式为, :[range]pydo {body}

vim 中有一个定义好的函数,这个命令将利用这个函数做一些事情

1
2
def _vim_pydo(line, linenr):
{body}

pydo 这个命令同样是以行为单位的,range的用法和上面说的一样。 line 是原始的行,
linenr 是 line number (行号)。大家可以猜一下下面命令的意思,

1
:pydo if line: return "%4d: %s" % (linenr, line)

显示结果大致是这样, 直接用这个文件测试了一下,

1
2
3
4
5
6
55: 将显示结果2  :)


58: :[range]py[thon] << {endmarker}
59: {script}
60: {endmarker}

没有指定range,默认为整个文件,”%4d: %s” % (linenr, line) 这句的意思是,
在每行前加行号,不足4位的数字的用空格补齐

在早些时侯,vim命令要实现这个的效果,需要写一行诡异的命令,

1
:%s/^/\=printf('%4d: ', line('.'))

挑战太大,vim 还专门写了一篇wiki http://vim.wikia.com/wiki/Insert_line_numbers

3.3 pyfile

这条命令的格式为,:[range]pyf[ile] {file}

可以直接使用python 文件,不需要一句一句的输入python 语句了。

1
2
3
4
5
:python from vim import *
:python from string import upper
:python current.line = upper(current.line)
:python print "Hello"
:python str = current.buffer[42]

3.4 python3

命令的格式和python是一样的,不过需要开启 +python3

3.5 vim的python 接口

这个不属于这个小文章的想说的了,写了这成vim的manual了,可以直接参看:
http://vimhelp.appspot.com/if_pyth.txt.html#python-vim

4. 最后

为了避免vim和emacs的圣战,hx 和 lzx等emacs党,可以直接忽视此文 :)

5. 参考资料

http://vimhelp.appspot.com/if_pyth.txt.html