Suppose I launch vim from a large project root folder and want to compile a specific example (I ll use zephyr RTOS for example). This root folder is located under /home/<user>/zephyr/
Let's say I run vim samples/basic/blinky/src/main.c
.
Now if I want to compile it, I would go, from another terminal to samples/basic/blinky/build/
and run make
If I want to build it without leaving vim, I could run :make -C samples/basic/blinky/build/
I would like to automate this process, pressing any key, let's say f5.
So if I have, for example, two vertical splits, v1 and v2.
In v1 I have samples/basic/blinky/src/main.c
and in v2 I have samples/drivers/rtc/src/main.c
.
Pressing f5 from v1 would lead to run the equivalent of :make -C samples/basic/blinky/build/
, and from v2 would lead to run the equivalent of :make -C samples/drivers/rtc/build/
The common pattern is that the build folder is located in ../build/
from the current c file directory.
I don't want to "permanently" use :cd
or :lcd
to change working directory, even for the current split/window because:
- My ctags tags file is located in the root folder, so I want to be able to jump to any function that
samples/basic/blinky/src/main.c
uses but are not necessarily defined in the same file.- If I want to open a new file, I want to access it using its path from the root folder and not the current file path
My current solution is to have a function in my ~/.vimrc
which temporally changes the working directory to the current file equivalent build folder, so that I can run :make
and then changes back the working directory to the root folder.
It looks like this:
nnoremap <F5> :call MakeTst()<CR>
function! MakeTst(...)
:cd %:p:h
:cd ../build/
:make
:cd /home/<user>/zephyr/
endfunction
While this works, the downside is that the root folder is hardcoded inside the ~/.vimrc
.
How can I achieve the same result without hardcoding the root folder path?