简体   繁体   中英

VIM Buffer/Window Hiding

I'm writing a vim plugin which needs to do some special window management. What I want is as follows:

On startup, the current window will be split in two and the newly created window will be used for user input and plugin output. Let's call this the I/O window.

Occasionally the I/O window will need to be "replaced". That is, a new set of contents will replace the portion of the screen containing the I/O window. Call this the temporary window. However, the I/O window will remain saved somewhere. When the user leaves this temporary window (with :q or :q!) the contents of the I/O window will reappear in the original spot.

How would I go about doing such a thing? I've been trying things and reading about buffers and windows for a some time now in the help pages but I still can't seem to figure it out.

What I think should work is:

  1. :new to split the screen on startup.
  2. :set bufhidden=hide on the I/O window.
  3. :enew to display the temporary window.

This works right up until the user does a :q from the temporary window. Instead of the screen remaining split, the top portion completely disappears. Attempting to quit from this point leads to errors about unsaved data.

Thanks in advance.

In order not to show errors about unsaved data (and not to set modified flag), use setlocal buftype=nowrite . To actually save something when vim leaves or buffer is closed, try

new
setlocal buftype=nowrite " or even buftype=nofile, if it is possible
let s:iobufnr=bufnr('%')
augroup FooSaveIO
    autocmd!
    execute "autocmd BufUnload <buffer=".s:iobufnr."> call s:SaveIO()"
augroup END

where call s:SaveIO() must be replaced with command that saves this IO. About quiting buffer without deleting window: try bufkill plugin and its :BD command. Another way is to capture BufWipeout event for temporary window and opening third window putting IO buffer here (so that when temporary buffer and associated window are closed, IO window will be shown on their place), but there are too many ways to break this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM