reddit / emacs

How do I find a file but then open the folder it is located in.

jcdix · 2022-04-09 02:18
Sorry for the dumb question, but I am trying to replace ranger with dired and can't duplicate getting to a folder via searching for a file. In ranger I would use locate to find a file and then press enter and it would open the folder of the file with the file I searched for highlighted. In dired I use helm-locate, which finds the file, but I can't figure out how to just choose to open the location, not the file itself. Is there a way to do that?
7890yuiop · 2022-04-09 02:34
If you're in dired.... why don't you just select the directory you want to open instead of selecting a file?
jcdix · 2022-04-09 02:50
The issue is that many times I don't know what directory the file is in (I have thousands of directories and files), so I start by searching for the file, using some word I remember, like the name of the author if it is a book. Anyway, I just discovered that I can locate the file, open it, and then press Ctrl+x d to open the underlying directory. I prefer ranger's way of opening the location with the file highlighted, but this works.
shryzr · 2022-04-09 03:12
Also look at https://emacsredux.com/blog/2013/09/24/dired-jump/
00-11 · 2022-04-09 03:25
`M-x dired-jump`. I bind it to `C-x C-j`.
jcdix · 2022-04-09 03:38
That's just what I was looking for. Thanks!
7890yuiop · 2022-04-09 04:01
So this, then? (with-eval-after-load "dired" (define-key dired-mode-map (kbd "C-d") #'my-dired-find-file-parent-directory)) (defun my-dired-find-file-parent-directory () "In Dired, visit the parent directory of the file or directory on this line." ;; Derived from `dired-find-file' (see which). (interactive) (let ((find-file-run-dired t) (switch-to-buffer-preserve-window-point (if dired-auto-revert-buffer nil switch-to-buffer-preserve-window-point))) (find-file (file-name-directory (dired-get-file-for-visit)))))
cazzipropri · 2022-04-09 07:39
If you are going through helm, it's the "." entry at the top.
grimscythe_ · 2022-04-09 07:59
I was looking for a solution for the exact same inquiry as you have at some stage. I use `M-K` as in Alt+Shift+k to open dired in `helm-find-files-map` and `helm-projectile-find-file-map`: ``` (general-define-key :keymaps '(helm-find-files-map helm-projectile-find-file-map) "M-K" '(lambda () (interactive) (helm-select-nth-action 1))) ``` And for `helm-locate` the `nth-action` is different, it's number 4, not 1. so the code is as follows: ``` (general-define-key :keymaps '(helm-locate-map) "M-K" '(lambda () (interactive) (helm-select-nth-action 4))) ``` If you're using something else than `general` for your keybindings then you'll have to lookup how to bind keys to a specific map, I simply don't remember the default way of doing that. EDIT: Oh, when you're in `helm` you can press `M-i` to see all the available actions, just if you're curious.
oantolin · 2022-04-09 08:45
I think that's the default binding now.
00-11 · 2022-04-09 18:11
Define "now". ;-) It's not bound by default in Emacs 27.2 or prior, at least. Dunno about 28.
oantolin · 2022-04-09 18:28
"Now" means the most recent Emacs release, so 28.1. :) From NEWS.28: *** 'dired-jump' and 'dired-jump-other-window' moved from 'dired-x' to 'dired'. The 'dired-jump' and 'dired-jump-other-window' commands have been moved from the 'dired-x' package to 'dired'. The user option 'dired-bind-jump' no longer has any effect and is now obsolete. The commands are now bound to 'C-x C-j' and 'C-x 4 C-j' by default.
jcdix · 2022-04-10 16:28
Thanks for this. I will have to teach myself some emacs lisp before I can understand. But I appreciate your help. I will copy and paste this into an org note for future review!
jcdix · 2022-04-10 16:30
Thanks a lot! I will have to do a little studying before I can understand. But I appreciate your technical know how.
grimscythe_ · 2022-04-10 17:13
I was in a hurry writing this. Helm offers a few interfaces for looking for files, like the helm-find-file, helm-locate, etc. When you see the list of files and you highlight over any of them, you can launch dired directly from the highlighted file (which is what I think you're looking for). If you're stuck, let me know. I'll take a peek how to do the keybindings in the default Emacs way without using the General package.
7890yuiop · 2022-04-10 22:25
In which case I'll add that the sole difference between this and the normal `dired-find-file` command is the call to `file-name-directory` at the end: Instead of `(find-file (dired-get-file-for-visit))` which is what normally happens, we instead do `(find-file (file-name-directory (dired-get-file-for-visit)))` to visit the *directory* of that thing. So that's pretty simple in practice. I can't tell if you've actually tried it, but if not then you should do so -- I believe it's what you want. Everything else was pure copy/paste from `dired-find-file`, but FYI the latter comes with explanatory comments: (defun dired-find-file () "In Dired, visit the file or directory named on this line." (interactive) ;; Bind `find-file-run-dired' so that the command works on directories ;; too, independent of the user's setting. (let ((find-file-run-dired t) ;; This binding prevents problems with preserving point in ;; windows displaying Dired buffers, because reverting a Dired ;; buffer empties it, which changes the places where the ;; markers used by switch-to-buffer-preserve-window-point ;; point. (switch-to-buffer-preserve-window-point (if dired-auto-revert-buffer nil switch-to-buffer-preserve-window-point))) (find-file (dired-get-file-for-visit))))