每天工作中都需要频繁在一些文件之间切换:
1.txt 2.txt 3.txt
类似这样的文件列表, 下面的脚本可以上我使用快捷键在这些文件之间切换:
* 如果已经有上述列表中任何一个文件已经打开, 就使用那个已经打开的缓冲区
* 自动切换到该缓冲区所在的tabpage
* 如果还没有打开上述任何一个文件, 在一个新的tabpage中打开该文件.
* 打开新文件之间自动保存原来打开的文件.
function! g:SwitchTips(fname)
let tab_no = 0
let buf_no = 0
let found_global = 0
let all = ""
if v:servername != 'DAILY_GVIM'
silent call remote_foreground("DAILY_GVIM")
silent call remote_send("DAILY_GVIM", ":call g:SwitchTips('" . a:fname . "')")
return
endif
for i in range(tabpagenr('$'))
" Find the target buffer whose file is tips file, not need be one of
" misc,c,csharp,xml,pc-lint
let found = 0
let tab_no = i + 1
for buf_n in tabpagebuflist(tab_no)
let buf_no = buf_n
let full_fname = bufname(buf_n)
" If not an absolute name then must be the in the current directory, expand it to
" a full file name
if( full_fname !~ '[\\/]' )
let full_fname = getcwd() . '\' . full_fname
endif
" If a tip XML file?
let all = all . " " . full_fname . "[" . buf_no . "]"
if( full_fname =~? '^D:\\work\\tips\\[^\\/.]\+\.xml$' )
let found = 1
break
endif
endfor
if found
let found_global = 1
break
endif
endfor
if (found_global && strlen(a:fname) == 0)
return
endif
let full_fname = 'D:\work\tips\' . a:fname
" echo "Files: " . all . ", global found: " . found_global . ",tabpage: " . tab_no . ", buf_no: " . buf_no . ",winnr = ". bufwinnr(buf_no)
" return
if found_global
if( tabpagenr() != tab_no )
exe 'tabn ' . tab_no
endif
exe 'norm ' . bufwinnr(buf_no) . "\w"
if( filereadable(full_fname) )
update
exe 'edit ' . full_fname
else
echoerr "File [" . full_fname . "] not exist"
endif
else
if( strlen(a:fname) == 0)
let full_fname = 'D:\work\tips\misc.xml'
endif
if( filereadable(full_fname) )
exe 'tabe ' . full_fname
else
echoerr "File [" . full_fname . "] not exist"
endif
endif
endfunction
nnoremap :se enc? fenc? fencs?
nnoremap :call g:SwitchTips('misc.xml')
nnoremap :call g:SwitchTips('c.xml')
nnoremap :call g:SwitchTips('csharp.xml')
nnoremap :call g:SwitchTips('xml.xml')
nnoremap :call g:SwitchTips('pc-lint.xml')
阅读(984) | 评论(0) | 转发(0) |