Question

Here's what the lines in my .vimrc look like:

autocmd FileType python nnoremap <buffer> <F9> :exec '!python' shellescape(%)<cr>
autocmd FileType java nnoremap <buffer> <F9> :exec '!javac' shellescape(%) && '!java' shellescape(%:r)<cr>
autocmd FileType c,cpp nnoremap <buffer> <F9> :exec '!gcc' shellescape(%) && './a.out'<cr>

The Python map executes fine. The Java map will compile alright, but won't run. I'm not entirely sure if my syntax is correct. I don't know what's going on with the C version of the map...it keeps displayig the message:

"error: Class names, 'test.C,test', are only accepted if annotation processing is explicitly requested"

Anyone know what's going on?

Était-ce utile?

La solution

You're right, the syntax is slightly wrong (for all of them!). If you intend to issue two :! calls, these must be separated by | (escaped <Bar> in a mapping), not &&:

autocmd FileType java nnoremap <buffer> <F9> :exec '!javac' shellescape(%) <Bar> exec '!java' shellescape(%:r)<cr>

But better do this with a single call: The && must then be quoted, so that it is evaluated by the shell launched by :!, not Vim:

autocmd FileType java nnoremap <buffer> <F9> :exec '!javac' shellescape(%) '&& java' shellescape(%:r)<cr>

Furthermore, the special % keyword is only recognized directly in the command-line, not when using it in an :execute expression. You need to wrap this with expand(). Oh, and shellescape() should receive an additional flag for proper :! escaping.

autocmd FileType java nnoremap <buffer> <F9> :exec '!javac' shellescape(expand('%'), 1) '&& java' shellescape(expand('%:r'), 1)<cr>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top