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.

  • What about using the current directory of the shell? – choroba Apr 26 '17 at 7:54
  • 4
    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 Apr 26 '17 at 8:59
  • 1
    @wvxvw: Please consider posting that as an answer. – Drew Apr 26 '17 at 14:03
  • 1
    FWIW, I bind rename-buffer to a key (and I use it fairly often). – Drew Apr 26 '17 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 Apr 26 '17 at 15:15

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.

| improve this answer | |

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.

| improve this answer | |

Add a function to your init.el, and create a keybinding:

(defun unique-shell ()
  (call-interactively 'shell)

(global-set-key "\C-z" 'unique-shell)

Now every time you press ctrl+z, it will launch a shell with a new name.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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