tpope / vim-obsession

obsession.vim: continuously updated session files
http://www.vim.org/scripts/script.php?script_id=4472
1.76k stars 70 forks source link

Restore sessions automatically #2

Open robmiller opened 11 years ago

robmiller commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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
robmiller commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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

robmiller commented 11 years ago

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 commented 11 years ago

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

monokrome commented 11 years ago

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 commented 8 years ago

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 commented 5 years ago

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

The workflow itself first, motivation second:

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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