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

Restore sessions automatically

Open robmiller opened this issue 11 years ago • 14 comments

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.

robmiller avatar Mar 11 '13 20:03 robmiller

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.

tpope avatar Mar 11 '13 20:03 tpope

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!

robmiller avatar Mar 11 '13 20:03 robmiller

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

tpope avatar Mar 12 '13 22:03 tpope

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 avatar Mar 12 '13 22:03 robmiller

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 avatar Mar 15 '13 12:03 robmiller

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

robmiller avatar Mar 15 '13 12:03 robmiller

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

robmiller avatar Mar 15 '13 12:03 robmiller

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

tpope avatar Mar 15 '13 17:03 tpope

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.

monokrome avatar May 01 '13 22:05 monokrome

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

vphantom avatar Apr 25 '16 11:04 vphantom

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.

ulidtko avatar Jul 12 '19 14:07 ulidtko

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?

tpope avatar Jul 15 '19 01:07 tpope

@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

ulidtko avatar Jul 17 '19 15:07 ulidtko

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

tpope avatar Jul 17 '19 16:07 tpope