Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim function to switch between splits

Tags:

vim

vim-plugin

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?

like image 941
SUB Avatar asked Sep 26 '15 21:09

SUB


1 Answers

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.

like image 130
statox Avatar answered Oct 19 '22 20:10

statox