Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to set the alternate buffer? #25

Open
danielmatz opened this issue Apr 11, 2014 · 15 comments
Open

Is it possible to set the alternate buffer? #25

danielmatz opened this issue Apr 11, 2014 · 15 comments

Comments

@danielmatz
Copy link

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?

@tpope
Copy link
Owner

tpope commented Apr 11, 2014

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

@tpope
Copy link
Owner

tpope commented Apr 11, 2014

execute 'keepalt' a:cmd ...

@chiphogg
Copy link

chiphogg commented May 9, 2014

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

@tpope
Copy link
Owner

tpope commented May 9, 2014

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

@chiphogg
Copy link

chiphogg commented May 9, 2014

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
Copy link

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.

@sj26
Copy link

sj26 commented Sep 6, 2014

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

@ironhouzi
Copy link

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

@gfixler
Copy link

gfixler commented Jan 28, 2015

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.

@boblehest
Copy link

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.

@rafaeln
Copy link

rafaeln commented Sep 18, 2015

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

wincent added a commit to wincent/wincent that referenced this issue Feb 9, 2016
It has long annoyed me that once I've started to explore the directory
of a file using my `-` mapping, I can't easily get back to the file with
`<space><space>` (ie. `<leader><leader>`) or `<C-^>`. The same issue
affects vim-vinegar (although I am not using right now). There is a
workaround when using netrw, which is setting the `g:netrw_altfile`
option, which causes netrw to use `:keepalt` under the covers:

    tpope/vim-vinegar#25

Anyway, I've added a similar option to NERDTree in my fork and have an
open PR for it here:

    preservim/nerdtree#550

* roles/dotfiles/files/.vim/bundle/nerdtree 4ebbb53...9843fd3 (1):
  > Make window creation command configurable
lencioni added a commit to lencioni/dotfiles that referenced this issue Feb 21, 2016
It has long annoyed me that once I've started to explore the directory
of a file using vim-vinegar's `-` mapping, I can't easily get back to
the file with `<space><space>` (ie. `<leader><leader>`) or `<C-^>`.
Thanks to @wincent [0], I have found a workaround:

  tpope/vim-vinegar#25

Unfortunately, this doesn't fix my other annoyance of netrw polluting
the jumplist. Looking at `:help netrw` it looks like `g:netrw_keepj`
intends to do this and it is defaulted to "keepj" (on), but it isn't
quite as good as I would like.

[0]: wincent/wincent@337792ff
@Maltimore
Copy link

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?

@zen3gr
Copy link

zen3gr commented Feb 20, 2022

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

@tpope
Copy link
Owner

tpope commented Feb 20, 2022

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

@zen3gr
Copy link

zen3gr commented Feb 21, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants