0

I am editing a file on a remote server via Tramp, and I decide that I want to open up an ansi-term session on the remote server (as opposed to relying on shell-command). How do I do this? When I run ansi-term, it just opens up a new session on my local computer.

  • ansi-term is 'just' a terminal emulator, running on the local Emacs instance. Are you asking how to connect to a remote shell process (inside an ansi-term buffer) ? – phils Mar 16 '17 at 21:26
  • 3
    Possible duplicate of Honor default-directory in ansi-term – phils Mar 18 '17 at 11:49
  • You are right, phils. And what is shocking to myself: I answered there with the gnome-term solution and totally forgot about it.... so much about getting older. – dfeich Mar 18 '17 at 20:22
1

You can use the following function

(defun dfeich/ansi-terminal (&optional path name)
  "Opens an ansi terminal at PATH. If no PATH is given, it uses
the value of `default-directory'. PATH may be a tramp remote path.
The ansi-term buffer is named based on `name' "
  (interactive) 
  (unless path (setq path default-directory))
  (unless name (setq name "ansi-term"))
  (ansi-term "/bin/bash" name)
  (let ((path (replace-regexp-in-string "^file:" "" path))
    (cd-str 
     "fn=%s; if test ! -d $fn; then fn=$(dirname $fn); fi; cd $fn;")
    (bufname (concat "*" name "*" )))
    (if (tramp-tramp-file-p path)
    (let ((tstruct (tramp-dissect-file-name path)))
      (cond 
       ((equal (tramp-file-name-method tstruct) "ssh")
        (process-send-string bufname (format
                      (concat  "ssh -t %s '"
                           cd-str
                           "exec bash'; exec bash; clear\n")
                      (tramp-file-name-host tstruct)
                      (tramp-file-name-localname tstruct))))
       (t (error "not implemented for method %s"
             (tramp-file-name-method tstruct)))))
      (process-send-string bufname (format (concat cd-str " exec bash;clear\n")
                       path)))))

This code is also linked this gist. I have a similar gist for opening gnome-terminals at the current path (or any given path) here.

With the ansi terminal there may just some small but non-critical problem with the remote server not knowing the 'eterm-color' type. E.g. look at this stackoverflow question.

  • If you were keen, you could integrate emacs.stackexchange.com/q/18672 with your solution here, in order to directly run the ssh command in the terminal, as opposed to starting a local shell and then running commands in the local shell. – phils Mar 16 '17 at 22:02

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.