简体   繁体   中英

Emacs shell mode open file in buffer

My setup:

  • Emacs terminal mode ( emacs -nw )
  • inside it, use the shell mode (invoked with Mx 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.

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)

You could use tramp from within your original emacs session to browse the remote server via ssh, using dired. Then, any remote file you open is opened in your local emacs session.

If you prefer to avoid dired and want to browse with a shell, you can prepend the remote location (/name@host:/path/from/pwd) to the file name. You can automate that with a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM