2

Suppose I have a path like /path/to/file/ in the buffer. I'd like to put point over that path and invoke a command that will either open it if it exists or create it if it doesn't.

Edit 1 Thanks to @Drew for pointing out that /path/to/file/ ends with a slash. I meant /path/to/file which is a file, not a directory.

Also, in my case M-x ffap on /tmp/non-existing-file will wait for user input at the minibuffer with the following prompt Find file or URL: /tmp/

enter image description here

Edit 2

Here's what I tried

(defun ffap-create-file ()
  "find or create file at point"
  (interactive)
  (setq filename (ffap-string-at-point))
  (ffap filename))

This code works, but it doesn't prompt. The "usual" behavior of ffap is to prompt the user with the filename already populated in the minibuffer.

1 Answer 1

1

Just M-x ffap does what you want, no? If so, just bind it to some key:

(global-set-key `C-o` #'ffap)

That puts you in a buffer for the new file (or new directory, if, as you show, you use a final slash). Just do what you want in that buffer, then save it, if it's for a file. Or if it's for a directory then (as the message tells you when you create the buffer), use M-x make-directory RET RET to create the directory.

This command will take care of creating the directory as well:

(defun my-ffap (file/dir)
  "..."
  (interactive (progn (require 'ffap) (list (ffap-prompter))))
  (when (directory-name-p file/dir) (make-directory file/dir t))
  (find-file file/dir))
7
  • In my case M-x ffap on /tmp/non-existing-file will wait for user input at the minibuffer with the following prompt Find file or URL: /tmp/
    – ychaouche
    Commented Apr 26, 2022 at 9:11
  • Did you try my-ffap? For me, it fills the minibuffer with the file name the cursor is on.
    – Drew
    Commented Apr 26, 2022 at 15:51
  • same with my-ffap, no automatic entry in the minibuffer. See imgur.com/a/rsunUdL. It could be that my emacs version is too old (24.3.1)
    – ychaouche
    Commented Apr 27, 2022 at 10:21
  • same if I use code of ffap-prompter from emacs github mirror
    – ychaouche
    Commented Apr 27, 2022 at 10:57
  • Here's what I tried gist.github.com/ychaouche/0428f902f00dd3c30ec1d74e5ac76c9c. It works, except it doesn't prompt.
    – ychaouche
    Commented Apr 27, 2022 at 11:05

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.