cacophonix's blog

By cacophonix, 12 years ago, In English

for running testcases in vim if first copy the test cases in a text file called 'in' and press F6

which runs the code by taking input from the 'in' text file.

i have

map <F6> :w<CR>:!g++ % -g && (ulimit -c unlimited; ./a.out < in) <CR>

in my .vimrc file.

IS IT POSSIBLE TO RUN THE CODE BY TAKING INPUT FROM THE SYSTEM CLIPBOARD ?

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
12 years ago, # |
  Vote: I like it +2 Vote: I do not like it

In a platform-dependent way, yes. xclip (with some parameter I don't remember) in Linux and pbpaste in Mac OS can output the contents of clipboard. So for example in Mac OS adding 'pbpaste > in;' before ./a.out should do the trick.

»
12 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

You can use this way:

map <F6> :call TestIt() <CR>
function TestIt()
  let l:test = substitute(@+, "'", "", "g")
  let l:file = @%
  write
  let l:output = system("g++ '".file."' -g && (ulimit -c unlimited; echo '".test."' | ./a.out)")
  echo l:output
endfunction

I tried it on Linux and Windows, so it should work.

UPD: local variables and sanitize string

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I used this two in vimrc:

autocmd filetype cpp nnoremap <F6> :w <bar> exec '!g++ -g -Wall -std=c++0x '.shellescape('%').' && ./a.out' <CR>
autocmd filetype cpp nnoremap <F7> :w <bar> exec '!g++ -g -Wall -std=c++0x '.shellescape('%').' && ./a.out < d.in' <CR>

Is good to activate warnings with -Wall because several warnings are truly errors and may become difficult to find it.