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

Restore sessions automatically #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

robmiller
Copy link

When starting Vim, check if a Session.vim file exists in the current directory. If it does, and if we haven't been passed any arguments, then load the session.

I'm not sure if you wanted this to be an setting thing or even if you wanted to leave it up to people's .vimrcs, but I figured it couldn't hurt to submit a pull request — I would certainly think that the automatic behaviour fits the philosophy of the plugin.

@tpope
Copy link
Owner

tpope commented Mar 11, 2013

I am going to give this some bake time in my personal config before evaluating merging it in. It does have the potential to confuse/annoy people into uninstalling the plugin.

@robmiller
Copy link
Author

Yeah, I was in two minds about it. I couldn't think of an elegant way to make it a non-default setting in what's otherwise an elegant and setting-less plugin, either — but I'll cede to your infinite knowledge on that front!

@tpope
Copy link
Owner

tpope commented Mar 12, 2013

When I call Vim with -S, and I also have that autocmd, and the session in question changes the current directory, I get an error. Here's my fix:

autocmd VimEnter *
      \ if !argc() && empty(v:this_session) && filereadable('Session.vim') |
      \   nested source Session.vim |
      \ endif

When starting Vim, check if a Session.vim file exists in the current
directory. If it does, and if we haven't been passed any arguments, then
load the session.
@robmiller
Copy link
Author

Great spot. I can reproduce, and can verify that doing the check within the autocmd works fine. I've updated this branch and rebased into the same commit as before.

@robmiller
Copy link
Author

Actually, I'm now getting this error if I start vim in a directory where a session is present, when starting without -S:

Error detected while processing VimEnter Auto commands for "*":
E492: Not an editor command:    nested source Session.vim | endif

@robmiller
Copy link
Author

Ah; it's a problem with the placement of nested; it needs to come outside the if. I'll push a fix.

When a session file existed, but no manual session was specified, Vim
was throwing a syntax error from our autocmd definition.

This fixes the problem by moving the `nested` declaration outside the
checks for the existence of a session.
@robmiller
Copy link
Author

Pushed; not sure of the etiquette on squashing this into the previous commit, but let me know and I'll rebase and force push.

@tpope
Copy link
Owner

tpope commented Mar 15, 2013

I favor squashes, but a merge isn't on the table in the short term so feel free to leave it for now.

@monokrome
Copy link

If this functionality is added, then I would hope that the sessions directory is configurable in some way or another. I am sure that I am not the only user who doesn't like having Session.vim sitting in my project directories.

I created monokrome/vim-lazy-obsession for this, but I think that it would be nice if it was just part of vim-obsession as long as it was configurable enough.

@vphantom
Copy link

If you're interested in a similar take, maybe look at vphantom/vim-obsession. I've added two commits in this fork: one to load sessions if Vim is invoked with no arguments (this topic), and another one to use ".session.vim" instead of "Session.vim" (personal perference — I hate clutter).

@ulidtko
Copy link

ulidtko commented Jul 12, 2019

Long time no code! Let me bring up another workflow I have had trouble solving.

The workflow itself first, motivation second:

  • launch nvim-qt GUI, without arguments off course;
  • :cd into a project directory with a .session.vim file.

Now, why. I'm perfectly comfortable with vim in terminal. But I've noticed that in modern multi-tab split-window terminals, often containing nested tmux/ssh mixes, detached containers and bg'd jobs, it can be hard to locate the "primary project" I'm currently trying to concentrate working on.

Exactly that, having a dedicated window which I can always switch to instantly, is a major part of why I've been using stuff like Atom for years. So, back to Vim, and I'm transplanting my workflow onto nvim-qt.

Passing arguments like -S, or using VimEnter isn't practical for the standalone GUI case. The best I have is :cd, then not forgetting to manually source the session file, if there's one.

So, how to solve this? Pretty easy in fact, there's a DirChanged autocmd event.

autocmd DirChanged * ++nested
      \ if empty(v:this_session) && filereadable('.session.vim') |
      \   source .session.vim |
      \ endif

P.S.: the filename Session.vim disappoints me as well, so +1 on .session.vim.

@tpope
Copy link
Owner

tpope commented Jul 15, 2019

What happens if I have a bunch of files open and then I :cd to a directory that has a session file? Does it blow away all my existing tabs and windows? What if I don't want that? What if I forget the session file is there?

@ulidtko
Copy link

ulidtko commented Jul 17, 2019

@tpope well, those are valid concerns; but you can solve them with dead simple prompts ("Session found in CWD, load? (y/n)" etc), just like xolox/vim-session does. Obviously, there'd better be no prompts on the "hot" UI interaction paths, but in risky corner cases like you describe the python's EAFP doesn't really shine :)

FWIW, I'll share some of my findings.

  1. The :au DirChanged event is only available in Neovim, not in Vim8 (yet?..)
  2. Missing the ++nested part brakes horribly the CtrlP plugin.
  3. I've found that thaerkh/vim-workspace together with this snippet works best for me:
let g:workspace_session_name = '.session.vim'  
let g:workspace_session_disable_on_args = 1  
if has('nvim')
    au DirChanged * ++nested
        \ if empty(v:this_session) && filereadable(g:workspace_session_name) |
        \   exe ':source' . g:workspace_session_name |
        \ endif
endif

@tpope
Copy link
Owner

tpope commented Jul 17, 2019

You want to make :cd prompt? Are you out of your mind? Make a new command!

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

Successfully merging this pull request may close these issues.

5 participants