开发者

Vim new window for program execution, similar to cwindow

I am using Vim to edit/compile individual cpp files (makepgr=g++\ -g\ %) and want to execute the resulting binaries ideally using a shortkey like F6. The desired behavior is then, to open a new window similar to :cwindow with a fixed maximal height (say 20 lines), which contains the program output. I also want to be able to close this window again with another shortkey like F7.

As a bonus, it would be great to have the execution time of the program shown in the windows title/status.

EDIT: the binaries I am talking about are non-interactive! They mostly read some input files, do data manipulation, write their results to 开发者_如何学JAVAother files and have some output while doing so.


This little script should do the trick:

let s:title="ProgramOutput"

function! Open_output_window()
    let winnr = bufwinnr(s:title)
    if winnr != -1
        exe winnr . 'wincmd w'
        return
    endif

    let bufnr = bufnr(s:title)
    if(bufnr == -1)
        let wcmd = s:title
    else
        let wcmd = '+buffer' . bufnr
    endif

    exe 'silent! botright vertical 80 split ' .wcmd

    silent! setlocal buftype=nofile
    silent! setlocal noswapfile
    silent! setlocal nobuflisted
endfunction

function! Close_output_window()
    let winnr = bufwinnr(s:title)
    if winnr != -1
        exe winnr . 'wincmd w'
        close
    endif
endfunction

nmap <F6> <ESC>:call Open_output_window()<CR>:r ! %:r.exe<CR>
nmap <F5> <ESC>:call Close_output_window()<CR>

Most of this code came from the taglist.vim plugin - it's reasonably simple to understand. You can modify the split command for your window size preferences.


I'm not sure if this is helpful in any way, since you're asking about vim:

My first thought about how to solve this problem would be usage of GNU screen. Reason is not, that vim isn't capable of doing this, but that vim isn't exactly usable as terminal emulator, so running some interactive programs doesn't work. Plus screen is relatively easy to configure.

I would of course write a specialized screen config for that task.


Add to the vimrc:

 map <F6> :tabnew<CR>:.!ls -la<CR>

Open vim and press F6. You should see in the new tab with the output of the command ls -la. Now, if this works for you then you can change it (replace myprog with the name of your executable):

 map <F6> :tabnew<CR>:.!./myprog<CR>

You can even give name to this tab:

 map <F6> :tabnew myprog<CR>:.!./myprog<CR>

Do not like tabs? Replace tabnew with split or vsplit wich allows to split window horizontally or vertically. Want splitted window to be small? Use

 map <F6> :5split myprog<CR>:.!./myprog<CR>

Replace 5 with any other number you likes. Also add

 map <F7> :tabclose<CR>

to get ability to close the tab by pressing F7.

P.S. You can replace hard-coded myprog name with expand("%:r").".exe" (for Windows) or just expand("%:r") (for *nix) as already been said.


You could redirect the input into the quickfix window (similar to how :make works):
:cex system(expand("%:r").".exe")

You can navigate through quickfix history using :colder and :cnewer

I realise this isn't exactly what you were looking for, but hopefully it's near enough to be useful.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜