I use dired to browse a directory and its sub-directories with i
(dired-maybe-insert-subdir
)
When the cursor is in a sub-directory, and I do C-x C-f
, the default directory is the root of my buffer, is there a way to change that?
So if I visit /foo/bar and then I insert in bar's buffer the content of /foo/bar/baz and have my cursor there, get a mini-buffer with /foo/bar/baz/
when I ask to visit a file (with the default keybinding or another, that doesn't matter to me).
The following is my own solution.
(defun dired-subdir-aware (orig-fun &rest args)
(if (eq major-mode 'dired-mode)
(let ((default-directory (dired-current-directory)))
(apply orig-fun args))
(apply orig-fun args)))
(advice-add 'find-file-read-args :around 'dired-subdir-aware)
I just learned how to use the advice function to augment existing functions (and macros). See (elisp) Advising Functions.
Now if you go to a subdir under dired and run C-x C-f
, you will be prompted with a correct path.
Update:
Recently, I began to play with (ido) Top. With dired-subdir-aware
defined, I easily extend ido-find-file
to recognize subdir with the following code.
(dolist (fun '(find-file-read-args ido-expand-directory))
(advice-add fun :around 'dired-subdir-aware))