11

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.

m33lky
  • 297
  • 3
  • 9
  • What about using the current directory of the shell? – choroba Apr 26 '17 at 07: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 Apr 26 '17 at 08: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

3 Answers3

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.

user2739472
  • 241
  • 2
  • 4
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.

azzamsa
  • 634
  • 7
  • 16
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.

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