vim官网上有现成的插件增加vim的语法颜色,一直弄不好,自己动手丰衣食。原理比较简单,通过ctags工具生成tags文件,再从tags文件生成vim的source文件,然后vim的source命令执行它。
1. 下载一个支持Lua的vim,学了Lua手痒,并且对vim的script不大懂,函数是由用Lua写的;
2. 下载一个ctags,cygwin貌似带有,也可能冲突,下成别的,确认一下版本; 类似
Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
Compiled: Jul 9 2009, 17:05:35
Addresses: <dhiebert@users.sourceforge.net>, http://ctags.sourceforge.net
Optional compiled features: +win32, +regex, +internal-sort
3.写个Lua文件,命名为stag.lua; 可再自己修改它,加入更多的个人设定,如已知的tag,改改颜色什么的;
local tags_vim=io.open("tags.vim", "w");
local hl={};
hl.f="Function";
hl.m="Member";
hl.d="Define";
hl.s="Struct";
hl.v="Variable";
hl.t="Typedef";
--ctags -R -V -f ./tags
io.input("tags")
for line in io.lines() do
local id, idtype=string.match(line, "^(.-)\t.-;\"\t(.)");
if (id) then
local hltype=hl[idtype];
hltype=hltype or "Type";
tags_vim:write("syn keyword " .. hltype .. " " .. id .. "\n");
end
end
tags_vim:write(
[[
syn keyword Typedef string
syn keyword Typedef std
syn keyword Typedef auto_ptr
highlight Function gui=none guifg=navy guibg=bg ctermfg=Green
highlight Member gui=none guifg=#808080 guibg=bg ctermfg=DarkMagenta
highlight Define gui=none guifg=#a055df guibg=bg ctermfg=White
highlight Struct gui=none guifg=#7f0055 guibg=bg ctermfg=LightGreen
highlight Variable gui=none guifg=blue guibg=bg ctermfg=LightGreen
highlight Typedef gui=none guifg=blue guibg=bg ctermfg=Yellow
]]
);
tags_vim:close();
4 .在gvimrc中给绑定两个键,看个人喜欢;
map <F11> :call Myhl() <CR><F12>
map <F12> :so tags.vim<CR>
function! Myhl()
lua << EOF
dofile("stag.lua");
EOF
endfunction
5 .到源代码的目录下运行ctags.exe,生成tags文件,在vim中按一下F11,就能看到自己喜欢的颜色设定了