I have two buffers open in vim using a vertical split which are linked using :set scrollbind.
Is there a way to switch between the windows so that the cursor remains on the same (relative) row when I do switch between them using the ctrl+w commands?
A mapping will do what you need
Do this in the left window:
:nmap <right> :let linenum=getpos('.')[1]\|:wincmd l\|:call cursor(linenum,0)<cr>
and do this in the right window:
:nmap <left> :let linenum=getpos('.')[1]\|:wincmd h\|:call cursor(linenum,0)<cr>
Then you can use the left and right arrows to switch between the windows and the cursor will go to the same line in the other window.
Edit:
I didn't read your question carefully (shame on me!:).
Here's how to get exactly what you wanted:
On the left window:
:nnoremap <right> :let offset=winline()\|wincmd l\|exe 'normal ' . offset . 'H'<cr>
and on the right:
:nnoremap <left> :let offset=winline()\|wincmd h\|exe 'normal ' . offset . 'H'<cr>
Have fun!
Vim version 7.3 added a cursorbind
option:
When this option is set, as the cursor in the current window moves other cursorbound windows (windows that also have this option set) move their cursors to the corresponding line and column. This option is useful for viewing the differences between two versions of a file (see 'diff'); in diff mode, inserted and deleted lines (though not characters within a line) are taken into account.
^W+r
(switch buffers) followed by ^W^W
(return cursor back)
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