Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to split window on the same line

Tags:

vim

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?

like image 252
Chris Avatar asked Aug 23 '11 21:08

Chris


3 Answers

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!

like image 122
holygeek Avatar answered Oct 02 '22 01:10

holygeek


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.

like image 23
Matteo Riva Avatar answered Oct 02 '22 01:10

Matteo Riva


^W+r (switch buffers) followed by ^W^W (return cursor back)

like image 30
dimba Avatar answered Oct 01 '22 23:10

dimba