Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs shell mode open file in buffer

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.

like image 793
user2460978 Avatar asked Apr 03 '26 00:04

user2460978


1 Answers

It's best to use tramp.

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

(defun connect-remote ()
  (interactive)
  (dired "/[email protected]:/"))

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)
like image 85
abo-abo Avatar answered Apr 08 '26 14:04

abo-abo