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. 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.
精彩评论