I am trying to find a function which will switch back to the original window in a split view for vim. I know about shortcut and I also know about the function :call cursor. But is there a function which can let me switch back to the previous split window so I could stick it in my vim function?
In a vimscript you can use:
" Save the window where you are currently
let l:currentWindow=winnr()
" Do stuff where you change of window
" Go back to the original window
exe l:currentWindow . "wincmd w"
For more information the doc is always an excellent reading:
:h wincmd
:h winnr()
Edit Another way to do it is to use wincmd p
:
wincmd
is the vimscript equivalent to Ctrlw in normal mode.
In normal mode when you change of window you can use Ctrlw + p to come back to the previous window. So in vimscript you simply use:
wincmd p
To go back to the previous window.
Of course if the rest of your function use more than 2 splits you will not go back to your initial window but if you have only two splits it can be lighter than using a variable to keep the number of your window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With