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

Avoid saving lcd #10

Closed
rafi opened this issue Mar 19, 2015 · 14 comments
Closed

Avoid saving lcd #10

rafi opened this issue Mar 19, 2015 · 14 comments
Assignees
Labels

Comments

@rafi
Copy link

rafi commented Mar 19, 2015

How could I make vim-stay avoid saving lcd variable?
Reading the doc, seems the Autocommand API might be a good direction,
but I'm not sure how to work it out. Could you help? :)

@kopischke
Copy link
Collaborator

As documented in the help, the persistent local working directory is a side effect of how Vim’s view sessions work. If you have a version of Vim recent enough to support haslocaldir(), you can indeed work around that using vim-stay’s autocommand API. One way would be to temporarily suppress the local working directory when saving the session:

augroup stay_no_lcd
  autocmd!
  autocmd User BufStaySavePre  if haslocaldir() | let w:lcd = getcwd() | cd - | cd - | endif
  autocmd User BufStaySavePost if exists('w:lcd') | execute 'lcd' fnameescape(w:lcd) | unlet w:lcd | endif
augroup END

As you will have noticed, this is somewhat hacky, needing an actual, redundant directory change (the double cd -) because of the idiotically awkward way Vim implements working directory handling. As it is also incompatible with all but the most recent Vim versions, I won’t add this hack to vim-stay, much as I would like to offer an option to suppress persisting the local working directory and / or the local arglist.

@kopischke kopischke self-assigned this Mar 19, 2015
@rafi
Copy link
Author

rafi commented Mar 21, 2015

Works like a charm so far, thank you for the complete solution!

@kopischke
Copy link
Collaborator

Good to hear that. One important caveat: currently, the BufStay*Post events do not fire when errors occur while saving / loading views; the above hack thus will leave you with your current local working directory unset in such a case (I’m thinking about changing this behaviour as the Pre / Post event pairs are also used in the integration modules to handle third party state).

@kopischke
Copy link
Collaborator

PS: I’d be interested in knowing why you prefer to suppress local working directory persistence. Would you care to elucidate?

@rafi
Copy link
Author

rafi commented Mar 21, 2015

When dealing with multiple projects, I like designating each project a tab and change each tab's lcd to its project's directory. I want to suppress lcd's persistency because:

  1. I don't like the thought of my lcd changing without my knowledge, unless I do it manually.
  2. Somehow together with xolox/vim-session I experienced wrongfully mixed lcd values which led me here after discovering lcd setting entries in my vim-stay view files.

@kopischke
Copy link
Collaborator

Point 1 is a valid concern – I find the local working directory restoration a bit disorienting myself at times, which is why I have toyed with remedies. Unluckily, all of these are gross hacks of the kind I showed above, thanks to Vim’s geriatrically demented cwd handling.

Point 2 is one where I’d been remiss not to warn you that vim-session is not entirely without problems of its own; in its efforts to not inherit the warts of Vim’s session management, it has re-implemented all of that feature instead of piggy-backing on it. I found it often left my Vim in a confused and confusing state, which is why I removed it from my setup.

@rafi
Copy link
Author

rafi commented Mar 21, 2015

Thanks for all this viable information. About xolox/vim-session - it's been acting pretty good for me. I've also ported Shougo's session unite source to use vim-session. Out of curiousity - what session persistency alternative are you using now?

@kopischke
Copy link
Collaborator

Out of curiousity - what session persistency alternative are you using now?

None, really: per project tabs with a tab scoped working directory courtesy of my fork of Kana’s vim-tabpagecd, vim-stay for file level persistence, but mostly simply leaving my MacVim instance running all the time suffice to my needs :).

@blueyed
Copy link

blueyed commented Mar 25, 2016

As it is also incompatible with all but the most recent Vim versions, I won’t add this hack to vim-stay, ...

You could have a version / patch check for this - if it's known which version is required.

For what it's worth, I am not getting a lcd in the view file with recent versions of vim/neovim.

For older versions it might be even possible to remove it, based on haslocaldir() (which might need a check to be available though also).

@kopischke
Copy link
Collaborator

@blueyed the whole local working directory issue is some thing I am still mulling possible solutions over. One thing I want to avoid is adding features that only work in certain Vim versions and patch levels. A consistent set of features for all versions of Vim stay supports is a design priority of mine, hence the statement you quoted above.

@blueyed
Copy link

blueyed commented Oct 11, 2017

@kopischke
Any update on this?
As for the workaround above, it should take into account and prefer :tcd over :cd I guess if it exists?!

@blueyed
Copy link

blueyed commented Oct 11, 2017

What about removing the lcd from the mkview in the first place (by rewriting it)?! (if haslocaldir() is false)

@kopischke
Copy link
Collaborator

@blueyed absolutely none; as you probably have noticed, development of my plugins has been dormant for quite a while now, mostly because life has left me precious little time for dev work these last two years. Sorry for that.

For what it’s worth, I will add that even should I find time to pick up development again, I still probably would not go the rewriting route: keeping the breakage potential low by sticking to a well defined API (in this case: the mkview / loadview commands) instead of fiddling with internals has pretty much been a design principle for my plugins.

@blueyed
Copy link

blueyed commented Oct 14, 2017

Understood.
FWIW the following makes the workaround work with Neovim:

augroup stay_no_lcd
  autocmd!
  if exists(':tcd') == 2
    autocmd User BufStaySavePre  if haslocaldir() | let w:lcd = getcwd() | exe 'cd '.fnameescape(getcwd(-1, -1)) | endif
  else
    autocmd User BufStaySavePre  if haslocaldir() | let w:lcd = getcwd() | cd - | cd - | endif
  endif
  autocmd User BufStaySavePost if exists('w:lcd') | execute 'lcd' fnameescape(w:lcd) | unlet w:lcd | endif
augroup END

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

No branches or pull requests

3 participants