I would like the preview window disappears automatically when cursor is not in the preview window or preview window loses the focus. Is it possible?
You may want to have a look on autocommands. A simple example would be:
autocmd WinLeave * pc
Which calls pc
(close preview window) every time you leave a window. A more involved example could use a separate function that performs extra checking:
autocmd WinLeave * call ClosePreviewWindow()
function ClosePreviewWindow()
if &pvw
pclose
endif
endfunction
Check :h autocmd.txt
to learn more. This file has a complete listing of autocommand events in section 5, so you can choose the one that fits better.