As title. After doing windo it seems like Vim forgot where it started.


I just found that NeoVim provides some functions to do so easily:

local cur_win = vim.api.nvim_get_current_win()
-- do your windo, for example:
vim.cmd("windo if &buftype != 'nofile' | let g:non_float_total += 1 | endif")

And since my problem was originally asked in context of Vim before edit(yeah, it's embarrassing that I'm writing NeoVim plugin in Lua but always resort to VimScript first :p ), here is an answer that resolves that, by the power of ancient great people:

" Just like windo, but restore the current window when done.

function! WinDo(command)
  let currwin=winnr()
  execute 'windo ' . a:command
  execute currwin . 'wincmd w'
com! -nargs=+ -complete=command Windo call WinDo(<q-args>)

The source: https://vim.fandom.com/wiki/Windo_and_restore_current_window

  • 2
    The answer to the title question is basically :execute winnr 'wincmd w' aka :call win_gotoid(win_getid(winnr)) where winnr is the desired window number. Window IDs are sometimes better, though.
    – D. Ben Knoble
    Jan 4 at 13:51

Your Answer

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

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