vim 个性化设置与功能扩展均通过 script 来实现,这种 script 又叫 plugin。plugin 是 vim 的核心与精髓。

最常用的配置文件 vimrc,也是一种 plugin。换句话说,所有的 plugin 都在配置 vim 的行为。

plugin 类型及其相互关系

常见的 plugin 类型:

  • vimrc
  • global
  • filetype
  • syntax
  • compiler

vimrc 是核心 plugin (类似 main 函数),所有的配置都可以在这一个文件中完成。 所有其他配置都直接或间接由该文件调用以生效。

与其他编程语言一样,为了提高源文件的可读性,增加代码的可重用性等, 衍生出了 global/filetype/syntax/compile 等 4 中主要的 plugin。

通过 runtime, source 等命令加载其他 plugin。

vim 搜索路径

vim 通过路径区分 plugin 类型。 由 runtimepath 控制搜索顺序,一旦找到立即停止。

默认搜索顺序:

  1. $HOME/.vim: linux home 目录下的用户个性化设置
  2. `$VIMRUNTIME: 系统自带的 plugin

建议将个性化配置全部放在 $HOME/.vim 中, 与 vim 自带的配置文件隔离,方便备份。 vim 中执行 :echo $variable 查看变量取值.

查看默认 plugin 搜索路径:

:set runtimepath?
runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/
vim73,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after

plugin 安装

拷贝到对应的 plugin 目录下即可。

更新帮助文档,在 vim 中运行 :helptags ~/vimfiles/doc

global plugin

global plugin 位于 plugin 目录下,对全局生效,所以默认自动加载。

filetype plugin

filetype plugin 只对特定类型的文件生效。

根据文件类型动态决定行为,通过事件监听(autocmd)实现。 基本原理如下:

" required
filetype plugin on
" define behavior when filetype change to `tt`
" here, the behavior is set to execute echo "hello"
autocmd FileType tt echo "hello"
" execute cmd below, output "hello" at the bottom of vim window
:set filetype=tt

如果没有用 autocmd 显式声明 xxx 文件对应的行为, 则加载 ftplugin 目录下的 xxx.vim

:set filetype=c
"expt: load c.vim and change highlight in c syntax
"required: c.vim in ftplugin, it is in $VIMRUNTIME/ftplugin by default

注意:使用 ftplugin 时,需要开启 filetypefiletype plugin

:filetype plugin on  " 开启 filetype 和 filetype plugin
:filetype  " 查看状态,输出如下
filetype detection:ON  plugin:ON  indent:ON

一个更深入 filetype plugin 的例子

autocmd BufNewFile,BufRead *.xml source ~/.vim/ftplugin/xml.vim

同样是事件监听机制,区别在于:

  • 监听的事件: 创建或打开 .xml文件(BufNewFile,BufRead)
  • 执行的行为: 加载 ftplugin/xml.vim

syntax plugin

原理与 ftplugin 类似,监听 syntax 事件,加载 syntax 目录下的插件。 触发 filetype 事件的同时,也会触发对应的 syntax 事件。

syntax on
:set syntax=c

compile plugin

同样是事件监听,插件位于 compiler 目录。

" add below to vimrc
autocmd BufNewFile,BufRead *.py compiler python`
" open a file ending with .py and run
:make
" expt: errors printed

更多的一些细节

与 vi 的兼容性。

vim 在 vi 的基础上进行了很多的功能扩展。 若要使用 vim 的特性,vim 必须运行于与 vi 不兼容的模式。

一般,把 set noncompaible放在配置文件开始处即可。
ubuntu 12.04 默认的 vimrc 先加载 ubuntu.vim, 其中设置了该选项。

vim feature-list

vim 自身有很多 feature, plugin 都依赖于相应的 feature.

为了使 vimrc 通用,使用 plugin 前先用has() 检测feature 是否存在。
如下:

if has("syntax")
  syntax on
endif

:version 查看 feature-list

:version
VIM - Vi IMproved  Aug , compiled May    ::)
包含补丁: -
修改者 pkg-vim-maintainers@lists.alioth.debian.org
编译者 buildd@
巨型版本 无图形界面。  可使用(+)与不可使用(-)的功能:
+arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent -clientserver -clipboard +cmdline_compl +cmdline_hist
+cmdline_info +comments +conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs -dnd -ebcdic +emacs_tags +eval +ex_extra
 +extra_search +farsi +file_in_path +find_in_path +float +folding -footer +fork() +gettext -hangul_input +iconv +insert_expand +jumplist
 +keymap +langmap +libcall +linebreak +lispindent +listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape +mouse_dec
 +mouse_gpm -mouse_jsbterm +mouse_netterm -mouse_sysmouse +mouse_xterm +mouse_urxvt +multi_byte +multi_lang -mzscheme +netbeans_intg
 +path_extra -perl +persistent_undo +postscript +printer +profile +python -python3 +quickfix +reltime +rightleft -ruby +scrollbind +signs
 +smartindent -sniff +startuptime +statusline -sun_workshop +syntax +tag_binary +tag_old_static -tag_any_white -tcl +terminfo +termresponse
 +textobjects +title -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo +vreplace +wildignore +wildmenu +windows
 +writebackup -X11 -xfontset -xim -xsmp -xterm_clipboard -xterm_save
  系统 vimrc 文件: "VIM/vimrc"
  用户 vimrc 文件: "HOME/.vimrc"
    用户 exrc 文件: "HOME/.exrc"
       VIM 预设值: "/usr/share/vim"
编译方式:  -Wformat -Wformat-security -Werror=format-s
ecurity -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=
链接方式: gcc   -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed -o vim  -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl     -L
/usr/lib/python2./config -lpython2. -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
请按 ENTER 或其它命令继续

vim plugin 原理的更多相关文章

  1. MyBatis源码分析(2)—— Plugin原理

    @(MyBatis)[Plugin] MyBatis源码分析--Plugin原理 Plugin原理 Plugin的实现采用了Java的动态代理,应用了责任链设计模式 InterceptorChain ...

  2. Good Vim plugin for python [Vim python mode]

    Here I got a very neat plugin for vim which is awesome indeed. It's from youtube years before. So le ...

  3. 十個必用的 Vim Plugin

    ◎ The NERD Tree 操作 Vim 時,通常都在 Terminal 底下作用,無法像一般的 GUI    應用程式可以以樹狀目錄來瀏覽檔案. The NERD Tree    是一將檔案目錄 ...

  4. VIM从原理上认识^M问题

    问题背景 VIM在打开文件的时候如果遇到两种换行符风格(dos与unix)共存的文件,通常会在行尾显示出烦人的^M.如果^M较少,比较容易定位到哪几行出了问题,但是如果^M较多,就很难搞.下面先给出解 ...

  5. webpack的plugin原理

    plugin是webpack生态的重要组成,它为用户提供了一种可以直接访问到webpack编译过程的方式.它可以访问到编译过程触发的所有关键事件. 1. 基本概念 1. 如何实现一个插件 1. plu ...

  6. gvim work notes.. a few days' work on 64bit vim and plugin compilations

    (a 600MB+ sized c/c++ compiler which is capable of hi-light and JB styled completion!! and of-course ...

  7. 聊聊Vim的工作原理

    聊聊Vim的工作原理 日常里一直在用Vim这个编辑器,前阵子学习关于Linux中的fd(文件描述符)时,发现vim的进程描述符会比上一个自动加一,后续了解到vim的工作原理后,解开了这个疑问,所以记录 ...

  8. vim深入研究

    About VIM--Unix及类Unix系统文本编辑器 Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.VIM是纯粹的自由软件. Vim普遍被推崇 ...

  9. Ubuntu14 搭载vim环境查看源码

    首先是下载完整的vim74,然后编译安装.遗憾的是当编译时,没有开启图形界面. 在安装新版本的Vim之前,你需要卸载原来安装的老版本Vim,依次在终端下执行下列命令: sudo apt-get rem ...

随机推荐

  1. X265编码效率仍然低

    本次测试软件环境:Intel Celeron双核 2.60 Ghz CPU; 4GB 内存:安装 Ubuntu 13.04 hzsx@hzsx-server:~$ lsb_release -a No ...

  2. hdoj 1253 胜利大逃亡

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. Robot Framework自动化测试(二)第一个用例

    RIDE启动界面: 首先创建一个Test project File-New Project ,选择Directory类型 在创建的文件夹上右键,创建一个Test Suite Openbaidu, NE ...

  4. JQuery- 解析JSON数据

    我们先以解析上例中的comments对象的JSON数据为例,然后再小结jQuery中解析JSON数据的方法.上例中得到的JSON数据如下,是一个嵌套JSON: {,,"nickname&qu ...

  5. 皮皮果模式系统开发app

    皮皮果系统定制开发,皮皮果网站系统开发. 皮皮果游戏横空出世,比QQ农场有过之而无不及,深得广大玩家喜爱.无论草根百姓还是达官贵人,都可以来学习娱乐的游戏.让你在娱乐中收获物质生活和精神生活,英伦果园 ...

  6. Data Structure导论的导论

    DataStructure是一组互相之间存在一种或多种特定关系的数据的组织方式和它们在计算机内的存储方式,以及定义在该数据上的一组操作. 从以上定义可知,数据结构是研究数据的组织.存储方式及其操作的学 ...

  7. nginx模块编程之获取客户ip及端口号

    ngx_request_t结构体中有一个connection定义,该定义指向一个ngx_connection_t的结构体: 结构体定义如下: struct ngx_connection_s { voi ...

  8. java并发 使用ScheduledExecutor的温室控制器--thinking in java 21.7.5

    package org.rui.thread.newc; import java.text.DateFormat; import java.text.SimpleDateFormat; import ...

  9. 在含有null值的复杂类的集合(Collection)中取最大值

    在日常编程中,经常遇到要在一组复杂类的集合(Collection)中做比较.取最大值或最小值. 举个最简单的例子,我们要在一个如下结构的集合中选取包含最大值的元素: public class Clas ...

  10. Margin和Padding之间的区别

    margin ,padding body他们之间的区别就是 margin表示的是外边框的距离 padding表示的是内边框的距离 body表示的边框的距离