2012-02-08 22 views
5

我試圖讓一個漂亮的代碼打印機過濾器(例如perltidy)接受依賴於vim變量的任意選項。我的目標是將項目特定選項傳遞給在可視模式下用作過濾器(:!)的外部命令。如何在可視化模式下的外部過濾命令中使用vim變量?

下表達我的意圖(最後一行是有問題的):

" set b:perltidy_options based on dirname of the currently edited file 
function! SetProjectVars() 
    if match(expand("%:p:h"), "/project-foo/") >= 0 
    let b:perltidy_options = "--profile=$HOME/.perltidyrc-foo --quiet" 
    elseif match(expand("%:p:h"), "/project-bar/") >= 0 
    let b:perltidy_options = "--profile=$HOME/.perltidyrc-bar --quiet" 
    else 
    let b:perltidy_options = "--quiet" 
    endif 
endfunction 

" first set the project specific stuff 
autocmd BufRead,BufNewFile * call SetProjectVars() 

" then use it 
vnoremap ,t :execute "!perltidy " . b:perltidy_options<Enter> 

然而,在最後一行(vnoremap)是在vim的錯誤,因爲它擴展爲:

:'<,'>execute "!perltidy " . b:perltidy_options 

並且執行命令不能接受範圍。 但我想這樣:

:execute "'<,'>!perltidy " . b:perltidy_options 

我該怎麼做?

p.s.我的perltidy被配置爲像unix過濾器,我使用vim 7.3。

回答

2

您可以使用<C-\>egetcmdline()保存命令行的內容:

vnoremap ,t :<C-\>e'execute '.string(getcmdline()).'."!perltidy " . b:perltidy_options'<CR><CR> 

,但在這種情況下,我會建議簡單<C-r>=其清除出去:execute需要:

vnoremap ,t :!perltidy <C-r>=b:perltidy_options<CR><CR> 
2

如果你想擺脫命令(ex)模式的範圍, CRL-u會做到這一點。

vnoremap ,t :execute "!perltidy " . b:perltidy_options<Enter> 

成爲

vnoremap ,t :<C-u>execute "!perltidy " . b:perltidy_options<CR> 

:H c_CTRL-U

快樂vimming,

路加福音

相關問題