2010-09-21 100 views
2

我有一個vim文件中的以下代碼,它在編輯php文件時自動獲取。但我無法讓它工作。VIM:使用自定義函數作爲shell的參數

"PHP config 
if !exists("g:addPath") 
    let g:addPath = 1 
    let $PATH=$PATH.';C:\Program Files\Mozilla Firefox' 
endif 

function! MakeThisUrl() 
    let s:url='http://localhost/' 
    let s:url=s:url. expand('%') 
    return s:url 
endfunction 

function! MakeCustomUrl() 
    let s:url='http://localhost/' 
    let s:url=s:url. expand('%:p') 
    return s:url 
endfunction 


map <F9> :w<CR>:!firefox -new-tab MakeThisUrl()<CR> 
map <F10> :!firefox -new-tab call MakeCustomUrl() 
imap <F9> <Esc>:w<CR>:!firefox -new-tab MakeThisUrl()<CR><CR> 
imap <F10> <Esc>:!firefox -new-tab call MakeCustomUrl() 

這個想法是讓vim自動生成正確的URL,所以我可以通過設置F9來測試代碼。但是,我不能讓它執行MakeThisUrl()和我得到它的

:!firefox -new-tab MakeThisUrl() <CR><CR> 

代替

:!firefox -new-tab http://localhost/filename.php <CR><CR> 

如何使它工作的任何想法? 在此先感謝

回答

2

但是,我不能讓它執行MakeThisUrl()和我得到了
:火狐 - 新標籤MakeThisUrl()

這不起作用,因爲該命令就像鍵入一樣執行。試試這個:

map <F9> :up<CR>:execute ":!firefox -new-tab ".MakeThisUrl()<CR> 

的重大變化:利用執行從表達的評價結果​​的命令的:execute的。表達式評估是在函數被調用時。這裏它的結果與":!firefox -new-tab "連接在一起,它被作爲Ex命令執行(命令以:開頭;對於:execute,領導:是可選的)。

P.S.小的變化/挑選:而不是:w使用:up(或:update)只寫入緩衝區,如果它被修改。

+0

非常感謝,它工作得很好。沒有使用execute來生成命令。感謝'更新'提示,我將它應用於其他'au'上的文件 – masterLoki 2010-09-21 23:12:41

相關問題