开发者

VIM: Automatically adding menu-items for scripts from a particular directory

开发者 https://www.devze.com 2022-12-12 14:47 出处:网络
I have a number of scripts (Ruby as it happens) I run from VIM, by setting up the startup file to contain (for instance):

I have a number of scripts (Ruby as it happens) I run from VIM, by setting up the startup file to contain (for instance):

amenu Ruby.script1 :%!ruby C:\ruby_scripts\script1.rb<cr><cr>
amenu Ruby.script2 :%!ruby C:\ruby_scripts\script2.rb<cr><cr>
...

What I would like to do, is to have VIM automatically check the C:\ruby_scripts directory and assign menu items automatically - can t开发者_如何学运维his be done?


You can combine glob() and exe with something like this:

let dirname = 'c:/ruby_scripts'
for script in split(glob(dirname . '/*.rb'))
    " Get the script name
    let scriptname = fnamemodify(script, ':t:r')
    let scriptfile = fnamemodify(script, ':p')
    " Add the item to the menu
    exe 'amenu Ruby.' . scriptname . ' :%!ruby ' . scriptfile . '<cr><cr>'
endfor

For more information, see:

:help glob()
:help split()
:help fnamemodify()
:help expand()
:help :exe


Try:

function! s:AddScript(dir, menuname)
  let files = lh#path#GlobAsList(a:dir, "*.rb")
  for f in files
    let n = fnamemodify(f, ":t:r")
    exe "anoremenu ".a:menuname.".".n." :%!ruby ".f."<cr><cr>"
  endfor
endfunction

call s:AddScript("c:/ruby_scripts", "Ruby")

NB: lh#path#GlobAsList comes from lh-vim-lib. With a substitute(), you shall be able to transform globpath() result to the string you'll then need to :execute.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号