1

Set up

  • Left (L) and right (R) are two vertically split windows displaying the same file.
  • No other windows exist in the tab.
  • The uppermost displayed line in L is x (Lx) and the uppermost line in R is y (Ry).
  • The focused window is denoted by an asterisk (*L or *R).

Question

I am looking for a normal mode map to do this:

  1. If L has focus: Change *Lx Ry into *Lx Rx.
  2. If R has focus: Change Lx *Ry into Ly *Ry.

I.e., I want the offset between the windows to be zero.

Attempts

syncbind almost solves the problem, but since it requires both windows to have scrollbind set, and I rarely have it set, things get more complicated. windo would apparently solve it:

noremap <F11> :windo set scb<bar>syncbind<CR>:windo set noscb<CR>

But although it works properly for case 2, it fails for case 1 because it shifts focus to the right window.

2
  • The focus shift is probably from windo. You could save the current winnr() and use execute winnr 'wincmd w' or similar – D. Ben Knoble Feb 10 at 14:06
  • 1
    @D.BenKnoble Indeed, I hadn't looked at :h windo: "The last window (or where an error occurred) becomes the current window". I will give it a try. I only wish we had a syncbind! to remove the offset of all windows regardless of scrollbind... – Quasímodo Feb 10 at 14:08
1
noremap <expr> <F11> winnr()==1 ?
    \ ':windo set scb<CR><C-W><C-W>:syncbind<CR>:windo set noscb<CR><C-W><C-W>' :
    \ ':windo set scb<bar>syncbind<CR>:windo set noscb<CR>'

I have a strong feeling this solution exceedingly verbose and can probably be optimized, but it does the job.

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.