vim-vinegar icon indicating copy to clipboard operation
vim-vinegar copied to clipboard

Is it possible to set the alternate buffer?

Open danielmatz opened this issue 10 years ago • 15 comments

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?

danielmatz avatar Apr 11 '14 17:04 danielmatz

You can try adding keepalt to the appropriate spots in s:opendir(), but I suspect the problem is on the netrw end.

tpope avatar Apr 11 '14 18:04 tpope

execute 'keepalt' a:cmd ...

tpope avatar Apr 11 '14 18:04 tpope

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

  1. Open up gvim to edit a file. Say, $ gvim foo.
  2. Press - to bring up netrw, and open a new file.
  3. Hit <Ctrl-^> to go back to the alternate buffer.
  • Expected behaviour:
    • I should end up in foo.
  • Actual behaviour:
    • I end up back in netrw.

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.)

chiphogg avatar May 09 '14 15:05 chiphogg

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".

tpope avatar May 09 '14 16:05 tpope

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.

chiphogg avatar May 09 '14 17:05 chiphogg

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.

  1. 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
    
  2. If it doesn't work, your vim is probably too old (the master branch requires 7.4.231).

    1. Check out an earlier, less-demanding version.

      git tag v150h d424dde6ac100a2
      git checkout v150h
      
    2. 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.

chiphogg avatar May 10 '14 01:05 chiphogg

Fixing this with a preference in netrw seems like the correct solution, not a vinegar problem. @chiphogg's instructions work perfectly for me.

sj26 avatar Sep 06 '14 03:09 sj26

@chiphogg Thank you. Just adding let g:netrw_altfile = 1 to vimrc worked just fine with Vim 7.4.560.

ironhouzi avatar Jan 18 '15 09:01 ironhouzi

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.

gfixler avatar Jan 28 '15 08:01 gfixler

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.

boblehest avatar Feb 26 '15 00:02 boblehest

Thanks for the solution, @chiphogg! Works perfectly for me using vim 7.4-843 with netrw v154b. And thanks, @tpope for the great plugin!

rafaeln avatar Sep 18 '15 23:09 rafaeln

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?

Maltimore avatar Feb 14 '22 22:02 Maltimore

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

zen3gr avatar Feb 20 '22 12:02 zen3gr

A little indirect but <C-W>s- will do that.

tpope avatar Feb 20 '22 23:02 tpope

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?

zen3gr avatar Feb 21 '22 07:02 zen3gr