Vi and Vim Asked by Kirill Bugaev on August 31, 2021
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('.')
endif
endfunc
func s:RestoreCursor()
call cursor(s:saved_cursor[1], s:saved_cursor[2])
endfunc
func s:get_qf_bufnr()
for buf_i in getbufinfo()
if getbufvar(buf_i.bufnr, '&buftype') == 'quickfix'
return buf_i.bufnr
endif
endfor
return -1
endfunc
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.
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.
Answered by Ralf on August 31, 2021
Put this in your vimrc
augroup quickfix
autocmd!
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.
Answered by klaus on August 31, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP