Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: preserve cursor position when switching between buffers with :bn

How do I preserve my cursor position within a line when switching buffers, with :bn for example?

Vim remembers which line my cursor was on, but always moves my cursor to the beginning of the line when I switch between buffers.

like image 523
bvpx Avatar asked Dec 05 '16 23:12

bvpx


2 Answers

I'm not sure why Vim behaves this way, but fortunately, the exact position is stored in the '" mark (cp. :help 'quote).

The following :autocmd will attempt to restore the cursor to that position, using the g` command:

:autocmd BufEnter * silent! normal! g`"

Note: You can append positioning commands like zz (which positions the current line in the center of the window) or zv (which opens any folds) after the g`.

like image 174
Ingo Karkat Avatar answered Nov 16 '22 02:11

Ingo Karkat


How about

Vim cursor jumps to beginning of the line after buffer switch

TL;DR

:set nostartofline

For me g`" messes with my quickfix position.

like image 20
12 revs Avatar answered Nov 16 '22 03:11

12 revs