reddit / emacs
Multiple Scratch Buffers
Phaedrus_Schmaedrus · 2022-03-17 17:20
I want to have the ability to spawn new scratch buffers in Emacs as desired. It looks like there are a few packages that do that, but before I start diving into each one I wanted to check with the community and see if there's an obvious winner/any packages that I shouldn't waste my time with.
EDIT: Wow, yeah, I really hadn't considered just creating and not saving a buffer. Chalk that up to me making things more complicated than they need to be. Thanks all.
crawsphi · 2022-03-17 17:27
In the simplest case, you can just do `C-x b` (`switch-to-buffer`) and enter a name, like "scratch2". That will create a new buffer if the name you choose doesn't exist.
If there are other specific things you're looking for, please expand.
onetom · 2022-03-17 17:28
what packages have you found for this purpose?
if u just C-x b and type a non-existent buffer name, like scratch-2, then enter, is that not sufficient?
jwiegley · 2022-03-17 18:09
I feel like `itch.el` would be the perfect name for something that needs to be scratched multiple times. :)
ieure · 2022-03-17 18:13
I wrote [scratch](https://melpa.org/#/scratch) a bajillion years ago, but still use it every day.
I use it a lot when sharing code snippets, since it puts the scratch buffer into whatever major mode you need to make the code look nice.
doolio_ · 2022-03-17 18:15
I like [ieure/scratch-el](https://github.com/ieure/scratch-el) which allows one to create a scratch buffer of the current major mode or of any other.
mrolivertaylor · 2022-03-17 18:15
I wrote my own solution to this: https://www.olivertaylor.net/emacs/custom-scratch-buffers.html
doolio_ · 2022-03-17 18:17
Snap, I was obviously still typing my reply when you replied. Yes, great little package. Thanks for it.
awalker4 · 2022-03-17 19:05
To add to this - if you just want a hotkey to make a throwaway buffer you can call something like this:
(defun generate-scratch-buffer ()
"Create and switch to a temporary scratch buffer with a random
name."
(interactive)
(switch-to-buffer (make-temp-name "scratch")))
onearmedphil · 2022-03-17 19:07
I personally prefer to C-x b !a. All my scratch buffers start with ! and I’ve mentally reserved ! for this. So when I went to open another it’s just C-x b !b
[deleted] · 2022-03-17 19:10
[deleted]
AuroraDraco · 2022-03-17 19:35
I would activate lisp-interaction-mode in this function as well to make it more like the scratch
eev200 · 2022-03-17 20:03
I have written a function that does that.
[https://gist.github.com/eev2/52edbfdb645e26aefec19226c0ca7ad0](https://gist.github.com/eev2/52edbfdb645e26aefec19226c0ca7ad0)
If you mark some text before you call the function, the text will be copied in the newly created scratch buffer. In addition, the scratch buffer will inherit the mode associated with the buffer when the function was called from (can be disabled with C-u).
arthurno1 · 2022-03-17 20:23
And if you need it to be in some other major mode than fundamental, say org or python, just M-x desired-mode-name, after you have created new buffer.
It is also trivial to encapsulate it in a small interactive function for say some scratch buffer for most often used mode, for example:
(defun scratch-org ()
(interactive)
(switch-to-buffer (generate-new-buffer-name "*scratch-org*"))
(org-mode))
Emacs does not offer to save that buffer by default, which might be what you want or not.
00-11 · 2022-03-17 20:33
['n sniff](https://en.wikipedia.org/wiki/Scratch_and_sniff)
whompyjaw · 2022-03-17 20:36
Just curious, what do you typically use your scratch/itch buffers for?
arthurno1 · 2022-03-17 20:44
> if you put a file extension on the buffer name, the appropriate mode will automatically activate.
No it won't, it will be in fundamental mode. Mode enables only on buffers with associated files. Try it yourself.
You will have to enable a desired mode with M-x your-mode. That is exactly why I wrote it the comment I posted before you commented :-).
jwiegley · 2022-03-17 20:44
I use *scratch* multiple times a day, primarily for performing surgery on text that I want to copy&paste to someplace outside of Emacs.
[deleted] · 2022-03-17 20:52
[deleted]
arthurno1 · 2022-03-17 20:58
> (switch-to-buffer (make-temp-name "scratch"))
I would rather use:
(switch-to-buffer (generate-new-buffer-name "scratch"))
So you get names like :
scratch
scratch<2>
scratch<3>
Instead of something like
scratchJjISjn
TokenMenses · 2022-03-17 21:25
You are correct. I deleted the comment.
arthurno1 · 2022-03-17 21:35
It is also good to exam some lengthy lists or other stuff which might got shortened in *Messages* buffer. I would typically do something like
(insert (pp (buffer-local-variables)))
and then move cursor a line below and C-x C-e. Maybe there is a better way to examine big collections, but this way I can search for stuff, replace, cut, paste etc.
16rlut3Br · 2022-03-17 23:07
If you name them with a starting asterisk '*', you won’t be prompted to safe on exit; if they have a normal name, they will prevent you from quitting before you chose their fate.
karthink · 2022-03-18 03:28
Thanks for writing it, been using it every day for years.
whompyjaw · 2022-03-18 14:55
Thanks so much for adding!
Opening-Training-972 · 2022-03-19 04:44
Thanks. I use your `scratch` package every day.
By the way, package `cl` is deprecated in Emacs 27, can you update it to use `cl-lib` instead?
ieure · 2022-03-19 15:17
Yes, thanks for the suggestion.