1

I often open temp files and I don't care about saving the contents when I modify the buffer. How can I tell Emacs to ignore buffer changes when the filename matches a pattern, so that I don't get prompted to save the buffer when I kill it?

Edit: I am loading files from the command line, so I'd like to auto-detect this situation and configure the buffer accordingly.

jdigital
  • 125
  • 5

1 Answers1

1

I use scratch buffers for that purpose.

(defun python-scratch () 
  (interactive) 
  (let ((python-scratch-buffer (get-buffer-create "*python-scratch*")))
    (switch-to-buffer python-scratch-buffer)
    (python-mode)))

You can convert the buffer with the file to a scratch buffer.

(defun my-detach-buffer-from-file ()
  (interactive)
  (setq buffer-file-name nil))
bertfred
  • 1,699
  • 1
  • 11
  • 23
  • I am loading the file from the command line. How can I automatically have the file load into a scratch buffer? – jdigital Oct 26 '16 at 18:44
  • How can this be done automatically, when the file is loaded? I don't want to have to run a command. – jdigital Oct 26 '16 at 19:21
  • How do you load your files ? From a command line in emacs ? And how do you want to auto detect this situation ? You have to distinguish between loading a file and loading its contents in a scratch buffer. – bertfred Oct 26 '16 at 19:33
  • I am loading from the command line (cmd.exe). I can specify a regex and when that matches, I'd like to have the buffer set to the equivalent of scratch mode. – jdigital Oct 26 '16 at 19:42
  • In a new instance of emacs or an already exisiting ? Btw that's a pretty exotic usage of emacs :D – bertfred Oct 26 '16 at 19:48
  • Already existing instance. I have a single instance of emacs; from the command line, I can specify a file and have it open in that instance. But it shouldn't matter how the file is loaded. I'm now thinking I could do this with a tmp-file-mode. – jdigital Oct 26 '16 at 19:54
  • I've created a tmp-mode (copied from fundamental-mode) which sets the buffer-file-name to nil, as you mentioned in your answer. I added .tmp to the auto-mode-list and this works for me. I'll give you credit for the answer. Feel free to update your response with this extra information so that it's easy for find. – jdigital Oct 26 '16 at 19:58
  • I wonder if there's a way to do this without deleting the filename? Every now and then it could be useful to edit tmp files. I will work around this for now by using an unusual file extension. – jdigital Oct 26 '16 at 20:02
  • Just out of curiosity, why not load your files from within emacs if you already have a running instance, that you want to use ? Nothing is faster than emacs in terms of finding files on your filesystem. – bertfred Oct 26 '16 at 20:19
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/47483/discussion-between-jdigital-and-bertfred). – jdigital Oct 26 '16 at 20:27