2

Often I want to create a temporary R file and don't want to save the file or name the file. Is there a way to achieve this outside of using C-x C-f and naming a file? For example, in VSCode or Rstudio I can just create a new file (with just a Command + N, but don't need to actually name them or save them to starting using R. Is this possible in Emacs? For example, can I create a keyboard shortcut that creates a temporary file with an arbitrary name (maybe R_date_timestamp.R) in some temporary directory or a similar solution?

2 Answers 2

3

You can create a buffer without an associated file.

(defun ess-r-mode-scratch () "Create or switch to a scratch buffer in ess-r-mode" (interactive) (switch-to-buffer (get-buffer-create "*ess-r-mode-scratch*")) (ess-r-mode)) 

You can bind it to your preferred key, for e C-c n

(global-set-key (kbd "C-c n") #'ess-r-mode-mode-scratch) 

Your question inspired me to create the following more general function. With this one buffer will have the same major mode of the buffer where the function is called

(defun this-mode-scratch () "Create or switch to a scratch buffer in the current major mode" (interactive) (let ((this-mode major-mode)) (switch-to-buffer (get-buffer-create (format "*%s-scratch*" this-mode))) (funcall this-mode))) 
3
  • Wow, this is fantastic! This is exactly what I'm looking for! How can I also make this specific to ess-r-mode, do I just need to change all references to this-mode? Commented Jun 1, 2023 at 13:43
  • If you want the created buffer to always be in ess-r-mode, yes, just directly put ess-r-mode instead of this-mode. If what you want is being able to call this-mode-scratch only in ess-r-mode, you can use (define-key ess-r-mode-map (kbd "C-c n") 'this-mode-scratch). Commented Jun 1, 2023 at 16:55
  • If you need always the same mode the function can be simpler, see edit Commented Jun 1, 2023 at 17:33
2

If you want your temporary file to always have the same name, you can use the following command :

(defun create-r-temp-file () "Create an empty temporary R file." (interactive) (find-file-noselect "/your/temp/R/file")) 

Which will create a temporary file and an associated buffer, but won't automatically switch to that buffer.

On Linux, you can place temporary files in /tmp, since you mentioned "Command + N" I believe you use macOS and I don't know if something similar exists. But you can still delete it yourself by adding the following :

(shell-command "rm -f /your/temp/R/file") 

to a hook run before exiting Emacs. Or by calling rm -f /your/temp/R/file in a script run before shutting down your machine.

2
  • This is really close, thanks! I have the following now (defun create-r-temp-file () "Create an empty temporary R file." (interactive) (find-file-noselect (concat "/r_temp/tmp_" (format-time-string "%Y%m%d_%s") ".R"))) How can I have this file get focus in a new buffer without needing to navigate to it? Commented May 31, 2023 at 17:46
  • Use find-file instead of find-file-noselect? Commented May 31, 2023 at 19:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.