2

Say i create a new file using find-file (by typing a non-existing filename, since it create a newfile with said filename) How could i add the .org extensions at the end automatically?

I heard of hooks on emacs but i wasn't sure if this was the solution, so here i am.

To Clarify: I don't mind if every new files created have a .org extensions, as i center my workflow around org-mode like a lots of others do. Here find-file is just to be specific, but in general, i wouldn't mind if it happen even outside of using find-file.

5
  • Are you using C-xC-f or are you using your own command that makes use of find-file? If the latter, then you can use this in the interactive spec, to read the file name: (read-file-name "File: " nil (expand-file-name ".org") nil). That will insert the directory/.org, and put your cursor before the dot so you can type the file name.
    – Drew
    May 11 '20 at 22:19
  • Both :), I'm using the second code block on the accepted answer of my previous post May 11 '20 at 22:25
  • But i don't mind if every new files have the .org extensions, since i always end up using it, with or without using find-file to be honest. May 11 '20 at 22:26
  • mind showing an example more detailed using your aforementioned suggestion? just curious @Drew :) May 12 '20 at 4:55
  • (defun my-find-file (filename &optional wildcards) "..." (interactive (list (read-file-name "File: " nil (expand-file-name ".org") nil) t)) (find-file filename wildcards))
    – Drew
    May 12 '20 at 16:23
1

I'd do it this way:

(defun add-default-extension (retval)
  (let ((filename (car retval))) ; retval is (filename t)
    (if (or (file-exists-p filename)
            (file-name-extension filename))
        retval
      (message "Adding default .org extension")
      (list (concat filename ".org") t))))

(advice-add 'find-file-read-args :filter-return #'add-default-extension)

(Well, it would be more accurate to say "I wouldn't do it, but if I had to, I'd do it this way:" :P)


EDIT: The above answer only add .org to file names typed interactively at the find-file prompt, but I read in the comments to the question that you are actually not using find-file but this function instead:

(defun find-file-region()
  (interactive)
  (if (region-active-p)
    (let ((str (buffer-substring (region-beginning) (region-end))))
      (find-file str))
    (message "No region active")))

In that case, just add the extension there:

(defun find-file-region ()
  (interactive)
  (if (region-active-p)
      (let ((str (buffer-substring (region-beginning) (region-end))))
        (find-file (if (or (file-exists-p str)
                           (file-name-extension str))
                       str
                     (concat str ".org"))))
    (message "No region active")))
2
  • Oh, just because it leaves you no easy way of creating a new file without an extension, @Fólkvangr
    – Omar
    May 12 '20 at 16:23
  • Thanks, work great! @Omar and yeah, i think i wouldn't recommend (even myself) to do this, unless you always use the same extensions like me ( i always use org-mode everywhere anyway) @Fólkvangr May 12 '20 at 19:33

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.