How to install makeprg in vim temporarily

In the normal case, I use the vim make utility. I will install makeprg in the Makefile of the project I am currently working on. Since the project will usually last for weeks or even longer, I don’t need to change the makeprg settings very often. But sometimes I need to write some β€œfoobar” code either to practice my C ++ skill, or to prototype some primitive ideas in my mind. Therefore, whenever I switch to the "foobar" mode of using vim, I need to comment on the original makeprg setting, adding a new setting:

au FileType c set makeprg=gcc\ % au FileType cpp set makeprg=g++\ % 

which is really very inconvenient. when I get back to the "normal project mode" of using vim, I need to go back to the initial setup. back and forth ....

what i want to know from you guys: is it possible to temporarily set the makeprg parameter. for example, define a function in which the local makeprg value is set first, and then make is called before returning the form, the function call automatically restores makeprg to the value before the function call.

+4
source share
2 answers

If you want to save and restore the parameter before / after calling the function in vim, you will do it as follows:

  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 can also put your foobar code in a special folder and have a separate auto-command for installing makeprg separately for this:

  "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 ++ \%
+5
source

This is not exactly what you requested, but you can configure the parameters locally only on the buffer. Thus, you do not have to worry about packaging your functions; just change makepgrp locally to the files you need.

<Preview> *: setl * *: setlocal * : setl [ocal] ... Like " : set ", but set only a local value for 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 argument "all": display all local options local values. Without argument: display all local options local values ​​other than the default value. When a specific local parameter is displayed, the local value. For the global / local boolean parameter, when the global value is used, β€œ - ” is displayed before the option name. For the global option, the global value (but this may change in the future). {Not in Vi}
 au FileType c setl mp=gcc\ % au FileType cpp setl mp=g++\ % 
+7
source

Source: https://habr.com/ru/post/1303504/


All Articles