vim-vinegar
vim-vinegar copied to clipboard
Is it possible to set the alternate buffer?
After only a few days using vinegar, I'm now quite addicted to hitting -
to navigate around. It's a lot faster than what I used to do, so thank you!
The one habit I can't break is that once I navigate to a new file, I want to flip back to the last one I was on, and I do :b#
. This dumps me back into the file browser rather than to the last file. I get surprised every time, and end up having to open up Unite to browse my open buffers.
Do I need to just break my bad habit, or does this make sense as something that vinegar could make behave better?
You can try adding keepalt
to the appropriate spots in s:opendir()
, but I suspect the problem is on the netrw end.
execute 'keepalt' a:cmd ...
I think this is related to the issue I was about to report. It's pretty annoying: vinegar breaks <Ctrl-^>
(aka "<Ctrl-6>
"), which I use all the time.
Steps to reproduce
- Open up
gvim
to edit a file. Say,$ gvim foo
. - Press
-
to bring up netrw, and open a new file. - Hit
<Ctrl-^>
to go back to the alternate buffer.
- Expected behaviour:
- I should end up in
foo
.
- I should end up in
- Actual behaviour:
- I end up back in
netrw
.
- I end up back in
This is never what I want, and it's probably never what anyone wants. (Though as you say, it could well be netrw's fault.)
Okay that's just ridiculous. The netrw buffer is the alternate buffer here. Skipping it might be a bit more DWIM but the existing behavior is definitely not "broken".
Good point. I was just plain wrong when I said vinegar broke something -- sorry!
However, just because it's not broken doesn't mean it couldn't be better. As I said, it seems to me that no vinegar user ever wants <Ctrl-^>
to go to netrw: if they wanted netrw, they would just hit -
. (It's fewer keystrokes, not to mention more natural to a vinegar user.) And at least two people (and probably more) expect the alternate buffer to be the one before netrw.
A policy of "never make netrw the alternate buffer for vinegar users" would solve both my problem and the original poster's. I don't know how hard it would be to implement, but it seems like it would clearly improve user experience. And making netrw better for users is, of course, the whole point of vinegar.
Thinking about this more:
I don't think you'd want to change s:opendir()
. Once you go into netrw, the alternate file is the buffer you were just editing -- which is what you want it to be. So the keepalt
should happen whenever you open the new file, from netrw. That means you're right -- this is netrw's domain.
I found that the issue has been discussed on the vim-dev Google group. Opinions were split for and against the keepalt
(but note that vinegar hadn't been published yet).
Anyway... here is the solution.
-
Add to
.vimrc
:" We need netrw >= v150f. Plugin 'eiginn/netrw' " <Ctrl-^> should go to the last file, not to netrw. let g:netrw_altfile = 1
-
If it doesn't work, your vim is probably too old (the
master
branch requires 7.4.231).-
Check out an earlier, less-demanding version.
git tag v150h d424dde6ac100a2 git checkout v150h
-
Pin the plugin in vundle: change the line as follows:
Plugin 'eiginn/netrw', {'pinned': 1}
-
Once I get the new netrw working, I've confirmed that it does the wrong thing without g:netrw_altfile
set, and the right thing with g:netrw_altfile = 1
.
Fixing this with a preference in netrw seems like the correct solution, not a vinegar problem. @chiphogg's instructions work perfectly for me.
@chiphogg
Thank you. Just adding let g:netrw_altfile = 1
to vimrc worked just fine with Vim 7.4.560.
I've recompiled Vim, pulled in netrw, tagged and checked out that commit, and added that altfile line to my vimrc, and <C-^>
still takes me back to netrw after using it to open a file. Sad trombone.
Note that g:netrw_altfile = 1 still doesn't seem to keep the old file as the alternate one.
If I try to use ctrl-^ after opening a new file with netrw, I get an error saying "No alternate file", when I'd obviously prefer to go back to the previous file.
Thanks for the solution, @chiphogg! Works perfectly for me using vim 7.4-843 with netrw v154b. And thanks, @tpope for the great plugin!
So sorry for reviving this 6+ year old thread. But as @boblehest said, using g:netrw_altfile = 1
doesn't fix the problem because there is now no alternate file at all unless there was an even older file recorded as the alternate file. Am I missing something?
I'm not sure I've understood the OP correctly but what I was looking for is a way to open netrw after pressing to a new window, leaving my current file in its place. I've tried @chiphogg without any success. Any ideas on what I'm doing wrong? Thanks
A little indirect but <C-W>s-
will do that.
Thanks, that is perfect.
Could these keys be mapped it to simple -
? I tried nmap - <C-W>s-
but doesn't seem to work.
Another request, altough I'm not sure that it vimvinegar related: is there any way for the split window opening the file's directory to go to the lefta of the file?