I am pretty new to Emacs. Could someone please help me to write a custom command which I can add to my init file? It would do the following:

  1. Split the screen vertically to get two windows.
  2. Check if a buffer named '* ansi-term *' (without spaces) exists. If so, it would open this buffer in the one of vertically split windows.
  3. If a buffer with such a name doesn't exist it would run command ansi-term (ignoring all prompting) to start a new terminal instance in one of the vertically windows.
  • This is not clear: buffer named '* ansi-term *' (without spaces). Please clarify. (Also, please say what you've tried so far and what kind of "help" you need.)
    – Drew
    Commented Jan 20, 2019 at 19:03
  • I'm guessing the OP added spaces to avoid some effect of the markup, but as far as I can tell the name of the buffer is *ansi-term* - indeed, without spaces.
    – NickD
    Commented Jan 21, 2019 at 4:56
  • Thank you, guys. Solutiion from xuchunyang works great!
    – Ilja Leiko
    Commented Jan 21, 2019 at 7:47
  • If you're interested in learning elisp, I highly recommend reading "An Introduction to Programming Emacs Lisp". You may already have it. Type C-h i and look for Emacs Lisp Intro. Otherwise, you can read it online. It's a super fun read! gnu.org/software/emacs/manual/html_node/eintr/index.html Commented Jan 21, 2019 at 15:21

1 Answer 1


Here's a command which might do what you want:

(defun my-ansi-term ()
  (select-window (split-window-right))
  (let ((buffer (get-buffer "*ansi-term*")))
    (if buffer
        (switch-to-buffer buffer)
      (ansi-term shell-file-name))))

If you're not happy with the window layout, try to figure it out by yourself, try switch-to-buffer-other-window etc.

  • Awesome, thank you! Works just as expected! Such a time saver!
    – Ilja Leiko
    Commented Jan 21, 2019 at 7:47

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.