2011-03-04 71 views
8

我想制作一个vim备忘单插件。这很简单:Vim脚本:缓冲区/备忘单切换

  • 我想切换我的作弊表。一个vertexlit切换,如Taglist或NERDTree。
  • 我希望cheatsheet是文件类型特定的。所以当我打开一个.cpp文件时,我切换了我的C++ cheatsheet。
  • 我想让cheatsheet水平分割。所以它显示了两个文件,我的语法备忘单和我的片段触发备忘单。

我已经有了vimhelp格式的这些备忘录集合,但现在我必须手动打开它们。

我还没有真正做过任何vim脚本,但我想这将是非常简单的放在一起。我八九不离十生病谷歌搜索无关codesnippets的,所以我要问这里是:

  1. 任何人都可以给我什么,我需要在关于学习VIM脚本拼凑在一起,这很短的总和,达。我很难找到的是如何切换缓冲区窗口。

  2. 如果您知道任何介绍教程,涵盖了我需要启动并运行的资料,请提供一个链接。

TX,

aktivb

回答

2

我已经忘记了这件事,直到我得到关于Eduan的答案的通知。由于我发布了这个问题,我已经完成了相当多的vim脚本,包括让它工作:

let g:cheatsheet_dir = "~/.vim/bundle/cheatsheet/doc/"                  
let g:cheatsheet_ext = ".cs.txt" 

command! -nargs=? -complete=customlist,CheatSheetComplete CS call ToggleCheatSheet(<f-args>)   
nmap <F5> :CS<CR> 

" strip extension from complete list  
function! CheatSheetComplete(A,L,P) 
    return map(split(globpath(g:cheatsheet_dir, a:A.'*'.g:cheatsheet_ext)),       
     \ "v:val[".strlen(expand(g:cheatsheet_dir)).              
     \ ":-".(strlen(g:cheatsheet_ext) + 1)."]")                
endfun 

" specify cheatsheet or use filetype of open buffer as default 
" instead of saving window status in a boolean variable, 
" test if the file is open (by name). If a boolean is used, 
" you'll run into trouble if you close the window manually with :wq etc       
function! ToggleCheatSheet(...) 
    if a:0  
    let s:file = g:cheatsheet_dir.a:1.g:cheatsheet_ext 
    else          
    if !exists("s:file") || bufwinnr(s:file) == -1                     
     let s:file = g:cheatsheet_dir.&ft.g:cheatsheet_ext 
    endif 
    endif          
    if bufwinnr(s:file) != -1 
    call ToggleWindowClose(s:file) 
    else 
    call ToggleWindowOpen(s:file) 
    endif     
endfun    


" stateless open and close so it can be used with other plugins    
function! ToggleWindowOpen(file)   
    let splitr = &splitright 
    set splitright       
    exe ":vsp ".a:file 
    exe ":vertical resize 84" 
    if !splitr  
    set splitright 
    endif       
endfun 

function! ToggleWindowClose(file)  
    let w_orig = bufwinnr('%') 
    let w = bufwinnr(a:file) 
    exe w.'wincmd w' 
    exe ':silent wq!'  
    if w != w_orig   
    exe w_orig.'wincmd w' 
    endif 
endfun            
2

了以下功能可能无法做你想要什么,我没有测试它,但它应该给你一些想法。

主要思想是该函数读取当前缓冲区的文件类型(可以通过输入:echo &ft来测试),然后设置合适的作弊sheat的路径。如果存在,则在拆分窗口中打开此路径(只读且不可修改)。然后,您可以按照您所希望的方式调用此函数,例如通过将其映射到{F5}键,如图所示。

我不确定切换的可能性(这是否比只关闭拆分窗口更容易?),但您可以查看bufloaded()函数,该函数返回当前是否访问给定文件。

function! Load_Cheat_Sheet() 
    let l:ft = &ft 

    if l:ft == 'html' 
     let l:path = 'path/to/html/cheat/sheet' 
    elseif l:ft == 'c' 
     let l:path = 'path/to/c/cheat/sheet' 
    elseif l:ft == 'tex' 
     let l:path = 'path/to/tex/cheat/sheet' 
    endif 

    if l:path != '' && filereadable(l:path) 
     execute ':split +setlocal\ noma\ ro ' l:path 
    endif 
endfunction 

map <F5> :call Load_Cheat_Sheet()<CR> 

希望这会有所帮助。只要有什么不清楚的地方就喊,或者你想知道更多。

+0

谢谢,它不是我想要的,但它让我开始了。但是,我想要切换。我将需要这些用于我计划的其他脚本。 – aktivb 2011-03-07 21:25:37

+0

@aktivb你有没有得到这个工作?我期待着做同样的事情 – 2012-07-13 17:59:00

1

想到我会添加到Goulash的答案。

我认为为了实现切换,您只需使用一些if语句和全局变量。

let g:cheatsheet_toggle_on=0 

if (g:cheatsheet_toggle_on == 0) 
    " Turn the cheatsheet on 
    " Also make sure to know that the toggle is on: 
    let g:cheatsheet_toggle_on=1 
elseif (g:cheatsheet_toggle_on=1 
    " Do whatever you need to turn it off, here 
endif 

希望这个数字表明了这一逻辑。 :)