There is a keybinding in Emacs that lets you scroll in another window that you're not currently working on (C-M-v
to scroll-other-window-down and C-M-V
to scroll-other-window-up). For example, in a horizontal split, while you're writing in the upper window, you can scroll the view of the bottom window without switching to that window. I'm curious whether there is a similar keybinding for that in Vim or if it's not natively there, how can we add a similar feature.
4 Answers
scroll the view of the bottom window without switching to that window
Something like this
call win_execute(win_getid(winnr('j')), "normal! \<C-E>")
winnr('j')
returns Window number of a window below the current one. win_getid()
translates Window number to :h winid
. Then win_execute()
runs an arbitrary command as if that window were active (but display is not updated, and auto-commands are not triggered).
Note: win_execute()
is not available in Neovim.
-
This answer is very educational and useful. I just wonder how can I modify
win_getid(winner('j'))
to tell him to do something on the other window assuming there are only two. The other window could be on the right, left, up, or down. Can I generalize it like that? Commented Jul 25, 2020 at 11:22 -
1From
:h winnr()
, I can see you can use#
(last window used) in place ofj
.– BiggybiCommented Jul 25, 2020 at 11:38 -
@Biggybi I saw that, too. But the last window is not always the other window in the same tab. I might switch between tabs and I guess using this command would not point me to the other window in the same tab but the window back in the tab I switched from. Commented Jul 25, 2020 at 11:48
-
1@Biggybi My bad! You're 100% right. I just checked out that command in action and it works right, even if I just switch from another window in another tab. Looks like tabs are separate environments in Vim. Commented Jul 25, 2020 at 11:57
-
1@SaeedAhadian There are "window IDs" and "window numbers". Both are used here. Window ID is a unique number for a whole Vim session (1000+); while "window number" is basically "a window position within tab" (1, 2, 3 etc.).– MattCommented Jul 25, 2020 at 13:30
I use the mouse for this. With set mouse=a
I can scroll any open window from any mode (including normal mode). It’s not the purest solution, but it works very well for me.
Assuming you have 2 windows. Then map like this in your .vimrc
:
map <F11> <C-W><C-W><C-D><C-W><C-W>
It will map F11 to do these actions:
<C-W><C-W>
- move cursor to second window.<C-D>
- scroll second window downwards half of screen.<C-W><C-W>
- move cursor back to first window.
Change part 2 to whatever you want, look at :h scroll.txt
to see more commands. For example, <C-U>
scrolls half screen upward. <C-E>
, <C-Y>
scroll one line.
See:
:h CTRL-W_CTRL-W
:h windows.txt
:h scroll.txt
This works well for me:
nnoremap <M-PageDown> <C-w>p<C-d><C-w>p
nnoremap <M-PageUp> <C-w>p<C-u><C-w>p
I use this in Neovim.