Emacs shell mode open file in buffer

emacs, shell, ssh, terminal

Solution

It's best to use tramp.

I have this shortcut (I call it with `smex`):

(defun connect-remote ()
  (interactive)
  (dired "/user@domain.com:/"))

This opens a dired buffer on the remote. You just use it as any dired buffer.

I've had a function to open a term from dired for a while, but I've added an option to ssh from a tramp dired buffer just now:

(defun dired-open-term ()
  "Open an `ansi-term' that corresponds to current directory."
  (interactive)
  (let ((current-dir (dired-current-directory)))
    (term-send-string
     (terminal)
     (if (file-remote-p current-dir)
         (let ((v (tramp-dissect-file-name current-dir t)))
           (format "ssh %s@%s\n"
                   (aref v 1) (aref v 2)))
       (format "cd '%s'\n" current-dir)))))

(defun terminal ()
  "Switch to terminal. Launch if nonexistent."
  (interactive)
  (if (get-buffer "*terminal*")
      (switch-to-buffer "*terminal*")
    (term "/bin/bash")))

And this is the shortcut that I use:

(define-key dired-mode-map (kbd "`") 'dired-open-term)

Problem

My setup: - Emacs terminal mode (`emacs -nw`) - inside it, use the shell mode (invoked with M-x `ansi-term`) - inside this shell, connect to a remote server with ssh Suppose I'm browsing the remote server inside the shell and find a file I want to edit. Is there a command to open it as a parallel buffer/window? The only method I know to open a file from the shell is to do `emacs -nw` again, which isn't quite convenient because a) I don't keep the shell open and b) it's really a different Emacs session, so for instance the "yank buffer" is different. Edit: if there is a different/better way to work with a remote server with Emacs I'm just as interested; it's what I'm trying to do.

Original source