Open robmiller opened 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.
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!
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
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.
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
Ah; it's a problem with the placement of nested
; it needs to come outside the if
. I'll push a fix.
Pushed; not sure of the etiquette on squashing this into the previous commit, but let me know and I'll rebase and force push.
I favor squashes, but a merge isn't on the table in the short term so feel free to leave it for now.
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.
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).
Long time no code! Let me bring up another workflow I have had trouble solving.
The workflow itself first, motivation second:
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
.
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 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.
:au DirChanged
event is only available in Neovim, not in Vim8 (yet?..)++nested
part brakes horribly the CtrlP plugin.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
You want to make :cd
prompt? Are you out of your mind? Make a new command!
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
.vimrc
s, 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.