2012-12-21 31 views
3

我使用AppendModeline功能模式行添加到我的VIM文件:有沒有辦法在vim中獲取布爾選項的字符串表示?

 
" Append modeline after last line in buffer. 
" Use substitute() instead of printf() to handle '%%s' modeline in LaTeX 
" files. 
function! AppendModeline() 
    let l:modeline = printf(" vim: set ts=%d sw=%d tw=%d :", 
     \ &tabstop, &shiftwidth, &textwidth) 
    let l:modeline = substitute(&commentstring, "%s", l:modeline, "") 
    call append(line("$"), l:modeline) 
endfunction 

但我想擴展它。它應支持添加當前值expandtab
使用& expandtab,我可以得到當前值的數字表示。但是像set et = 0不支持vim。它必須是設置[no] expandtab
難道我真的要測試& expandtab和追加expandtabnoexpandtab升:模式行還是有一種方式來獲得當前值的字符串表示?
set expandtab?顯示[no] expandtab,但我不知道如何在腳本中使用它(或者甚至可能)。

回答

4

是的,你必須這樣做。使用:redir可以捕獲輸出,但基於:redir的解決方案至少需要四行,並使用正則表達式來獲取該值。使用&et乾淨多了:

… printf("… %set …", …, &expandtab ? '' : 'no', …) 

注:%set%s其次et(簡稱expandtab)。字set這裏只是一個意外。

相關問題