In the normal case I use vim's make utility I will set makeprg to the Makefile of the project I'm currently working for. Since usually the project will last for weeks or even longer, I don't need to change the setting of makeprg very often . But sometimes I need to write some "foobar" code either for practicing my c++ skill or for prototyping some primitive ideas in my mind. So whenever I switch to the "foobar" mode of vim usage, I need to comments the original makeprg setting add the new setting as following :
au FileType c set makeprg=gcc\ %
au FileType cpp set makeprg=g++\ %
which is really very very inconvenient . when I back to the "normal project mode" of vim usage, I need to change back to the original setting . back and forth ....
what I want to know from you guys is that : is it possible to make the setting of makeprg temporarily . for example , define a function in which first set a local value of makeprg and then call make before return form the function call automatically restore makeprg to the value before the function call.
It's not exactly what you asked for, but you can set options local to a buffer only. That way you don't have to bother wrapping your functions; just change makepgrp
locally on the specific files you want.
*:setl* *:setlocal* :setl[ocal] ... Like ":set" but set only the value local to the current buffer or window. Not all options have a local value. If the option does not have a local value the global value is set. With the "all" argument: display all local option's local values. Without argument: Display all local option's local values which are different from the default. When displaying a specific local option, show the local value. For a global/local boolean option, when the global value is being used, "--" is displayed before the option name. For a global option the global value is shown (but that might change in the future). {not in Vi}
au FileType c setl mp=gcc\ %
au FileType cpp setl mp=g++\ %
If you want to save and restore an option before/after a function call in vim, you would do it like this:
let oldmakeprg = &l:makeprg try " set new value of makeprg and call the function set makeprg=new\ value call MyFunction() finally " set makeprg back to old value let &l:makeprg = oldmakeprg endtry
You could also put your 'foobar' code in a special folder and have a separate autocommand to set makeprg separately for that:
" normal settings for makeprg au FileType c set makeprg=gcc\ % au FileType cpp set makeprg=g++\ % " special settings for foobar code au BufRead,BufNewFile **/foobar/**.c set makeprg=gcc\ % au BufRead,BufNewFile **/foobar/**.cpp set makeprg=g++\ %
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With