What I am trying to do seems a very basic stuff, but I can't find anything about it. I am working on a project built as usual:
project
|-- bin
|-- inc
`-- src
I would like to make my project using the make command included in Vim. But each time I have to specify :make -C ../
. I would prefer, if there is not Makefile file in the current directory, go in the parent directory. I already do that with
set tags+=./tags;/
in my .vimrc
.
Furthermore, the make is by default ugly. Are there options to add color to make, and allow a direct access to the errors (as in Emacs).
Thanks
The best way to learn is practice. Take a few minutes to try Vim out. If you're on a Linux system right now, open up a terminal and type vim filename. Enter insert mode and type a bit (or copy some of the text from this article into Vim) and then hit Escape to start practicing movement around the file.
For the Vim curious Vim is a text editor that can greatly increase your productivity when you are coding. Typing speed is irrelevant up to a certain level. Your ability to navigate through code is much more important. This is where Vim, along with its keybindings, layout, and setup, can help you speed up the process.
What distinguishes vim from other editors? It's a modal editor that makes it so efficient. Unlike any other editors, that only have one mode which is insertion mode, you are constantly typing and streaming text into the buffer.
Slight modification of what Adam said:
:set makeprg=[[\ -f\ Makefile\ ]]\ &&\ make\ \\\|\\\|\ make\ -C\ ..
Unescaped, this is
[[ -f Makefile ]] && make || make -C ..
which means, pseudo code style
if file-exists(Makefile)
then make
else make -C ..
This only goes one directory up. If you'd like a more general solution that will go as many directories up as necessary, you'll need to be able to search ancestor directories until a Makefile is found, and I'm not sure how to do that simply from the command line. But writing a script (in whatever language you prefer) and then calling it from your makeprg shouldn't be hard.
The solution of rampion is a first step, but computed on vim load. When I load a multi tab session, the path can be inconsistent from one tab to another.
Here my solution (+ extra with tabnew).
fun! SetMkfile()
let filemk = "Makefile"
let pathmk = "./"
let depth = 1
while depth < 4
if filereadable(pathmk . filemk)
return pathmk
endif
let depth += 1
let pathmk = "../" . pathmk
endwhile
return "."
endf
command! -nargs=* Make tabnew | let $mkpath = SetMkfile() | make <args> -C $mkpath | cwindow 10
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