I've been able to reproduce this, and here's what's happening.
Setup
- Open vim
- Type a few lines of text
:Sexplore
- With the focus on netrw, click on the buffer with text (esp. on the line numbers).
That buffer switches to a new buffer (generally named after the line you clicked on).
Why?
netrw
maps <LeftMouse>
to call the scriptlocal function NetrwLeftmouse(1)
. This function is responsible for editing the files you click on. It tries to abort if you don't click in the window (if ... || v:mouse_win != winnr()
), but I believe by the time this executes the mouse window and winnr()
have been aligned (the mapping actually performs a regular <LeftMouse>
first, so focus has already changed).
The function happily carries on and edits a new buffer. <C-o>
is sometimes enough to get you back, but because the function uses NetrwKeepj
, the jumplist doesn't change, so <C-o>
may not suffice.
Solutions
- Don't use the mouse in netrw (easy to give up, but might accidentally still get you). Combine with unmapping the mouse keys in
netrw
(in ~/.vim/after/ftplugin/netrw.vim
for example) for maximum effect.
- ??? This would require a serious patch for a strange edge case, and I'm not quite sure how to do it yet.