Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Specifically, I'd like to set the makeprg option only if it has not been changed yet. Any way to do this?

like image 903
static_rtti Avatar asked Oct 11 '22 02:10

static_rtti


1 Answers

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.

like image 136
Prince Goulash Avatar answered Oct 19 '22 13:10

Prince Goulash