Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reuse the same Vim window/buffer for command output, like the :help window?

Tags:

vim

Though I'm no Vim expert, I've been scratching an itch by working on a rough Vim equivalent of TextMate's ⌘R functionality to run Ruby code from a buffer and display the output.

The script currently just opens a new window (split) with :new and puts the output there. If you run it multiple times, it opens multiple windows. Ideally, I'd like it to reuse the same window within each tab page, much like :help does.

I've looked but haven't found a way to achieve this. Any pointers?

like image 941
Henrik N Avatar asked Mar 14 '11 19:03

Henrik N


People also ask

How to see buffers in Vim?

Use the ":sbuffer" command passing the name of the buffer or the buffer number. Vim will split open a new window and open the specified buffer in that window. You can enter the buffer number you want to jump/edit and press the Ctrl-W ^ or Ctrl-W Ctrl-^ keys. This will open the specified buffer in a new window.

How do I close vim buffers?

Close buffer named Name (as shown by :ls ). Assuming the default backslash leader key, you can also press \bd to close (delete) the buffer in the current window (same as :Bclose ).


1 Answers

You can create a scratch buffer with a name, so that on subsequent calls you can check to see if that buffer is already open (and if so reuse it) or you need a new one. Something like this:

function! Output()
    let winnr = bufwinnr('^_output$')
    if ( winnr >= 0 )
        execute winnr . 'wincmd w'
        execute 'normal ggdG'
    else
        new _output
        setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
    endif
    silent! r! ls
endfunction
like image 149
Matteo Riva Avatar answered Sep 20 '22 20:09

Matteo Riva