I'm writing a vim script. When fun() is called, I want a new window of 3 lines height is opened, with the content of register h displayed. And then, when the user moved the cursor, like hjkl, the window is closed automatically.
The code just like below. But I found the CursorMoved event is tiggered unexpected. Therefore, The window which was supposed to display and wait for user input is just flashed and closed.
Why the autocmd in fun() is executed as soon as it is installed?
Is there anything I missed or wrong?
let t:tmpname = tempname() function! s:CloseWin() let l:c_nr = bufnr("") let l:p_nr = bufwinnr(t:tmpname) exec l:p_nr . " wincmd w" close exec bufwinnr(l:c_nr) . " wincmd w" autocmd! vgroup endfunction function! s:fun() let @h = 'xxxx' silent! topleft 3 new silent! exec "edit " . t:tempname silent! 1,$delete _ silent! put h silent! call cursor(1, 0, 0) silent! delete _ silent! setlocal noswapfile silent! setlocal buftype=nofile silent! setlocal bufhidden=hide silent! setlocal nowrap silent! setlocal foldcolumn=0 silent! setlocal foldmethod=manual silent! setlocal nofoldenable silent! setlocal nobuflisted silent! setlocal nospell silent! wincmd p augroup vgroup autocmd! autocmd CursorMoved * call s:CloseWin() augroup end endfunction