Normally I want to use autochdir to

change to the directory containing the file which was opened or selected

However, in netrw, there's a warning

However, the autochdir option:

:set acd

is problematic. Autochdir sets the current directory to that containing the file you edit; this apparently also applies to directories. In other words, autochdir sets the current directory to that containing the "file" (even if that "file" is itself a directory).

I want to make the current directory to be the directory contain the file (for files), or the browsing directory (for directories). How can I do that?

Currently I use

set autochdir
let g:netrw_keepdir = 0
autocmd BufEnter * if isdirectory(expand("%")) | set noautochdir | else | set autochdir | end

Is there any possible problem with that approach, or is there a better one?


How to set the current working directory in vim doesn't mention this.

How do I enable mouse in netrw only (terminal vim)? uses a nested autocommand; maybe the same trick will help here?

There's also a page on the tips wiki about the current directory that might help.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.