Using elisp, I call this function (dired "~"), for example, to get to my Home directory in dired mode. When I press RET on one of the files, I want to capture the file path and store it in a variable instead of opening the file.

In other words, if the function dired did actually return the file name of the selected file using RET, I want to do something like this:

(setq file-name (dired "~"))

Where file-name would be the full path of the file that the user selected and pressed RET on.

Any ideas?


Just a little clarification of what I wanted to achieve:

  1. Find the function that will get me the file name from point in a Dired buffer (The answer to that is what @phils said).

  2. Bind RET to store the file name in a variable (The answer to that was provided by @Drew in the comments of @phils answer).


2 Answers 2


I imagine the function dired-get-filename is what you're looking for?

Or perhaps dired-get-file-for-visit would be more useful (being a wrapper which handles some common errors).

  • Thanks @phills for the quick reply, I've tried something like this: (defun test () (interactive) (dired "~") (setq x (dired-get-filename))) and it seems to work, the variable x does capture the selected filename. However, there's one problem, I have to open the file by pressing RET to get the filename. Is there a way to prevent dired from opening the selected file when pressing RET? I might be using the dired-get-filename function in a wrong way, so maybe you could provide a little code snippet? :D Thanks again. Aug 20, 2017 at 23:52
  • Based on your comment it is not clear what you are really trying to do. @phils's answer tells you how to set a variable's value to the name of the file at point. Now you are talking about having to press RET to open the file and asking how to not open the file by pressing RET (?). Why are you hitting RET? Please consider clarifying your original question a bit.
    – Drew
    Aug 21, 2017 at 0:59
  • Are you perhaps asking how to change the key binding of RET in Dired to a command that just puts the file name at point into a variable?
    – Drew
    Aug 21, 2017 at 1:01
  • Hello @Drew, yes, your last comment is what I want to achieve. When I hit RET I want the file at point to be stored in a variable. Shall I modify the original question? Aug 21, 2017 at 1:13
  • Yes, please. The answer then is probably to drop the (dired "~") from your command test and bind test to RET in dired-mode-map: (define-key dired-mode-map (kbd "RET") 'test). Once a user is in a Dired buffer, RET will then copy the file name to variable x. Not sure that is a great idea, though.
    – Drew
    Aug 21, 2017 at 1:51

This is an older post but I was recently looking for something similar and stumbled upon this. My goal was: "use dired to retrieve a file's name". As mentioned in this post's Accepted Answer's comments, dired doesn't seem to be made for this.

I'm new to elisp so there might be plenty of better ways than my approach demonstrates, but I hope that it is helpful for anyone trying to do something similar.

My solution was to use completing-read and directory-files to get something close to this behaviour:

  (defun find-in-directory (path)
    "Find file in directory."
    (let ((filename
           (completing-read "File: " (directory-files-recursively path "\\.txt$"))))

completing-read provides the auto-complete functionality using directory-files (here, directory-files-recursively) as the source (i.e., what can be selected or auto-completed on). I've also included a "\\.txt$" argument to limit the extensions of the returned files, demonstratively, though it isn't neccessary per OP's use-case.

  • One could always use functions like mapcar to transform the file names for the auto-complete (e.g., to remove the directory portion of the string) - just be sure that however it is returned is the format that is required.
    – Keegan G
    Oct 22, 2020 at 22:51
  • 1
    Why not use read-file-name?
    – Stefan
    Oct 23, 2020 at 14:12
  • @Stefan Fair point! read-file-name will do auto-complete on files in the current directory, but none of the nested files; it isn't recursive. If you're looking for nested files, you'll have to navigate. For my use-case, I wanted to auto-complete on the file names without having to navigate folders. So, "user experience", let's say. :)
    – Keegan G
    Oct 24, 2020 at 19:38

Your Answer

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

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