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.
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.
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)))
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 "*"))))
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.
(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)
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")))))
You are given access to someone else's Emacs and you want to open a new buffer. If you are lucky, this someone has set enable-recursive-minibuffers to t. If not, you can temporarily do M-: to eval this Emacs Lisp expression (don't forget to restore this parameter later):
(setq enable-recursive-minibuffers t)
Now, you open a new buffer:
C-xb prompts for a buffer name
C-uM-: will eval and print the result of the expression to the minibuffer prompt:
(random)
This gives you a random number in the full range of representable integers (including negative ones), which is very unlikely to be already taken. Press Return to conclude to switch to the new buffer.
I was looking for a solution for which I have the freedom to give a name or it randomly generates a name. I merged the andswer of #dotemacs and the answer of R. P. Dillon. I'm a n00b in elisp so it took me some half an hour to resolve some of my stupid mistakes.
(defun mm/generate-temp-buffer (buf)
"A function to generate temprory buffers using either
a random name or given name"
(interactive "sNew temp buffer name: ")
(switch-to-buffer
(get-buffer-create
(concat "*tmp*"
(if (equal buf "")
(make-temp-name "")
buf)
"*")
)))
This is how it works:
you run the function (M-x mm/generate-temp-buffer)
you are asked for a name.
2.1. if you type something it will create a buffer named *tmp*YOUR_STRING*
2.2. if you don't give an input and just press RET, it will create a buffer named something like *tmp*o8f4Mf*
Hopefully this is useful to others and save someone's time.
Related
This is a very simple request: I want to create a new, blank file without giving it a name (yet). I can use the scratch buffer but there's only one. I can C-x C-f and open a new file, but then I have to give it a name and path. If I'm just writing notes to myself or sketching out ideas, I don't want to have to give it a name. How do I create a new, empty, unnamed file?
You can create a new buffer with: C-xb and type in the buffer name and it will create a new buffer with a name that you choose.
If you want to save that buffer, just hit C-x w to create the file with its contents to a desired location.
My recommendation is that you give org-mode a try. It will do what want among a million other things.
Give it a name. Just don't save it.
Not what you wanted to hear, but this is the Emacs way.
Use C-x C-f, giving the (to-be-file-visiting) buffer a name. Edit the text. Do not use C-x C-s to save the buffer to the file (i.e., to disk).
Note that you can first put yourself in a directory whose contents you don't care about, so that if you accidentally do save the buffer there then you can easily find, recognize and toss the file. To change directories, you can use M-x cd. Or just do it by editing the directory part when you first use C-x C-f. Or use C-x d to put yourself in a Dired buffer for the directory.
If you don't want to take the chance of accidentally hitting C-x C-s and thus saving your edits, then use C-x b instead of C-x C-f. You are (even here) prompted for the buffer name. Giving it a new name (not the name of an existing buffer) creates a new buffer. In this case, if you use C-x C-s then Emacs prompts you for the file location to save the buffer in.
Why would you want to use C-x C-f instead of C-x b, if you might not want to save the buffer? Providing a file extension in the file name you give automatically puts the buffer in the proper major mode (typically). Otherwise (for C-x b) you need to put the buffer in the mode you want.
Buffer *scratch* is by default in Lisp-Interaction mode, which is similar to Emacs-Lisp mode (but not the same).
In every other text editor or word processor the intuition is to create a “new file” or a “new buffer”, not to switch to idiosyncratic *scratch* buffer. For example, you write quick notes or thoughts in several different buffers to keep trace of them—later you decide if you throw them away or save them. Or you manipulate a snippet of text or code, but you don't want to change the original buffer, so you just copypaste it to a new temporary buffer.
*scratch* is set to Lisp Interaction mode, but if I want to quickly evaluate some Elisp code, I could eval it running eval-expression (Alt+:) or in Elisp interpreter IELM (Alt+x Enter ielm). Also, if you close *scratch* buffer, it doesn't ask you to save it, so you can accidentally lose all your work. Drew's traditional solution seems too sub-optimal. And I don't buy the argument that “this is how you do it in Emacs”. Emacs is a customizable editor, so you can and should create whatever workflow is comfortable for you.
That's how ErgoEmacs solves it, buy creating a new-empty-buffer command. You can implemented like this:
(defun new-empty-buffer ()
"Opens a new empty buffer."
(interactive)
(switch-to-buffer (generate-new-buffer "untitled"))
(funcall initial-major-mode)
(put 'buffer-offer-save 'permanent-local t)
(setq buffer-offer-save t))
The variable buffer-offer-save resets every time you change a major mode, therefore you need to annotate it with permanent-local. It also prompts only when you exit Emacs. I think it is intuitive for it to also ask, when you close a modified untitled buffer, therefore see my solution on how to upgrade kill-buffer to prompt before closing a modified buffer.
ErgoEmacs revamps the default keybindings completely and has new-empty-buffer bound to Ctrl+N, like in almost all software. Change variable initial-major-mode if you want the new buffer to have another mode on start.
See also:
Emacs: Problems of the Scratch Buffer # Xah Lee
Emacs: New Empty Buffer # Xah Lee
You can try the following snippet, just add it to your .emacs
(defun new-file-tmp()
"Create a new empty file."
(interactive)
(let ((buf (generate-new-buffer "untitled")))
(switch-to-buffer buf)
(put 'buffer-offer-save 'permanent-local t)
(setq buffer-offer-save t)))
(defun tool-bar-local-item-pre (icon def key map after_item &rest props)
"Add an item to the tool bar in map MAP.
ICON names the image, DEF is the key definition and KEY is a symbol
for the fake function key in the menu keymap. Remaining arguments
PROPS are additional items to add to the menu item specification. See
Info node ‘(elisp)Tool Bar’. The item is added after AFTER_ITEM.
ICON is the base name of a file containing the image to use. The
function will first try to use low-color/ICON.xpm if ‘display-color-cells’
is less or equal to 256, then ICON.xpm, then ICON.pbm, and finally
ICON.xbm, using ‘find-image’."
(let* ((image-exp (tool-bar--image-expression icon)))
(define-key-after map (vector key)
`(menu-item ,(symbol-name key) ,def :image ,image-exp ,#props) after_item)
(force-mode-line-update)))
(when (boundp 'tool-bar-map)
(tool-bar-local-item-pre "new" 'new-file-tmp 'new-file-tmp tool-bar-map
'new-file :label "" :help "New untitled File")
(define-key tool-bar-map (vector 'new-file) nil)
;; comment the above line if you want to keep the button for the default behavior
)
(global-set-key (kbd "C-n") 'new-file-tmp)
It defines a new command which creates new empty buffers
Then the code binds it to the top left button of the toolbar, if you are in gui mode, and to the Control-n shortcut.
You can check the post on my site about it.
disclaimer: it's my site
A buffer and a file are not the same thing.
terminology
Regarding buffers,
The text you are editing in Emacs resides in an object called a
buffer. Each time you visit a file, a buffer is used to hold the file's text.
Regarding files,
The operating system stores data permanently in named files, so most
of the text you edit with Emacs comes from a file and is ultimately
stored in a file.
Buffers and files are related through visiting,
Visiting a file means reading its contents into an Emacs buffer so you
can edit them. Emacs makes a new buffer for each file that you visit.
answering the question
Unless I'm mistaken, technically speaking, your question, as written, can't be answered. Pedantically speaking, there's no such thing as (or little practical use for) an unnamed file. A file is a handle for something stored on disk. If you have no handle, then why make a file?
The question can then be interpreted as having two possible meanings1.
1. Making a new buffer (without regard to name)
A new buffer must have a name. An unsaved buffer can be saved to file with a given name using write-file (C-x C-w). You will be prompted for a path/name. Once written, the buffer update to be visiting the file you just wrote.
Since the name of the buffer doesn't matter (until you write it to file), here's a function which creates buffers named *scratch1*, *scratch2*, ... .
(defun create-scratch-buffer ()
"Create a new numbered scratch buffer.
Taken from URL `https://stackoverflow.com/a/21058075/5065796' "
(interactive)
(let ((n 0)
bufname)
(while (progn
(setq bufname (concat "*scratch"
(if (= n 0) "" (int-to-string n))
"*"))
(setq n (1+ n))
(get-buffer bufname)))
(switch-to-buffer (get-buffer-create bufname))
(org-mode)
(if (= n 1) initial-major-mode)))
2. Making a new file (without regard to name)
As hinted at in the new buffer solution, new files can be created with write-file.
When called interactively (M-x make-random-file), this function prompts for a directory. It then writes an empty file named something random like tmp-17388716387615.txt.
(defun make-random-file (dir)
"Make a random file in DIR."
(interactive "DDir: ")
(let* ((filename (concat "tmp-" (int-to-string (random t)) ".txt"))
(filepath (concat dir filename)))
(write-region "" nil filepath nil 'silent nil 'excl)))
As always, if the code above doesn't make sense, look at the documentation for functions and variables with C-h f and C-h v, respectively. And/or read the Introduction to Programming in Emacs Lisp.
1 It was the second meaning which I was searching for solutions to when I found this as the top search engine hit. Apologies if this was a bit obtuse. Figured it was better to share my solutions with y'all than not. :)
My Emacs is on OS X system. Is there any way to make a new frame defaulted to an empty buffer whenever I use ⌘N (just like the way TextEdit works)? I prefer to write contents first and decide an appropriate filename later. However, Emacs wants me to decide the filename first and write contents later. I don't see any advantage for it. Does anyone know why Emacs works that way?
Basically, if I use C-x 5 2, Emacs always pops up a frame with whatever file I am currently working on. This is inconvenient. I also don't want my Emacs to pop up a new frame defaulted to *scratch* (many Google search results somehow suggest this approach). I prefer it to have a buffer temporarily called "Untitled" in the new frame, and if I use ⌘N again, Emacs pops up another temporarily "Untitled 2" buffer, and so on. In this way, I can decide the buffer filenames later.
You can create new buffers with switch-to-buffer. Type C-x b, enter a buffer name, and press RET. If no buffer with that name exists, Emacs creates a new one automatically in Fundamental Mode. You may switch to any other mode as usual with M-x, e.g. M-x python-mode. To change the default buffer, set the default value of major-mode to the desired buffer.
If you'd like to have a buffer name chosen automatically, and create a new frame, however, you need to write your own command:
(defun lunaryorn-new-buffer-frame ()
"Create a new frame with a new empty buffer."
(interactive)
(let ((buffer (generate-new-buffer "untitled")))
(set-buffer-major-mode buffer)
(display-buffer buffer '(display-buffer-pop-up-frame . nil))))
Bind this to C-c n:
(global-set-key (kbd "C-c n") #'lunaryorn-new-buffer-frame)
Now pressing C-c n creates a new frame with a new empty buffer named “untitled” where x is a consecutive number.
The following will create a buffer with a unique name. The buffer is not associated with any file, so if/when you ever C-x C-s save-buffer, you will be prompted to supply a filename.
(defun empty-frame ()
"Open a new frame with a buffer named Untitled<N>.
The buffer is not associated with a file."
(interactive)
(switch-to-buffer-other-frame (generate-new-buffer "Untitled")))
This might work for you, if I understand your request:
(defun empty-frame ()
(interactive)
(let ((fl (make-temp-file "Untitled")))
(switch-to-buffer-other-frame fl)))
This will open a new temporary file for each new buffer. If you'd rather not actually create the file, you can use this instead:
(defun empty-frame ()
(interactive)
(let ((bn "Untitled-")
(num 1))
(while
(get-buffer (concat bn (number-to-string num)))
(setq num (1+ num)))
(switch-to-buffer-other-frame
(concat bn (number-to-string num)))))
You've seen answers as to how to create new "Untitled" buffers, but as for why Emacs wants you to first choose a name, some of the reasons are:
Historical: that's just how it worked, and once you get used to it, it's no worse than the alternative.
Major modes and various other details are usually chosen based on the name of the file. So instead of creating an Untitled buffer and having to choose whether to put it into LaTeX mode or C mode, you just open a file with extension ".tex" or ".c".
Having a file name means that Emacs can use the standard auto-save procedure, whereas with the Untitled approach, applications need to have some special way to auto-save those Untitled documents at some "standard" place.
C-x b *untitled* will open new buffer if not exist. Also, see Emacs manual
The problem:
I'm in scheme-mode (simple quack)
scheme is already running
eval expression
it sends the expression to scheme buffer
but! doesn't bring that buffer up in 2nd window = no immediate feedback
and I have to manually switch second buffer to scheme which is annoying
Some (more recent) modes like fsharp-mode or tuareg do that automatically. Tried to read quack.el, but didn't find convenient separate function like "pop scheme". It's tangled within run-scheme which also changes focus. Settings also don't help.
I want to stay in my rkt/scm file and see repl buffer pop up if not already popped. Like this simple build logic in sublime-text but with persistent repl.
Maybe I should try geiser, but quack is ok for now. Just missing few obvious conveniences.
Just rungeiser. It's in MELPA now, so it's a quick install.
You can also try lispy (which uses geiser) for in-place scheme eval.
e will eval current expression and display the result in the minibuffer.
E will eval current expression and insert the result in current buffer.
u is bound to undo, so you can either e or Eu if you prefer.
I ended up writing this:
(setq scheme-program-name "guile")
(defun run-scheme-2.0 ()
"Loads your chosen Scheme implementation for interactive development,
and displays that buffer below your main editing buffer, and makes sure that
your cursor will be on your code."
(interactive)
(if (not (get-buffer "*scheme*"))
(progn
(setq starting-buffer (buffer-name))
(run-scheme scheme-program-name)
(split-window-below)
(other-window 1)
(shrink-window-if-larger-than-buffer)
(other-window 1)
(switch-to-buffer starting-buffer))))
(add-hook 'scheme-mode-hook 'run-scheme-2.0)
Sometimes I edit files that have a certain suffix and files with same basename but another suffix. Since the basenames are long and awkward I would like to reuse the existing filename to construct the other name.
What I do currently is:
C-x C-b (This shows me the Buffer List)
C-x o (Now I am in the Buffer List)
M-right M-left (At the beginning of the basename)
C-space M-right ... M-w (Copying the basename)
C-x o (Back where I came from)
...
There must be a more canonical way to do this! (I am not interested in a way that involves the definition of a command proper, I have too many of them already...)
Edit: actually, what I wanted is to do something with that very name, most often create or read a related file. #Trey Jackson's C-x C-v is it!
Strictly speaking this command does what you want, even if you don't want a command:
(defun copy-base-filename-as-kill ()
(interactive)
(let ((filename (buffer-file-name)))
(when filename
(kill-new (file-name-nondirectory (file-name-sans-extension filename))))))
But, if you're really just using this name to do something else... perhaps a better solution is to make doing that something else easier.
Try the command C-x C-v (aka M-x find-alternate-file) if what you want to do is create a new name based on the current name.
I don't think there's anything out of the box that does it, but here's a little elisp function:
(defun copy-buffer-filename ()
(when (buffer-file-name)
(kill-new (file-name-nondirectory (buffer-file-name)))))
I would like to translate the following function from vim script to emacs elisp (I use it to set the email recipients when writing emails).
My question is mainly how to get and set line contents in emacs, because with quick googling I could not find this out (probably I just did not know the right terms to google for, "getline" and "setline" in any case did show any results).
function! G_set_to()
let address = system('my-address-script') "shell command to choose addresses
let line = getline (2)
if strlen(line) == 4
call setline(2, line . address)
else
call setline(2, line . "; " . address)
endif
endfunction
Sorry if the answer is obvious, I am am completely new to emacs and don't even know how to use its in-built help system.
Cheers
Arian
Give this a shot, obviously customizing the variable G-address-script to suit your needs.
(require 'sendmail)
(defvar G-address-script "echo hi#google.com")
(defun G-set-to ()
"execute script in G-address-script and populate the To: line with the results"
(interactive)
(save-excursion
(mail-to)
(unless (= (current-column) 4)
(insert "; "))
(insert (shell-command-to-string G-address-script))
(when (looking-at "^$") ; when shell command has extra newline, delete it
(delete-backward-char 1))))
As far as using the help, there's a reasonable introduction to Emacs lisp here. But the quick intro is:
C-h i (that's control-h then i to see the info pages, there is one for Emacs and one for Emacs Lisp.
M-x apropos searches for terms
C-h f gives you help on a function
C-h v gives you help on a variable
In the *scratch* buffer, C-j will evaluate the expression right before the point (cursor)
M-x edebug-defun when the point is in a emacs lisp function will turn the debugger on, and the next time a function is run you'll step through it (M-x eval-defun in the function will turn the debugger off)
M-x eldoc-mode will turn on automatic documentation for emacs lisp functions and variables as you type them, read more here.
I took Trey's code and changed a few things and now it seems to work:
(defvar G-address-script "goobook_dmenu_with_cache")
(defun G-set-to ()
"execute script in G-address-script and populate the To: line with the results"
(interactive)
(save-excursion
(goto-line 2)
(re-search-forward "$")
(unless (= (current-column) 4)
(insert "; "))
(insert (shell-command-to-string G-address-script))))
Some remaining questions:
The bit where Trey's code checks if the string returned from G-address-script is empty doesn't work at the moment, what do I have to change?
Is there really no function to get the content of a specified line as a string?
Is there a better/more elegant way to do this? The vim script solution looks so much simpler, surely I am missing something.
Anyway, I'll mark this as solved in a day or so if there are no more answers as Trey solution basically does what I wanted. Thank you Trey!