2009-11-24 94 views
0

我有很多的腳本(紅寶石,因爲它發生),我從VIM運行,通過設置啓動文件包含(例如):VIM:自動添加菜單項爲腳本的特定目錄

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

我想要做的是讓VIM自動檢查C:\ruby_scripts目錄並自動分配菜單項 - 可以這樣做嗎?

回答

1

你可以像這樣結合​​3210和exe

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 

欲瞭解更多信息,請參見:

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

工作很好 - 謝謝! – monojohnny 2009-11-24 14:15:05

1

嘗試:

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來自lh-vim-lib 。使用substitute(),您應該能夠將globpath()結果轉換爲您需要的字符串:execute。

+0

無法得到這個工作在我的Vim安裝 - 我想由於lh#路徑#GlobAsList你提到的東西:但仍然,非常感謝! – monojohnny 2009-11-24 14:17:14

+0

是的,這個功能需要在腳本的正確目錄下安裝。由於它提供了一些您似乎不需要的服務,Al解決方案已經足夠。 – 2009-11-24 14:36:44

相關問題