Tuesday, July 8, 2014

Re: redirect output of shell script on to same window

On 2014-07-08, sinbad wrote:
> hi,
>
> i have a vim function which can be used to execute a shell cmd (for example 'ls')
> from within vim, i want to redirect the output to a same window always, for
> example, i will have two windows opened A | B. A will be my script and B will be
> my output window, when i execute A, if B is not opened, open B and display the
> output, if B is already there, then clear the existing contents of B and display
> the new output. I hope i'm clear on the requirement, i know i have to use the
> commands like 'rightbot new', 'vertical new', etc, i tried all combinations but
> nothing seems to be working, your help is much appreciated.

Here are some snippets from a function that runs a program to
perform a static analysis of a set of source files and displays
those results in a named window. They should help you with the
harder parts of what you're trying to do.

The window name is defined outside the function so that it's defined
only once and so that I can use it with multiple functions.

let s:results_name = "[SA Results]"

Within the function, we search for the results buffer by name. If
it already exists, we jump to it and clear it. Otherwise, we create
it.

let l:results_winnr = bufwinnr('^'.fnameescape(s:results_name).'$')
if l:results_winnr != -1
" The results buffer already exists, so jump to it and clear it.

exe l:results_winnr . "wincmd w"
normal! ggdG
else
" The results buffer does not exist, so create a new one.

exe 'silent new' s:results_name
setlocal buftype=nofile
setlocal bufhidden=delete " Either hide it or delete it, but
" don't keep it around because unless
" it's hidden, it will be empty.
setlocal noswapfile
setlocal filetype=saresults
setlocal linebreak " Make detailed error descriptions
" more readable.
setlocal nonumber
endif

The results buffer is given the filetype "saresults" so that syntax
highlighting can be applied to the contents.

I set 'linebreak' and 'nonumber' to make these particular results
look better in my environment. I left those commands in this
example so that you could see where to put such settings. Of
course, I could have put those commands in a file named
ftplugin/saresults.vim instead.

The function then executes the static analysis command and
collects its output to a temporary file whose name is obtained from
tempname().

" Obtain a temporary file for the results so that we can show
" the progress of the command.
"
let l:tmpreportfile = tempname()

"""""
" Use exe or system() to execute the command, sending the output
" to l:tmpreportfile.
"""""

If the command failed, there will be no report file and the buffer
will remain empty. You may wish to change this behavior so that the
buffer is cleared or created only if your command succeeds.

if filereadable(l:tmpreportfile)
" If the command failed, there will be no report file.
silent exe 'r' l:tmpreportfile
0d_
call delete(l:tmpreportfile)
endif

HTH,
Gary

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: