Vim 脚本:缓冲区/CheatSheet 切换

发布于 2024-10-20 20:14:12 字数 481 浏览 2 评论 0原文

我想做一个 vim 备忘单插件。这真的很简单:

  • 我想切换我的备忘单。 vertsplit 切换,如 Taglist 或 NERDTree。
  • 我希望备忘单是特定于文件类型的。因此,当我打开 .cpp 文件时,我会切换我的 C++ 备忘单。
  • 我希望备忘单水平分割。所以它显示了两个文件,我的语法备忘单和我的片段触发器备忘单。

我已经收集了这些备忘单,采用 vimhelp 格式,但现在我必须手动打开它们。

我还没有真正编写过任何 vim 脚本,但我想这会很容易组合在一起。我有点厌倦了在谷歌上搜索不相关的代码片段,所以我在这里问的是:

  1. 有人能给我一个关于 vim 脚本我需要学习的简短总结吗?我很难找到如何切换缓冲区窗口。

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

TX,

活动b

I want to make a vim cheat sheet plugin. It's real simple:

  • I want to toggle my cheatsheets. A vertsplit toggle, like Taglist or NERDTree.
  • I want the cheatsheet to be filetype specific. So I toggle my c++ cheatsheet when I have opened a .cpp file.
  • I want the cheatsheet to be horizontally split. So it shows two files, my syntax cheat sheet and my snippet trigger cheat sheet.

I already have a collection of these cheatsheets, in vimhelp format, but now I have to manually open them.

I haven't really done any vim scripting, but I imagine this would be really simple to put together. I'm sorta sick of googling unrelated codesnippets, so what I'm asking here is:

  1. Could anyone give me a short sum-up of what I need to learn in regards to vim scripting to piece this together. What I have a hard time finding is how to toggle the buffer window.

  2. If you know any intro tutorials that covers the material I need to get this up and running, please provide a link.

tx,

aktivb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千秋岁 2024-10-27 20:14:12

下面的函数可能无法完全满足您的要求,而且我还没有测试过它,但它应该可以给您一些想法。

主要思想是该函数读取当前缓冲区的文件类型(您可以通过键入 :echo &ft 进行测试),然后设置适当的备忘的路径。如果存在,则在拆分窗口中打开该路径(只读且不可修改)。然后,您可以按照您希望的方式调用此函数,例如将其映射到 {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>

希望这有帮助。如果有任何不清楚的地方,或者您想了解更多,请大声喊出来。

The function below may not do exactly what you want, and I haven't tested it, but it should give you some ideas.

The main idea is that the function reads the filetype of the current buffer (you can test this by typing :echo &ft) and then sets the path of the appropriate cheat sheat. If it exists, this path is then opened (read-only and non-modifiable) in a split window. You can then call this function any way you wish, for example by mapping it to the {F5} key as shown.

I'm not sure about the toggling possibilities (is this really easier than just closing the split window?) but you could look at the bufloaded() function, which returns whether or not a given file is currently being accessed.

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>

Hope this helps. Just shout if anything is unclear, or you want to know more.

白昼 2024-10-27 20:14:12

我已经忘记了这一点,直到我收到 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                                            

I had forgotten about this until I got a notice about Eduan's answer. Since I posted this question I've done quite a bit of vim scripting, including getting this to work:

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                                            
无声无音无过去 2024-10-27 20:14:12

我想我应该补充一下炖牛肉的答案。

我认为为了实现切换,您只需使用一些 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

希望这能弄清楚这个逻辑。 :)

Thought I would add to Goulash's answer.

I think in order to implement the toggle you would simply use some if statements and a global variable.

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

Hope this figures out that logic. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文