1

The article at http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file offers two suggestions.

  • One that sets the autochdir option.

    set autochdir
    
  • Another that uses autocmd on BufEnter event.

    autocmd BufEnter * silent! lcd %:p:h
    

This question is about autocmd only.

On StackOverflow, there are other posts where the answers suggest using autocmd BufEnter to solve this problem.

But lcd is local to a window. The current directory for every window remains intact unless we explicitly change it. So I am curious why autocmd BufEnter is being suggested here. It feels excessive to me because autocmd BufEnter executes lcd every time I switch between windows, say with Ctrl-w w, even when the buffer in window I am switching to hasn't changed.

I think, it is sufficient to execute lcd whenever a buffer in a window changes, thus autocmd BufwinEnter is sufficient.

What can go wrong if autocmd BufWinEnter is used instead?

For example, let us consider the following alternate solution.

autocmd BufWinEnter * lcd %:p:h

Can you describe a scenario where the autocmd BufEnter command would do the right thing but autocmd BufWinEnter command would not?

Community
  • 1
  • 1
Susam Pal
  • 32,765
  • 12
  • 81
  • 103
  • Is there a reason, you don't want to set 'autochdir'? – Christian Brabandt Mar 03 '16 at 10:50
  • @ChristianBrabandt I am not trying to avoid `set autochdir`. I should note that `:help autochdir` mentions, *"Note: When this option is on some plugins may not work"*, but that's not the reason why I want to avoid it. I haven't encountered any issues in the plugins I use due to `set autochdir`. I wanted this question to be about `autocmd BufEnter` because this workaround appearing in several places on the internet piqued me curiosity. – Susam Pal Mar 03 '16 at 11:51
  • Ah okay. But first of all, I think plugins need to take of that and have to be fixed, if they can't take of of 'autochdir' and second of all, I think this information comes from a time, when that option was a little bit buggy. I remember issues with netrw, but that should be solved long ago. – Christian Brabandt Mar 03 '16 at 12:51

1 Answers1

3

In theory, I think the BufWinEnter autocommand might be the better approach, as it avoids resetting the directory every time you enter the buffer.

However, you might wonder, what happens, if the user changes the current directory for the current window. Using the BufEnter autocommand, this would get reset once you re-enter that particular buffer, while the BufWinEnter autocommand will not trigger. That might be a problem or not, but is something one should consider when deciding which autocommand to use.

BTW: I have never understood, why one would use such an autocommand instead of simply setting 'autochdir'. Because that's what this option is for. I think in the past, the autochdir option has had some bugs, but this has been fixed, as far as I know, so such workarounds are not needed anymore.

Christian Brabandt
  • 8,038
  • 1
  • 28
  • 32
  • Because it's easier to ask about a (made-up) task of changing to file's directory than to explain e. g. that you want to cd to the root of the project of that file, or something else. – intelfx May 03 '17 at 00:39