I had a problem with execution of (comint-simple-send process command). My previous example would not show the last output because I didn't use the hook and the result was being dispalyed before the command finished it's execution. Als I didn't know about having to restart the process to reflect the code changes.

My solution

The following code works.

(add-hook 'comint-output-filter-functions
          '(lambda (txt) (message (format  " output ----------- %c%s" 10 txt))))

(let* ((process-buffer (make-comint-in-buffer "StrangeTerm" "*strange-term*" "/bin/bash"))
       (p (get-buffer-process process-buffer)))
  (comint-simple-send p "ls -l /"))

The lambda has the expected output, which I can see in Messages and in future I can do what I want with it.

Please confirm if this is the correct approach.

  • Why do you use ansi-term for ls ~/Desktop? Can't you use directory-files or shell-command or async-shell-command? (The latter two if the ls ~/Desktop is just an example of what you want to do.)
    – Tobias
    Jan 13, 2017 at 17:36
  • it could be any command, like asking a programming language interpreter for the list of loaded modules. Jan 13, 2017 at 17:58
  • You can call arbitrary commands from elisp without sending them to an ansi-term process. Unless you need to run multiple commands in the same terminal, there is an easier way to get the output of any single command.
    – Tyler
    Jan 13, 2017 at 18:05
  • I know there are easier ways, but to understand comint-mode and talking to processes i need to do it this way. Jan 13, 2017 at 18:12
  • Note that term-mode is based on comint but it does not use it.
    – Tobias
    Jan 13, 2017 at 19:11

1 Answer 1


One crude non-robust way to get the last output in term-modeis:

(defun term-recommended-hook ()
  "Hook function recommended as `term-mode-hook'. (See file \"term.el\".)"
  (setq term-prompt-regexp "^[^#$%>\n]*[#$%>] *")
  (setq-local mouse-yank-at-point t)
  (setq-local transient-mark-mode nil)
  (auto-fill-mode -1)
  (setq tab-width 8 ))

(add-hook #'term-mode-hook #'term-recommended-hook)

(let ((p (get-buffer-process "*ansi-term*")))
  (comint-simple-send p "ls ~/Desktop")
  (with-current-buffer (get-buffer "*ansi-term*")
    (buffer-substring-no-properties (save-excursion (term-previous-prompt 1) (forward-line) (point)) (line-beginning-position))))
  • thank you, I get what I want except the fact that buffer substring returns only last line of output. I think I can manage from this point onwards. Jan 13, 2017 at 19:47
  • @ruby_object In that case you didn't probably run term-recommended-hook (the setting for term-prompt-regexp is most important). Note that after installing the hook you need to restart ansi-term.
    – Tobias
    Jan 13, 2017 at 19:49
  • killing the term process, as you said and your hook example has helped me to find the solution. Jan 13, 2017 at 20:17

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.