José Ricardo
José Ricardo

Reputation: 1489

Define compiler for current file in vim

Is there any way to get the current compiler in vim?

For example, I want to map <Leader>r to compile the current file and map <Leader>i to load the REPL.

What I've been doing so far is defining one mapping for each file extension, like:

au BufNewFile,BufRead *.m map <Leader>r :!octave %<cr>
au BufNewFile,BufRead *.m map <Leader>i :!octave<cr>

After doing it for three file types (python and ruby work the same way) I'm wondering if there's a way to define one mapping that will work for all kinds of files (that have a compiler defined). Is it possible?

Update: sorry, my initial question made it look like I just wanted to compile the current file.

Upvotes: 1

Views: 547

Answers (2)

linguanerd
linguanerd

Reputation: 731

Update:

Thanks for clarifying your question. I will try to offer a better answer, including an extra feature for invoking the interactive debugger (in addition to RUN & REPL you requested).

I must issue a disclaimer that this is not necessarily idiomatic approach for using vim compiler plugins.

Hopefully this implements the alternative approach you are attempting to achieve.

Add this to your .vimrc file:

" Per-file-type definitions for           RUN        REPL      DEBUG
au BufNewFile,BufRead *.m  let b:comp = ["octave", "octave", "octave"]
au BufNewFile,BufRead *.py let b:comp = ["python", "python", "python -m pdb"]
au BufNewFile,BufRead *.pl let b:comp = ["perl",   "perl",   "perl -d"]
au BufNewFile,BufRead *.rb let b:comp = ["ruby",   "irb",    "ruby -rdebug"]

nmap <Leader>r :exec "!" b:comp[0] " %"<cr>
nmap <Leader>i :exec "!" b:comp[1]<cr>
nmap <Leader>d :exec "!" b:comp[2] " %"<cr>

The au commands set buffer-local b:comp array of run/repl/debug commands based on file extension. The nmap commands map the keystrokes you suggested.

And then you can use:

<Leader>r - to run the current buffer
<Leader>i - to start REPL in language corresponding to current buffer
<Leader>d - to start interactive debugger for the current buffer

Of course I do realize that there is no such thing as a true REPL for perl (at least not out-of-the-box), and in order to invoke the interactive debugger after launching octave it appears you must call a function such as dbstop("func") to set a breakpoint, and then invoke the function you want to debug, and so forth.

Bottom line: the "end user" of this approach would need to keep in mind that run/repl/debug support would necessarily differ depending on the capabilities of the language you are working with.

Upvotes: 2

perreal
perreal

Reputation: 97948

Use a makefile:

au BufNewFile,BufRead *.m map <Leader>r :make -f ~/.vim/global.make %<cr>

I think you can write a makefile that will do different stuff based on the type of its argument.

Upvotes: 1

Related Questions