In my vimscripts I sometimes have to use the windo command to execute an action on each open split.

The problem with this command is that it cycles through all the open splits and ends on a different split than the one I was on when I started the command.

I understand the causes of this behavior, but I need to find a workaround to get back on my original split after using windo. A solution I found is to do that:

let l:currentWindow=winnr()
windo [whatever I want to apply to all the splits]
exe l:currentWindow . "wincmd w"

Basically before using windo I get the number of my current window in the variable l:currentWindow. After using window I use exe to execute wincmd w which is the equivalent to Ctrl-w+w which allows to go to a window according to its number.

So far I haven't had a problem with this solution but I was wondering if there is another way to execute an action on each split without changing the cursor position?

I'm sure I'm not the first one to encounter this problem and other vimmers probably found different/better workaround.

1 Answer 1


Create a new :windo command witch restores the window.

command! -nargs=+ -complete=command Windo 
      \ let s:currentWindow = winnr() |
      \ execute "windo <args>" |
      \ exe s:currentWindow . "wincmd w"

This just wraps up your manual method into command :Windo

  • 1
    To avoid polluting your jumplist you can use keepjumps before windo and wincmd. Also might be faster with noautocmd.
    – Nate Bosch
    Feb 3, 2017 at 3:44

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.