在vim下快速切换文件

公司项目大量用到html和js,自己的项目也有大量的.h和.cpp,在编写代码的时候需要经常在对应的文件之间切换,使用buffer或者e命令切换都很麻烦,后来在别人的推荐下使用了fuzzyfinder后,效率确实提高了不少,我一般在有限的几个文件中切换,所以开新文件我还是用e命令,然后buffer之间切换使用fuzzyfinder,我是这么配置的:

noremap <C-T> :FufBuffer<cr>

这样在输入Ctrl-T的时候会让你输入buffer的名字,支持模糊匹配,然后可以输入一部分名字后按回车,然后vim就切过去了,确实很方便,但是不够更方便,特别是对于以下的文件之间切换:

<td>
  tab-livechat.html
</td>
<td>
  net/src/clhttp.cpp
</td>

然后跟着 https://www.ibm.com/developerworks/cn/linux/l-vim-script-1/ 学了下,试着写了一段脚本来完成了在这样的文件之间切换的脚本:

function! OpenOther()
    let lookups = ["%:p:r:s?src?inc?", "%:p:r:s?inc?src?", "%:p:r"]
    let other_type = {'vs' : 'fs', 'fs' : 'vs', 'html' : 'js', 'js' : 'html', 'h' : 'cpp', 'cpp' : 'h'}
    let type = expand("%:e")
    if has_key(other_type, type)
        let dst_types = get(other_type, type)
        for pattern in lookups
            let file = fnameescape(expand(pattern) . "." . dst_types)
            if filereadable(file)
                exec "e " file
                return
            endif
        endfor
    endif
endfunction
noremap &lt;C-i> :call OpenOther()&lt;CR>

在编辑的过程中,输入 Ctrl + i就可以自动在源文件和对应的头文件中间切换了,工作效率又提升了不少。


Last modified on 2013-08-28