开发者

In vim, is there a way to :set an option only if it is not set yet?

开发者 https://www.devze.com 2023-04-02 19:13 出处:网络
Specifically, I\'d like to set the makeprg option only if it has not been changed yet. An开发者_开发知识库y way to do this?You can access the contents of an option using &. So in this case you cou

Specifically, I'd like to set the makeprg option only if it has not been changed yet. An开发者_开发知识库y way to do this?


You can access the contents of an option using &. So in this case you could check if the option is "empty" like this:

if &makeprg == ""
    set makeprg=new_value
endif

--- EDIT ---

Xavier makes the good point that the default value of makeprg is not an empty string. You can use set {option}& to set an option to its default, so the following can be used to change the value only if the current value is the default:

function! SetMakePrg( new_value )
    let cur_value=&makeprg
    " let new_val=a:new_value
    set makeprg& 
    if &makeprg == cur_value
        " cur_value is default: set to new value
        exe "set makeprg=" . a:new_value
    else
        " cur_value is not default: change it back
        exe "set makeprg=" . cur_value
    endif
endfunction

So calling call SetMakePrg("my_make") will modify the option only if it is not currently the default.

0

精彩评论

暂无评论...
验证码 换一张
取 消