One option is for you to save what the previous window was before the windo
command. Then, later, jump to that window before jumping to your target window.
function! MyFunction()
let previous_win_id = win_getid(winnr('#'))
let current_win_id = win_getid()
windo ...
call win_gotoid(previous_win_id)
call win_gotoid(current_win_id)
endfunction
A better solution, though, is to use the win_execute()
function instead of the windo
command. The win_execute()
function prevents the side effects of entering a window while executing a command in that window, that includes updating the previous window (and the current window too.)
You need to use an explicit for
loop to run the command on every window. You can use gettabinfo()
to find the window ids of the windows in the current tab.
function! MyFunction()
for winid in gettabinfo(tabpagenr())[0]['windows']
call win_execute(winid, '...')
endfor
endfunction
If you want to iterate over every window in every tab, you can either use gettabinfo()
with no arguments (to get info for all tabs) and two nested for
loops (one for the list of tabs, then another one for the windows in that tab. Or you can use getwininfo()
directly, which returns information for all windows in all tabs in a flattened list, so you need a single for
loop.
If you're simply trying to set a variable or option in the other windows (e.g. disable 'relativenumber'
), then you can use setwinvar()
for windows in the current tab, or settabwinvar()
more generally.
For example, to disable the 'relativenumber'
in all windows of all tabs:
function! MyFunction()
for tabinfo in gettabinfo()
let tabnr = tabinfo['tabnr']
for winid in tabinfo['windows']
call settabwinvar(tabnr, winid, '&relativenumber', 0)
endfor
endfor
endfunction
Ctrl-W Ctrl-P
?