After each reboot, I have to painfully re-create and rename the multi-term buffers I use in my daily routine.

I am completely new to Lisp, but nonetheless, I searched for similar problem on the web and tried to adapt the code snippets I've found. I managed to open a multi-term buffer, but renaming does not work as expected. Here is what I've tried so far in my init.el:

(require 'multi-term)
(setq multi-term-program "/bin/bash")
;; Try to recreate my terminals
(add-hook 'emacs-startup-hook 'multi-term)
(add-hook 'emacs-startup-hook 'get-buffer)
(rename-buffer "redshift")  ;; it renames the previous buffer (not the good one)

As a bonus, I would also like to be able to cd to a specific directory. Thanks for your advice.

PS: I work on GNU Emacs 23.4.1 (Debian stable).


Such initialization might (I guess) looks like the following:

(require 'multi-term)

(defun init-multi-term (new-name dir)
  (let ((mt-buffer-name "*terminal*"))
    (comint-send-string (get-buffer-process mt-buffer-name) (format "cd %s\n" dir))
    (with-current-buffer mt-buffer-name
      (rename-buffer new-name))))

(defun init-multi-terms ()
  (init-multi-term "Downloads" "/home/demi/Downloads/")
  (init-multi-term "Github" "/str/development/"))

(setq multi-term-program "/bin/bash")
(add-hook 'emacs-startup-hook 'init-multi-terms)

I just tested it and it works fine for me. cd works as well.

  • 1
    On my computer, I had to change mt-buffer-name "*terminal*" to mt-buffer-name "*terminal<1>*". And now it works flawlessly. Oct 14 '14 at 13:28
  • This is probably because buffer with the name "terminal" already existed. Oct 14 '14 at 13:38

Technical explanation: that's because emacs-startup-hook is run after the init file is processed, therefore your call to multi-term is run after you do the rename operation.

Now, you don't need emacs-startup-hook at all, what you really want is to run those things in sequence, right from your init file. And you don't need to call get-buffer (which needs a buffer argument anyway, so that'd never work), since multi-term will display the buffer it creates.

So, try something like:

(require 'multi-term)
(setq multi-term-program "/bin/bash")
(rename-buffer "redshift")
(rename-buffer "foo")

or better yet

(mapc (lambda (name)
        (rename-buffer name))
      '("redshift" "foo"))

You may want to look at Workgroups2, which is available via MELPA. Although I haven't tried it with Multi-Term (thanks for the pointer, I'd never heard of it!), I'm quite happy with it for saving buffer state between Emacs sessions.

If workgroups2 doesn't work for you, there are pointers to other packages that manage workspaces here.

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.