Directly open remote shell with tramp in emacs

I find that the best to run my shells in remote machines in emacs is using TRAMP (using the ssh command in a local shell does not work for my needs). However, I only know how to run a TRAMP shell if I first visit a remote file with C-x C-f and then do M-x shell from that buffer. However, I would like to open a shell in a remote machine, using TRAMP, before opening any file on that remote host, but cannot find any explanation on how to do that on my searches. Is it possible to do that?


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


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/")