The Computer Oracle

Directly open remote shell with tramp in emacs

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping

--

Chapters
00:00 Directly Open Remote Shell With Tramp In Emacs
00:36 Accepted Answer Score 12
01:00 Answer 2 Score 3
01:30 Answer 3 Score 1
01:58 Answer 4 Score 1
02:09 Thank you

--

Full question
https://superuser.com/questions/841178/d...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#emacs #tramp

#avk47



ACCEPTED ANSWER

Score 12


Tramp comes into play, when the default-directory is remote. So you might change it, as you do with opening a remote file (or directory) in advance.

But you could write a small command like this:

(defun my-shell () (interactive) (let ((default-directory "/ssh:user@host:")) (shell)))

Then you can call M-x my-shell




ANSWER 2

Score 3


I use dired to access the remote machine and open a shell there.

Here is the function I use, taken and modified from Tikhon Jelviss' emacs configuration:

(defun anr-shell (buffer)
  "Opens a new shell buffer where the given buffer is located."
  (interactive "sBuffer: ")
  (pop-to-buffer (concat "*" buffer "*"))
  (unless (eq major-mode 'shell-mode)
    (dired buffer)
    (shell buffer)
    (sleep-for 0 200)
    (delete-region (point-min) (point-max))
    (comint-simple-send (get-buffer-process (current-buffer)) 
                        (concat "export PS1=\"\033[33m" buffer "\033[0m:\033[35m\\W\033[0m>\""))))

Example:

(anr-shell "/vagrant@localhost#2222:/vagrant/")



ANSWER 3

Score 1


Although not made overly clear in the manual C-u M-x shell allows you to specify the name of the shell buffer, and more importantly in your case allows you to specify the Default directory, which can take a string just like Tramp /ssh:server:/my/path.

An alternative is to open dired (C-x d) rather than an actual file, if you M-x shell from directory, the shell opens with the context of the dired directory.




ANSWER 4

Score 1


Here's another approach:

  1. M-x cd to change the buffer's default directory to the desired remote directory
  2. invoke M-x shell, which will open a shell on the remote machine