I'm trying to follow this article and configure my multi-term to work with remote files seamlessly. Edit: That article seems to be down currently. Try the cached version here.

But it's not working as expected. I'm running OS X 10.9.5 and GNU Emacs 24.4.1.

As explained in the above article, I have placed the following code in my .emacs.d/init.el:

(when (require 'term nil t)
  (defun term-handle-ansi-terminal-messages (message)
    (while (string-match "\eAnSiT.+\n" message)
      ;; Extract the command code and the argument.
      (let* ((start (match-beginning 0))
             (command-code (aref message (+ start 6)))
                (substring message
                           (+ start 8)
                           (string-match "\r?\n" message
                                         (+ start 8))))))
        ;; Delete this command from MESSAGE.
        (setq message (replace-match "" t t message))

        (cond ((= command-code ?c)
               (setq term-ansi-at-dir argument))
              ((= command-code ?h)
               (setq term-ansi-at-host argument))
              ((= command-code ?u)
               (setq term-ansi-at-user argument))
              ((= command-code ?e)
                 (find-file-other-window argument)))
              ((= command-code ?x)
                 (find-file argument))))))

    (when (and term-ansi-at-host term-ansi-at-dir term-ansi-at-user)
      (setq buffer-file-name
            (format "%s@%s:%s" term-ansi-at-user term-ansi-at-host term-ansi-at-dir))
      (set-buffer-modified-p nil)
      (setq default-directory (if (string= term-ansi-at-host (system-name))
                                  (concatenate 'string term-ansi-at-dir "/")
                                (format "/%s@%s:%s/" term-ansi-at-user term-ansi-at-host term-ansi-at-dir))))

And I have placed the following code in my .bashrc:

# are we an interactive shell?
if [ "$PS1" ]; then
  case $TERM in
      if [ -n "$SSH_CONNECTION" ]
        echo "if"
        _HOST=$(echo -n $SSH_CONNECTION | cut -d\  -f3)

      echo -ne "${PWD/#$HOME/~}\n"
      PROMPT_COMMAND='echo -ne "\033AnSiTh ${_HOST}\n\033AnSiTu ${USER}\n\033AnSiTc ${PWD/#$HOME/~}\n"'
      PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
      PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\033\\"'
      [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default

if [ "${TERM}x" = "eterm-colorx" ]
  alias e='echo -ne "\033AnSiTe"'
  alias x='echo -ne "\033AnSiTx"'
  echo "eterm-colorx"
  alias e='emacsclient -n -t -a nano'

After adding these, I see 2 issues.

  1. When I open a multi-term buffer, I see the current working directory being printed before each prompt. I also see a box (some unprintable character) and / preceding the path.

    prompt with the PWD printed each time

    I don't know where this string is coming from.

  2. Also when I try to open with command x samp.txt it opens a file with some terminal messages attached at the end of filename. The file name it tries to open is samp.txt^[AnSiTh xyz.

    xyz (renamed here) is the hostname of the system. It looks like the terminal message for setting the hostname somehow got appended to the filename. I couldn't figure out who/which code is appending this.

  • Did you ever find out what's causing the first problem? It has to do something with term.el, not bash, because if I ssh into my own box, everything is working as expected.
    – Stormking
    Commented Apr 20, 2016 at 9:01

3 Answers 3


I ran into the same problem and this seems to be something to do with bash. Switching the configuration around to zsh has solved the problem.

I ran into the issue on an ubuntu install with bash 4.3. Compiling a local version of bash 4.1 didn't help either. On a RHEL install with bash 4.1.2 the shell/emacs interaction work well.

  • 1
    I would also suggest switching to zsh. I've been using oh-my-zsh on my OS X system and since doing so, a number of irritating things have vanished. I also found building emacs from homebrew much better than installing the pre-build OS X version binaries.
    – Tim X
    Commented Feb 21, 2015 at 9:50

multi-term already support this feature so I don't think you need to add anything to your init.el file.

And according to emacswiki (http://www.emacswiki.org/emacs/AnsiTermHints#toc5), you just need to add this to your .bash_profile:

function set-eterm-dir {
    echo -e "\033AnSiTu" "$LOGNAME" # $LOGNAME is more portable than using whoami.
    echo -e "\033AnSiTc" "$(pwd)"
    if [ $(uname) = "SunOS" ]; then
    # The -f option does something else on SunOS and is not needed anyway.
    echo -e "\033AnSiTh" "$(hostname $hostname_options)" # Using the -f option can
                                                         # cause problems on some OSes.
    history -a # Write history to disk.

# Track directory, username, and cwd for remote logons.
if [ "$TERM" = "eterm-color" ]; then
  • Even with the above code in my .bashrc, I see the exact same problem I show in my issue 1. The command prompt shows a separate line with a box followed by a slash and then the PWD.
    – chitti
    Commented Feb 12, 2015 at 23:32

With emacs 25, changed the function in your .bashrc with this one:

if [ "$TERM" = "eterm-color" ]; then
   function eterm-set-cwd {
    echo -e "\033AnSiTc" $(pwd)
   # set hostname, user, and cwd
   function eterm-reset {
      echo -e "\033AnSiTu" $(whoami)
      echo -e "\033AnSiTc" $(pwd)
      echo -e "\033AnSiTh" $(hostname)
   for temp in cd pushd popd; do
      alias $temp="eterm-set-cwd $temp"
   # set hostname, user, and cwd now

source: https://gist.github.com/mkhattab/1739578

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.