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

Using :Files causes window-local current directory to change #678

Closed
3 of 9 tasks
Asheq opened this issue Aug 6, 2018 · 8 comments
Closed
3 of 9 tasks

Using :Files causes window-local current directory to change #678

Asheq opened this issue Aug 6, 2018 · 8 comments

Comments

@Asheq
Copy link

Asheq commented Aug 6, 2018

  • Category
    • Question
    • Bug
    • Suggestion
  • OS
    • Linux
    • macOS
    • Windows
    • Etc.
  • Vim
    • Vim
    • Neovim (NVIM v0.3.1)

To reproduce:

nvim -Nu <(curl https://gist.githubusercontent.com/junegunn/6936bf79fedd3a079aeb1dd2f3c81ef5/raw)
:echo haslocaldir(0)         " returns 0 as expected
:Files                       " choose a file to open
:echo haslocaldir(0)         " returns 1 (not expected)

This bug exists on neovim (NVIM v0.3.1) but not vim (VIM - Vi IMproved 8.1 Included patches: 1-202)

Thanks for your help! Please let me know if you need any more information.

@junegunn
Copy link
Owner

junegunn commented Aug 7, 2018

Can you describe why this is a problem? fzf can temporarily change the working directory using :lcd primarily to support searching inside arbitrary directories (e.g. :Files ~/foo) and displaying files in relative paths, and that's why haslocaldir(0) returns 1, but the actual working directory is restored.

@Asheq
Copy link
Author

Asheq commented Aug 7, 2018

Thanks for the quick reply!

Having a window-local working directory that is not purposeful (even if it is the same as the global working directory) can cause unexpected behavior. Here's a demonstration. Start with the following setup:

mkdir -p /tmp/dirA/dirB
cd /tmp/dirA/dirB
touch someFile.txt
nvim -Nu <(curl https://gist.githubusercontent.com/junegunn/6936bf79fedd3a079aeb1dd2f3c81ef5/raw)
:q " to close the extra window

Then do the following steps:

  1. :Files
    Select someFile.txt. As an unintended side effect, the window-local working directory will be set to /tmp/dirA/dirB

  2. :split
    This creates a new window that inherits the window-local working directory, /tmp/dirA/dirB

  3. :cd ..
    This sets the global working directory to /tmp/dirA and removes the window-local working directory for the current window. The problem is that the other window continues to have its old window-local working directory of /tmp/dirA/dirB. This means that typing :pwd in the current window will return /tmp/dirA but in the other window will print /tmp/dirA/dirB

Hope that explanation helps. Please let me know if you need any more info.

I think that the following things should be restored perfectly by :Files and other fzf commands:

  1. global working directory
  2. tab-local working directory
  3. window-local working directory

@junegunn
Copy link
Owner

junegunn commented Aug 8, 2018

Thanks for the explanation. I basically do not change directories within Vim, so this wasn't an issue to me. Is there a way to unset local directory?

@Asheq
Copy link
Author

Asheq commented Aug 9, 2018

The only ways to unset the window-local directory that I'm aware of are:

  1. setting the tab-local working directory with :tcd someDir (this removes the window-local directory)
  2. setting the global working directory with :cd someDir (this removes the window-local directory and tab-local directory)

@Asheq
Copy link
Author

Asheq commented Aug 9, 2018

I believe you can follow this pattern to restore all working directories to their previous state after using lcd to temporarily change the window-local directory to do something arbitrary:

let old_effective_wd = getcwd()

" Check what working dirs exist
let tab_wd_exists = haslocaldir(-1)
let window_wd_exists = haslocaldir()

" Change window-local working dir in order to do stuff
execute 'lcd ' . some_dir

" Do stuff here
" ...

" Restore working dirs
if window_wd_exists
	" Restore window-local working dir to old value
	execute 'lcd ' . old_effective_wd
elseif tab_wd_exists
	" Remove window-local working dir, keep tab-local working dir
	execute 'tcd ' . old_effective_wd
else
	" Remove window-local and tab-local dirs
	execute 'cd ' . old_effective_wd
endif

@junegunn
Copy link
Owner

Please update the main fzf repo and see if the problem is fixed for you.

@Asheq
Copy link
Author

Asheq commented Aug 13, 2018

It works perfectly now. I ❤️ FZF!

@TC72
Copy link

TC72 commented Jun 12, 2020

There is an issue with this when using tcd in neovim. If only tcd has been used to set the current directory of the tab then lcd shouldn't be used to override it.
This has been driving me crazy for a couple of weeks as I work with separate tabs where I set tcd so FZF is limited to the subdirectories of that tab. For example a tab for the scss folder and another for the javascript folder.

I'm never 100% sure which FZF command is doing it but I'll suddenly find one of my tabs is no longer using the correct directory. Tonight I narrowed it down to always happening with :Files which I don't normally use but it proved fzf.vim was doing this.

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

No branches or pull requests

3 participants