TIL: Vim - Environment Variable with :make
I currently use the Vim compiler cargo option while
working on projects in the Rust language. Since I
don't use cargo-watch or
bacon to build the project automatically, I
use key mappings:
nmap <F4> :make check<CR>
nmap <F5> :make run<CR>
The problem
When I needed to enable an environment variable to show logs, I tried setting it directly in the mapping and of course it didn't work:
nmap <F5> :MYAPP_LOG=debug make run<CR>
The solution
Use let $VAR to set the variable in the Vim process environment:
:let $MYAPP_LOG="debug"
:nmap <F5> :make run<cr>
The :let $VAR="value" command sets the environment variable inside the Vim
process, and since :make runs as a subprocess it inherits these variables
automatically.