Directly open remote shell with tramp in emacs

10,185

Solution 1

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

Solution 2

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/")
Share:
10,185

Related videos on Youtube

jmlorenzi
Author by

jmlorenzi

Updated on September 18, 2022

Comments

  • jmlorenzi
    jmlorenzi over 1 year

    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?

    • lawlist
      lawlist over 9 years
      Please add some additional explanation of what it is your are seeking. Clearly, you are more advanced that just wanting to know how to ssh into a remote machine from a shell buffer, but that is the direction your question leads the reader. So, do you want to have a function open a shell buffer and automatically ssh into your remote machine -- so that you are at a prompt and path on the remote server? Or, do you want a shell mode buffer to already be open, and then just hit an auto-login command? Do you use sshpass? Would you prefer to enter your password each time?
    • jmlorenzi
      jmlorenzi over 9 years
      Thanks @lawlist ! I reworded the question in hopes that it is more clear. To address your questions: I want some way that lets me make tramp open a remote shell buffer in a machine I specify. The expected behavior is that emacs asks for my password when I try to do this.
  • Jarmund
    Jarmund about 9 years
    Please repost the answer here, as links to the actual answer is discouraged to avoid link rot
  • mandark
    mandark about 9 years
    The answer has been reposted.
  • Jarmund
    Jarmund about 9 years
    Vote changed accordingly.
  • kwarnke
    kwarnke almost 6 years
    If emacs runs on windows and you want to login onto a linux host, you could set with M-x customize-variable explicit-shell-file-name to \bin\bash to avoid missing shell errors.
  • Terry
    Terry over 4 years
    @Michael Albinus I've applied that my-shell function. It successfully builds a TRAMP connection from my Windows 7 laptop to my Mac computer but after that it exits with env: C:/msys64/usr/bin/bash.exe: No such file or directory exit I think it's because in my .emacs tramp-encoding-shell is set to C:/msys64/usr/bin/bash.exe If only I was able to change the value tramp-encoding-shell to /bin/bash I think it would work out fine. How to do it without hassle? Thanks.
  • Michael Albinus
    Michael Albinus over 4 years
    As always, you could bind it. Like ... (let ((default-directory "/ssh:user@host:")(tramp-encoding-shell "/bin/bash")) (shell))). I haven't tested this, 'tho.