2011-03-04 43 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 

希望這個數字表明瞭這一邏輯。 :)