我試圖讓一個漂亮的代碼打印機過濾器(例如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。