3

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
  • Had anyone ever found a solution for this? I'm hitting the exact same issue. (tmp window that I want to close on cursor moved) – Olical Mar 6 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.