44

I tried to open a remote file via Emacs via Tramp.

(require 'tramp)
(setq tramp-default-method "ssh")

I get a message from Emacs

Tramp: Waiting for prompts from remote shell

Emacs hung and did not respond to any action after that

Emacs was installed on Windows; the remote file was on a Linux machine.

phils
  • 71,335
  • 11
  • 153
  • 198
jerboa
  • 1,371
  • 2
  • 12
  • 18

6 Answers6

71

If the account you're connecting to uses some weird fancy shell prompt, then there is a good chance that this is what makes tramp trip.

Log in as root, then enter

PS1="> "

(that's a normal, standard shell (ZSH, BASH, younameit) prompt, one that tramp will understand) then switch to the user account, and launch emacs -q (to make sure that your .emacs is not causing this mess) and try to C-x C-f /sudo:root@localhost:/etc/hosts and see what's what.

You can (not recommended) also customize the regexp that defines what tramp expects :

M-x customize-variable RET tramp-terminal-prompt-regexp

My approach :

  1. Make sure the variable tramp-terminal-type is set to "dumb"

M-x customize-variable RET tramp-terminal-type

  1. Test that in your .*shrc and serve the correct prompt :
case "$TERM" in
"dumb")
    PS1="> "
    ;;
xterm*|rxvt*|eterm*|screen*)
    PS1="my fancy multi-line \n prompt > "
    ;;
*)
    PS1="> "
    ;;
esac
yPhil
  • 8,049
  • 4
  • 57
  • 83
  • 5
    Thank you! This must be the source of many people's problems and they just don't realize it. Fixed my Tramp bug which I thought was a lost cause. – emish Mar 05 '12 at 17:22
  • 3
    It is worth noting that you can customize the prompt that tramp expects (M-x customize-variable "tramp-login-prompt-regexp" but my advice would rather be to make your prompt tramp-compatible (https://github.com/xaccrocheur/kituu/blob/master/.bashrc) and not the other way around – yPhil Jun 01 '12 at 19:29
  • Can I somehow check what prompt TRAMP sees and tries to match against? I'm a bit unsure what shell that is run and what configuration files that are read. – Nordlöw Oct 23 '12 at 12:15
  • @Nordlow: Yes, `M-x customize-variable "tramp-terminal-prompt-regexp"` – yPhil Dec 17 '12 at 14:24
  • 4
    I want all this to be automated. Prompt pattern should be detected automatically for the remote host. How do we achieve this? – Nordlöw Dec 17 '12 at 22:12
  • We don't. At the remote end, the prompt can be anything, really. The user could have setup a strange 3 lines non-UTF8 oddity. I hear what you say, Tramp should be able to parse *anything* and arbitrarily state "this is the remote prompt" but AFAIK he can't. – yPhil Dec 18 '12 at 08:33
  • 1
    This works for me, but the tests using `C-x C-f /sudo:root@localhost:/etc/hosts` did not work and returned a different error about `sudo can only use the local host` (I would have to configure multi-hop or something). If you altered the `PS1` variable, I recommend trying out the "My approach" section directly, and save the sudo configuration for another day. – modulitos Jun 11 '14 at 01:46
  • If you're still having issues after fixing the .*shrc file, try restarting Emacs. For some reason this was necessary for me - my guess is that tramp reused the connection it had initially opened, which still used the non-dumb prompt. – Michael Bosworth Oct 22 '14 at 16:16
  • 1
    Nobody mentioned the blocker I had: in case the shell is not interactive, the ~/.bashrc file returns immediately (by default in Gentoo). The "return" command should be removed. – avp Nov 19 '14 at 10:50
  • 1
    I wrote about it some time ago [here](https://www.bounga.org/tips/2017/11/30/fix-emacs-tramp-lag-and-timeout/) because I had a lot of trouble to figure out what was the problem – Bounga Sep 06 '21 at 07:37
18

Your Windows ssh client is the key here, and the 'ssh' Tramp method is almost certainly wrong.

If you're using Cygwin, then you need to use the 'sshx' method, and you probably need to use ssh-agent to handle authentication. Details are here: Using tramp with EmacsW32 and cygwin, possible?

I imagine the same applies to any stand-alone ssh client which does not require a full Cygwin installation, but does use the Cygwin DLLs. (I mention this, because I'm pretty sure I remember seeing such a thing.)

If you're using PuTTY then you want the 'plink' method, as Alex Ott pointed out. If the Wiki doesn't suffice, a search here will probably turn up solutions for configuring that approach.

Other alternatives I can suggest are:

  1. Use the Cygwin-native Emacs. That will be slower than NTEmacs, but Tramp seems to work well with the 'ssh' method, and password-prompting works as well.

  2. Host a Linux VM on your Windows box, and run Emacs on that. That's a fairly large hoop to jump through, but it's my preferred way of using Tramp when working in Windows.

Community
  • 1
  • 1
phils
  • 71,335
  • 11
  • 153
  • 198
4

Well, this is a defect of tramp.

The real solution is to prevent loading .bashrc when tramp is used. (because now it is PS1, but it can be PATH, or any other thing that your .bashrc will do that will displease tramp...).

This can be done by asking ssh to set an environment variable, and testing it in .bashrc:

Add this to ~/.emacs:

(require 'tramp-sh nil t) (setf tramp-ssh-controlmaster-options (concat "-o SendEnv TRAMP=yes " tramp-ssh-controlmaster-options))

and that at the beginning of ~/.bashrc:

if [ ! -z ${TRAMP-x} ] ; then return fi

Another default of tramp is that it doesn't have a variable to pass random arguments to the ssh command, we have to piggy-back on tramp-ssh-controlmaster-options.

informatimago
  • 147
  • 1
  • 4
  • I like the explanation here, but isn't this approach similar to that in the most upvoted answer, except you're sending a custom environmental variable and not loading bash (seems reasonable) whereas that answer has the shell check the TERM environmental variable and load a simple prompt in the case of Tramp (also reasonable)? Just checking my understanding before trying an approach. – Jesse W. Collins Nov 26 '19 at 17:33
3

If the problem is your fancy custom prompt in the remote shell, an easy workaround is to add to your .bashrc or equivalent:

if [[ $TERM == "dumb" ]]; then
    export PS1="$ "
fi

After you define your PS1.

Note: the credit goes to ChasingLogic as this is their suggestion in this thread.

prosoitos
  • 6,679
  • 5
  • 27
  • 41
  • 1
    Prior art (just for the sake of completeness): https://blog.karssen.org/2016/03/02/fixing-emacs-tramp-mode-when-using-zsh/ Original Emacs Wiki entry: https://www.emacswiki.org/emacs/TrampMode#h5o-9 – M-x Dec 14 '21 at 18:48
3

Had you checked Emacs wiki for solution? ssh is in PATH? It's also recommended to use plink on MS Windows - see section "Inline methods" in Tramp documentation

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • Alex: Thanks, but unfortunately the documentation lacks an example there. Here it is: C-x C-f /plink:user@host:/path/to/file – avp Nov 19 '14 at 10:35
0

By the way -- if You need tramp to sudo -- You can actually sudo without tramp using sudoedit.

Currently I'm using this bash function:

erf () { SUDO_EDITOR="emacsclient -a emacs" sudoedit $@; }
Community
  • 1
  • 1
Adobe
  • 12,967
  • 10
  • 85
  • 126