I'm interested in naming Emacs buffers. In particular I'd like to give names to shell
buffers that I've started. That way I can easily tell them apart when switching buffers.
3 Answers
Go to the buffer you want to rename, in your case *shell*
. Then type M-x rename-buffer
and enter the new name for this buffer.
If you use Helm
. Just press M-R in helm buffer list.
It calls helm-buffer-run-rename-buffer which will call emacs rename-buffer
function.
Add a function to your init.el, and create a keybinding:
(defun unique-shell ()
(interactive)
(call-interactively 'shell)
(rename-uniquely))
(global-set-key "\C-z" 'unique-shell)
Now every time you press ctrl+z, it will launch a shell with a new name.
-
I suppose
(add-hook 'shell-mode-hook 'rename-uniquely)
would work too.– jagrgCommented May 20, 2020 at 19:10 -
Wouldn't the (interactive) line already handle the (call-interactively...) part, or do these do something different?– DLANCommented May 22, 2020 at 17:59
-
You need the interactive call to use the optional argument (e.g.
C-u M-x unique-shell
). I was also getting an error from(advice-add 'shell :after (lambda (_)...)
, but I see the argument is optional so I probably should've used(advice-add 'shell :after (lambda (&optional _)...)
.– jagrgCommented May 23, 2020 at 18:37
C-u M-x shell
- in this way Emacs offers to name the buffer it creates. But if I forget, there's always theM-x rename-buffer
. Or are you asking for an advise on what that name should be?rename-buffer
to a key (and I use it fairly often).<...>
to buffer names if both are visiting a file with the same name based on some quite complex heuristic). In the later case, I don't quite have a good answer, but one possibility would be to update the name of the buffer using some meaningful part of the shell command ran in that buffer.