I need to set cursor position when opening quickfix window. I have tried

autocmd BufEnter * call cursor(2, 2)

It doesn't set cursor for quickfix buffer immediately after I have opened it.

UPD: I am trying to save current cursor position of quickfix window when close it and restore when open again.

let s:saved_cursor = [0, 1, 1]
autocmd BufDelete * call s:SaveCursor()
autocmd BufWinEnter quickfix call s:RestoreCursor()

func s:SaveCursor()
    if expand("<abuf>") == s:get_qf_bufnr()
        let s:saved_cursor = getpos('.')

func s:RestoreCursor()
    call cursor(s:saved_cursor[1], s:saved_cursor[2])

func s:get_qf_bufnr()
    for buf_i in getbufinfo()
        if getbufvar(buf_i.bufnr, '&buftype') == 'quickfix'
            return buf_i.bufnr
    return -1

It doesn't work. Scenario is below:

1) Open and fill quickfix buffer by cs find command

2) Move cursor in quickfix window

3) Close quickfix window by :cclose (and save cursor position by s:SaveCursor() function)

4) Open quickfix again by :copen (and restore cursor position by s:RestoreCursor() function)

Probably BufWinEnter autocmd event is not suitable for my purpose and I am trying to set cursor before quickfix window opened. On BufEnter event I can set cursor position if quickfix window is already opened. But if I close quickfix window and open it again cursor will be at first line first column.

  • 1
    Possible duplicate of autocmd with quickfix window
    – Ralf
    Feb 24, 2019 at 12:37
  • So, you want to set the cursor position in a quickfix list? From where? I mean, are you writing a script? If so, then do you assume the quickfix window is already open? I originally thought you just wanted to open the quickfix window after a quickfix command. Provide a detailed scenario, cause your question doesn't really makes sense to me.
    – 3N4N
    Feb 24, 2019 at 17:11
  • I have added some details in question. Feb 24, 2019 at 17:36
  • Have you checked, that s:SaveCursor() called?
    – Ralf
    Feb 24, 2019 at 18:56
  • Ralf, thanks for replay. I checked s:SaveCursor() on BufDelete event, it works. But your BufLeave variant looks more logical. Feb 24, 2019 at 19:55

2 Answers 2


From the source code it looks like the position in the quickfix window is determined by the current position in the quickfix list. This is set after the window was opened, hence after your autocmd ran.

First you need the autocmd event BufLeave, to save the cursor position (the quickfix buffer is not deleted). Then change your function s:RestoreCursor to contain:

call cursor(g:saved_cursor[0], g:saved_cursor[1])
echom "My Pos: " . string(getpos('.'))

This should print the cursor position you want to restore. But in the end the cursor ends up on the line corresponding to the current position in the quickfix list.

I have no idea how to prevent this.

  • Now s:RestoreCursor shows cursor position and it is exactly which in s:saved_cursor variable. Cursor is really moved to current position in the quickfix list. Any way thanks a lot for explanation of how things work. Feb 24, 2019 at 20:11

Put this in your vimrc

augroup quickfix
    autocmd QuickFixCmdPost [^l]* nested cwindow
    autocmd QuickFixCmdPost    l* nested lwindow
augroup END

From :h QuickFixCmdPost, this autocommand event is triggered after a quickfix command is run. And [^l]* makes sure that the quickfix command is not actually a location list command. From :h location-list, a location list is a window-local quickfix list, and to create a location list window, you'll just have to put an l at the beginning of the equivalent quickfix command. So, depending on whether the command has l at the beginning, the quickfix or location-list window is opened.

  • 1
    Thanks for answer. But I don't understand what this commands should do? I have putted it to my .vimrc, but I still can't set cursor in quickfix window by autocmd event when I open quickfix window. Feb 24, 2019 at 16:13
  • 2
    As the OP suggests, an accompanying explanation could go a long way
    – D. Ben Knoble
    Feb 24, 2019 at 16:33
  • I thought OP just wanted to open quickfix window after a quickfix command. I guess, s/he edited the question after I read. I don't know what s/he wants.
    – 3N4N
    Feb 24, 2019 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.