I did search, and got a cloeset answer form How can I automatically close preview window after I move cursor to another window in Vim?. However I found it doesn’t work with taglist plugin properly.
Here’s the code taken from the above link:
autocmd WinLeave * pc
autocmd WinLeave * call ClosePreviewWindow()
function ClosePreviewWindow()
if &pvw
pclose
endif
endfunction
I’m using a vim plugin called autopreview. Gennerally it will call ptag command to open a preview window and jump back to the buffer window. Here’s the problem, when it jumps from buffer window to preview window and back, it will trigger WinLeave event twice, then the preview window will be closed immediately after opened. So it never showed up. I tried to replace WinLeave with TabLeave/BufLeave, other problems occurred. So I came here for help.
UPDATE:
Maybe I should use tabclose/qa command?
Have you tried using the WinEnter event instead of WinLeave? In the handler you’d check to see if you’re entering the preview window and if so do nothing, otherwise check to see if the preview window is open and close it if necessary (like your code above).
Edit
Based on your description, in order to make this work you’ll need to check to see if the previous window was the preview window. This snippet will tell you that:
What that says is, “get me the option value
pvw(which happens to be window-local) for the previous window”, wherewinnr("#")gives the window number of the previous window.