Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to temporarily set makeprg in vim

Tags:

vim

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.

like image 439
Haiyuan Zhang Avatar asked Dec 12 '22 23:12

Haiyuan Zhang


2 Answers

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++\ %
like image 105
ephemient Avatar answered Jan 16 '23 17:01

ephemient


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++\ %
like image 26
too much php Avatar answered Jan 16 '23 16:01

too much php