Vim脚本:Buffer/CheatSheet Toggle

akt*_*ivb 8 vim scripting buffer toggle

我想制作一个vim备忘单插件.这很简单:

  • 我想切换我的秘籍表.一个vertsplit切换,如Taglist或NERDTree.
  • 我希望cheatsheet是特定于文件类型的.因此,当我打开.cpp文件时,我切换了我的c ++ cheatsheet.
  • 我想要将cheatsheet水平分割.所以它显示了两个文件,我的语法备忘单和我的代码段触发器备忘单.

我已经有了这些cheatsheets的集合,采用vimhelp格式,但现在我必须手动打开它们.

我还没有真正完成任何vim脚本,但我想这可以很简单.我有点不习惯谷歌搜索不相关的代码片段,所以我在这里问的是:

  1. 任何人都可以简单总结一下我需要学习的关于vim脚本编写的内容.我很难找到的是如何切换缓冲区窗口.

  2. 如果您知道任何介绍我需要的材料的介绍教程,请提供链接.

TX,

aktivb

akt*_*ivb 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                                            
Run Code Online (Sandbox Code Playgroud)