0

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)))
             (argument
              (save-match-data
                (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)
               (save-excursion
                 (find-file-other-window argument)))
              ((= command-code ?x)
               (save-excursion
                 (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))))
    message))

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

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

      echo -ne "${PWD/#$HOME/~}\n"
      PROMPT_COMMAND='echo -ne "\033AnSiTh ${_HOST}\n\033AnSiTu ${USER}\n\033AnSiTc ${PWD/#$HOME/~}\n"'
      ;;
    xterm*)
      PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
      ;;
    screen)
      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
      ;;
  esac
fi

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

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 Apr 20 '16 at 9:01
0

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 Feb 21 '15 at 9:50
0

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.
        hostname_options="";
    else
        hostname_options="-f";
    fi
    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
    PROMPT_COMMAND=set-eterm-dir
fi
  • 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 Feb 12 '15 at 23:32
0

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"
   done
   # set hostname, user, and cwd now
   eterm-reset
fi

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

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.