You are not logged in.

#1 2010-07-09 02:14:20

kismet010
Member
Registered: 2009-09-02
Posts: 137

vim c/c++/java [solved]

hi, i'm trying to use vim as my only full ide, for c/c++ and java

how can i bind the f5 key for a "compile, correct and run" process inside vim, like bigs ide? (and for differents languages)
for now i have this, but doesn't work:

au FileType c setl makeprg=gcc\ %
au FileType cpp setl makeprg=g++\ %
au FileType java setl makeprg=javac\ %

map <F5> : call Compila()<CR>
func! Compila()
  exec "w"
  exec "make"
  exec "cw"
endfunc
map <F6> : call CompilaEjecuta()<CR>
func! CompilaEjecuta()
  exec "w"
  exec "make"
  exec "cw"
  exec "!./%"
endfunc
map <F7> :cp<CR>
map <F8> :cn<CR>

i try with the cvim plugin.. but most commands are in that way "\xx", for example, to compile and run the command is \rc
it's looks like \ are not recognized, cos \rc only replace actual character with "c"
i'm using a spanish keyboard, i don't know if that's the problem

thanks

Last edited by kismet010 (2010-07-10 11:36:06)

Offline

#2 2010-07-09 03:44:37

a_priori_mouse
Member
Registered: 2010-05-16
Posts: 38

Re: vim c/c++/java [solved]

*A inattentive user's stupid comment was here. Sorry.

Last edited by a_priori_mouse (2010-07-09 04:08:18)

Offline

#3 2010-07-09 07:31:49

kismet010
Member
Registered: 2009-09-02
Posts: 137

Re: vim c/c++/java [solved]

xD

Offline

#4 2010-07-09 10:25:52

a_priori_mouse
Member
Registered: 2010-05-16
Posts: 38

Re: vim c/c++/java [solved]

kismet010 wrote:

for now i have this, but doesn't work:

What exactly does not work, what is error message? I tried these settings in my vimrc - seems it is working correctly.

Offline

#5 2010-07-09 10:47:51

kismet010
Member
Registered: 2009-09-02
Posts: 137

Re: vim c/c++/java [solved]

first, i don't want that when i press F5, command make exits vim, and i have to press a key (i only wanna see the quickfix window)
last, exec "!./%" doesn't run the file

and my purpose is to get better ideas from others users to do this...

Offline

#6 2010-07-10 08:38:28

essence-of-foo
Member
Registered: 2008-07-12
Posts: 84

Re: vim c/c++/java [solved]

Have a look at :make

"from my vimrc
map <F12> :make<enter>

Then you can put a Makefile in your project's directory and vim will execute it, when you press F12 (can be changed to F5 of course).

If you don't want to use Makefiles, you can set the makeprg varible to something different than "make"

With this method vim will parse the output from the makefile invokation and jump into the lines, where errors were found. To step from error to error, use the :cnext and :cprev commands (I have bound :cprev to F3).

Offline

#7 2010-07-10 09:29:17

a_priori_mouse
Member
Registered: 2010-05-16
Posts: 38

Re: vim c/c++/java [solved]

kismet010 wrote:

last, exec "!./%" doesn't run the file

First, man gcc:

-o file
           Place output in file file... 
           If -o is not specified, the default is to put an executable file in
           a.out, the object file for source.suffix in source.o, its assembler
           file in source.s, a precompiled header file in source.suffix.gch,
           and all preprocessed C source on standard output.

So, in your case make output file is ./a.out You shoud run ./a.out and not ./%.
Or, better:

au FileType c setl makeprg=gcc\ %\ -o\ %<
exec "!./%<"

or

exec "!./%:r

I do not remember the difference, but the this option is more popular

Last edited by a_priori_mouse (2010-07-10 09:59:50)

Offline

#8 2010-07-10 09:55:22

kismet010
Member
Registered: 2009-09-02
Posts: 137

Re: vim c/c++/java [solved]

yep, i'm making some progress since i wrote this post, one of them that one, specially from stackoverflow. now i'm trying to work with an errorfile

i read 2 books about vim, but i'd never imagine things like that gonna be so difficult xD i have to succumb to the power of eclipse sad

lot of thanks for your interest a_priori_mouse

Offline

#9 2010-07-10 10:12:39

a_priori_mouse
Member
Registered: 2010-05-16
Posts: 38

Re: vim c/c++/java [solved]

it`s nothing

Offline

#10 2010-07-10 11:17:22

Vain
Member
Registered: 2008-10-19
Posts: 179
Website

Re: vim c/c++/java [solved]

kismet010 wrote:

i have to succumb to the power of eclipse sad

No! Don't! smile

Here's what I came up with. It's not exactly what you're after (see below), but maybe you can borrow some of its ideas.

map <F5> :call CompileAndRun(1)<CR>
map <S-F5> :call CompileAndRun(0)<CR>

fun! CompileAndRun(runProgram)
    let l:progname = './' . expand('%:t:r')
    let l:interpreter = 0
    let l:domake = 1
    let l:runner = './run'

    " Otherwise :make won't return a proper exit status.
    setl shellpipe=2>&1\ \|\ tee\ %s;exit\ \${PIPESTATUS[0]}

    " Find out how to build the program.
    if filereadable("SConstruct")
        setl makeprg=scons
    elseif filereadable("Makefile") || filereadable("makefile")
        setl makeprg=make
    elseif filereadable("build.xml")
        setl makeprg=ant
    elseif &filetype == 'c'
        setl makeprg=gcc\ -Wall\ -o\ %<\ %
    elseif &filetype == 'cpp'
        setl makeprg=g++\ -Wall\ -o\ %<\ %
    elseif &filetype == 'java'
        setl makeprg=javac\ %
        let l:progname = 'java ' . expand('%:t:r')
        let l:interpreter = 1
    elseif &filetype == 'matlab'
        let l:progname = 'octave ' . expand('%') . ' | less'
        let l:interpreter = 1
        let l:domake = 0
    else
        " Assume it's a simple script.
        let l:progname = './' . expand('%')
        let l:domake = 0
    endif

    write
    if l:domake == 1
        silent !echo -e "\n\nBuilding..."
        make
        cw
    else
        silent !echo -e "\n\nNot running build tool."
    endif

    if v:shell_error == 0 && a:runProgram == 1
        if executable(l:runner)
            silent !echo -e "\n\nExecuting run script..."
            exec '!' . l:runner
        elseif executable(l:progname) || l:interpreter == 1
            silent exec '!echo -e "\n\nExecuting \"' . l:progname . '\""...'
            exec '!' . l:progname
        endif
    endif
endfun

It first checks if there are any build instructions (scons, make or ant). If so, we'll use them. If not, it checks if it knows the file type in order to build/compile a single file. If all that fails, we treat the file as a simple script.

For complex programs, I usually have a "run" script (containing command line arguments or whatever). If there's no such script, then try to execute the program/script directly.

Now, it still requires you to press "Return" a few times. That's because I like it that way. smile You could avoid that by prefixing "make" with "silent". If this results in a cluttered screen, use "redraw" or "redrawstatus" at the end of the function.

Offline

#11 2010-07-10 11:34:57

kismet010
Member
Registered: 2009-09-02
Posts: 137

Re: vim c/c++/java [solved]

wow!! that's exactly what i wanted big_smile back to the drawer eclipse...

thanks u guys

Offline

Board footer

Powered by FluxBB