12

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.

5
  • What about using the current directory of the shell?
    – choroba
    Commented Apr 26, 2017 at 7:54
  • 5
    I would normally call C-u M-x shell - in this way Emacs offers to name the buffer it creates. But if I forget, there's always the M-x rename-buffer. Or are you asking for an advise on what that name should be?
    – wvxvw
    Commented Apr 26, 2017 at 8:59
  • 1
    @wvxvw: Please consider posting that as an answer.
    – Drew
    Commented Apr 26, 2017 at 14:03
  • 1
    FWIW, I bind rename-buffer to a key (and I use it fairly often).
    – Drew
    Commented Apr 26, 2017 at 14:04
  • @Drew I'm not sure if OP wants the technical "how to rename shell buffer" answer, or "what should I name shell buffer to make it unique / meaningfull" kind of answer (eg. Emacs will append <...> 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.
    – wvxvw
    Commented Apr 26, 2017 at 15:15

3 Answers 3

14

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.

1

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.

0

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.

3
  • I suppose (add-hook 'shell-mode-hook 'rename-uniquely) would work too.
    – jagrg
    Commented May 20, 2020 at 19:10
  • Wouldn't the (interactive) line already handle the (call-interactively...) part, or do these do something different?
    – DLAN
    Commented 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 _)...).
    – jagrg
    Commented May 23, 2020 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.