Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open a temporary buffer

Tags:

emacs

For very long time I have done: C-x b and then some "unique" name like xbxb. So I use switch-to-buffer with a non-existent buffer. You can imagine what C-x C-b shows me: Lots of such names. xbxb, xbxbxxx .... It really gets annoying after some time (a week or so), since I discover that I have already used all good names.

Is there a more canonical way of opening a new buffer? If I want to run a shell a further time, I say C-u M-x shell. Something along that line would be ideal.

like image 720
false Avatar asked Apr 28 '12 13:04

false


6 Answers

You can use make-temp-name to generate a name for a file or buffer with a random postfix. With that as a base, you can write something like this:

(defun generate-buffer ()
  (interactive)
  (switch-to-buffer (make-temp-name "scratch")))

where "scratch" can be replaced by whatever prefix you'd like.

like image 132
R. P. Dillon Avatar answered Sep 30 '22 17:09

R. P. Dillon


make it so:

(defun new-scratch ()
  "open up a guaranteed new scratch buffer"
  (interactive)
  (switch-to-buffer (loop for num from 0
                          for name = (format "blah-%03i" num)
                          while (get-buffer name)
                          finally return name)))
like image 25
event_jr Avatar answered Sep 30 '22 17:09

event_jr


I signed up just to answer this question (because I use this function a lot, so I thought it'd be useful to share it here):

(defun tmpbuf (buf)
  "open a buffer,
if it doesn't exist, open a new one"
  (interactive "sBuffer name: ")
  (switch-to-buffer
   (get-buffer-create (concat "*" buf "*"))))
like image 25
dotemacs Avatar answered Sep 30 '22 18:09

dotemacs


I'm not really sure what you want. You say that "I discover that I have already used all good names", so letting Emacs generate the names isn't going to be any good, but if you are going to specify the name yourself, it doesn't get any more canonical than C-xb name RET.

Otherwise, one of the functions already suggested to let you enter a string and use that with some kind of "tmp buffer" pattern to create a new name would seem sensible.

Or scratch.el might prove useful, if what you actually wanted was a single temp buffer per major mode.

You could almost certainly benefit from binding C-xC-b to ibuffer, and using filters and/or groups to separate out the temporary buffers from the more important ones. That would deal with the list getting cluttered.

You seem oddly resistant to writing a new function? Even if there did turn out to be something built in, there's nothing wrong with using custom functions -- that's generally how you go about customising Emacs to your liking.

like image 24
phils Avatar answered Sep 30 '22 16:09

phils


(defun yashi/new-scratch-buffer-in-org-mode ()
  (interactive)
  (switch-to-buffer (generate-new-buffer-name "*temp*"))
  (org-mode))
(bind-key "<f7>" 'yashi/new-scratch-buffer-in-org-mode)

I'm using deft for a quick note-taking but sometimes I know I won't need the content but need a buffer in Org mode. For that, it's been serving me well. Hit F7 again will create a buffer with similar name, *temp*<2> in my case, acording to Uniquify.

BTW, here is a command to launch a new buffer with a new deft file, if you are interested. F6 to launch it.

(defun yashi/deft-new-file ()
  (interactive)
  (let ((deft-filter-regexp nil))
    (deft-new-file)))
(bind-key "<f6>" 'yashi/deft-new-file)
like image 31
Yasushi Shoji Avatar answered Sep 30 '22 16:09

Yasushi Shoji


I use this to open up a temporary buffer. Good thing? Helps me track which buffers I'd opened, and when.

(defun tmp-buffer()
      "Make a temporary buffer and switch to it - Like C-n for Sublime etc"
  (interactive)
  (switch-to-buffer (get-buffer-create (concat "tmp-" (format-time-string "%m.%dT%H.%M.%S")))))
like image 29
Chandravadan S Avatar answered Sep 30 '22 17:09

Chandravadan S