8

I often use :Ex or :Sex to explore the filesystem from an opened file directory, or just invoke vim passing a directory to start exploring the filesystem and then opening a file.

In any case, while navigating the directory tree, using the :edit {file} command to open new files always takes paths relative to the current directory from which vim was executed; so:

~# vim .
-> navigate to Development/
-> open a file
:e <tab> will complete path from home dir

or:

~# vim Development/
-> open a file
:Sex
:e <tab> will complete paths from Development/

is it possible (automagically or with a command) to change vim current working directory to the current selected directory while navigating the filesystem?

5

It is possible by setting the netrw configuration variable g:netrw_keepdir to 0 (default is 1).
To make it permanent, add in the .vimrc file this line:

let g:netrw_keepdir=0
7

Yes, vim has a :cd command, which either prints the current directory or changes the current directory. In the help is this - note the last lines:

:cd[!] {path}           Change the current directory to {path}.
                        If {path} is relative, it is searched for in the
                        directories listed in |'cdpath'|.
                        Does not change the meaning of an already opened file,
                        because its full path name is remembered.  Files from
                        the |arglist| may change though!
                        On MS-DOS this also changes the active drive.
                        To change to the directory of the current file: >
                                :cd %:h

As noted at the top of the help for :cd, this affects vim's working directory, not just what :Ex returns to or internal commands use, so external commands will also use the new location.

  • Is there a "directory-changed"-like event, while navigatin, which I could bind an autocommand to? Also, does it only work after opening a file? – guido Feb 9 '15 at 14:42
  • @guido Not that I know of, but the other two answers seem like a possibility for automatic changing – Izkata Feb 9 '15 at 16:35
7

I set the following two options to ensure that Vim's current working directory is always the same as the current buffer's.

set autochdir                   " Changes the cwd to the directory of the current
                                " buffer whenever you switch buffers.
set browsedir=current           " Make the file browser always open the current
                                " directory.
  • The second command does it; the first will change directory also when opening a file with vim path/to/file which I don't want. – guido Feb 12 '15 at 22:21
  • @guido I don't understand how setting only the browsedir setting affects the behaviour you're describing. That setting affects what directory the GUI file browser opens, not what directory is used for command-line completion. – Rich Feb 24 '15 at 10:13
  • @Rich you perfectly right; I forgot to delete let g:netrw_keepdir=0 from my vimrc before testing this! – guido Feb 24 '15 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.