开发者

Reading the result of an exe file in :vs in vim

Many of my programs are console like applications, which take a data file, and print out the results, either on screen (more often) or 开发者_开发问答in another file.

Since these days I'm doing some analysis which requires a lot of little tempting with one input data file, just to get a few numbers, I usually go: edit data file, start program, get result, edit data file, start program, get result ...

So I was wondering, is there a way in vim, while having open the input file, to define a function which would open a vertical split and load the result of program (pro12.exe) in it ?

How would one go about that ?


The easiest thing to do is probably write a Makefile to run the program (and redirect output to a file), and :set the autoread option on in Vim. Then, you can create a macro r by typing qr:make<Enter>q and run the macro with @r. The macro will cause Vim to invoke make, which will run the program to update the data file. The autoread option will make sure that vim refreshes the updated file without prompting you first.


Assuming pro12.exe is in your %PATH%, this will invoke your helper app and vert-split-open a static output file name. Map to whatever key you like. QND: won't work when relative paths (bufnames) change via cd. YMMV

fun! RunPro12()
    bufdo if bufname(bufnr($)) == '/path/to/pro12.exe.output' | close | endif
    silent exe '!pro12.exe'
    vs /path/to/pro12.exe.output
endfun
map <f3> :call RunPro12()<cr>


I don't like :bufdo solutions as this command always messes up the current windows organisation.

In normal time, I'd use lh#buffer#jump() that jumps to the window where the named buffer is opened if it is already opened, or opens the window otherwise.

function! s:Run0()
  call lh#buffer#jump('/path/to/pro12.exe.output', 'vsp')
  silent exe '!ls'
endfunction

Given the requested "to define a function which would open a vertical split", we could simply use:

function! s:Run1()
  let bn = bufnr('/path/to/pro12.exe.output')
  if bn >= 0
    exe 'bw '.bn
  endif
  silent exe '!pro12'
  vsp /path/to/pro12.exe.output
endfunction
nnoremap µ :call <sid>Run1()<cr>

Last thing. If as I suspect pro12 writes to stdout (the standard output, i.e. ~ the console), the last two lines of the function shall become:

vsp /path/to/pro12.exe.output
r!pro12

The "/path/to/pro12.exe.output" may be replaced with anything unique. Typically, I'd use a scratch buffer with a unique name having "pro12 result" in it.

PS: if in the function (this is important) you add a

nnoremap <buffer> q :bw<cr>

you'll be able to simply quit the output window simply by hitting q, from within the output window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜