8
votes

I have some trouble setting up Tramp with EmacsW32 and cygwin. I have configured emacs to use cygwin as shell using w32shell. I also set the HOME enviromental variable to c:/cygwin/home/myusername

Problem is that tramp seems to hang and that no connection is made:

"Tramp waiting for prompts for the new shell".

I have tried to turn on debugging, but still only see this message. Looking forward to get some tips on this. Thank you.

4
have you tried c:\cygwin with the slash the other way?Mica

4 Answers

6
votes

Take note of the cygwin-related information on the emacs wiki: http://www.emacswiki.org/emacs/TrampMode

I don't use EmacsW32, but I do successfully use TRAMP over ssh with Cygwin and NT Emacs.

I never got TRAMP working without an ssh agent (i.e. prompting for credentials) -- as you noticed, it just hangs -- but it works fine with one, so I didn't spend time trying to resolve that. Assuming you're also happy to use an agent (and you have already generated your keys and added authorized_keys files as necessary), the approach that works for me is:

  1. Run ssh-agent from cygwin.
  2. Launch NT Emacs via cygwin (so that it inherits the ssh-agent environment variables).
  3. Use 'sshx' as the TRAMP method (you can specify it manually in each file path, but I recommend making it the default, with (setq tramp-default-method "sshx")).

Those points are all covered at the Wiki, but you can also automate things somewhat:

For step 1, my bash profile automatically starts an ssh agent if one isn't already running, or prompts me for my passphrase if my identity has expired. (See code below.)

For step 2, the target of my Windows shortcut for launching emacs looks like this:

C:\cygwin\bin\bash.exe --login -c "env HOME=\"`cygpath '%APPDATA%'`\" /cygdrive/c/emacs/emacs-23.1/bin/runemacs.exe"

The --login argument means my bash profile is executed, which ensures that step 1 has always been taken care of before emacs is started.

(Setting HOME isn't necessary for TRAMP, but %APPDATA% is the default under NT Emacs, and this prevents the Cygwin home directory from taking precedence, therefore keeping your emacs config consistent regardless of whether you use this shortcut to run it.)

Finally, here's the code from my cygwin .bash_profile that manages my ssh-agent. It's a bit hacky, but it works for me.

Do note that I expire my identity after 4 hours (14400 seconds). TRAMP can hang after that happens (as mentioned before, I never got it to prompt me for credentials), so be aware of this issue. Type C-g to stop it from trying to connect. To resume connectivity, you can generally just start up another cygwin shell, re-enter your passphrase, and then TRAMP will be happy again. Occasionally it has remained unresponsive, but killing the *tramp* buffer will sort that out. Not expiring your identity should circumvent this issue, of course, should that be acceptable.

SSH_ENV="${HOME}/.ssh/environment"

# Run ssh-agent, if one is not already running
function start_agent {
    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent -t 14400 | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" >/dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" >/dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ >/dev/null || {
        start_agent;
    }
    #if our ssh-added identity has expired (see -t option to ssh-agent)
    #then we need to re-add it
    if ! /usr/bin/ssh-add -l >/dev/null; then
        /usr/bin/ssh-add;
    fi
else
    #no ssh-agent running at the moment
    start_agent;
fi
7
votes

As a foot note, I've since changed the way I use TRAMP under Windows.

TRAMP under Windows was simply too slow. The Control Master feature of OpenSSH was what I needed -- this keeps a persistent SSH connection open, therefore eliminating the costly connection that is otherwise required for every TRAMP operation. Unfortuntely, Cygwin is fundamentally incapable of supporting Control Master at this point in time :/

This being the case, my current solution is not to run Emacs in Windows at all. Instead I installed Cygwin's X.Org server, and started running Emacs on a GNU/Linux VM (hosted on my Windows box), and using the Cygwin X display.

This works really really well.

I'm running emacs in daemon mode, which means it doesn't matter if there are ever any issues with the X server, and if I need to shut down the Windows box, I can simply save the state of the VM, restore it afterwards, and not actually lose my session(*). (desktop.el provides pretty decent session management in any case, or if you need to reboot the VM; but if you're using that, you may wish to modify the desktop-files-not-to-save variable so that it doesn't ignore all of your TRAMP buffers! I just set it to "^$")

Be sure to start Cygwin's X server with startxwin rather than startx (the former enables the X server's windows to appear as regular Windows windows), and then from an xterm you can execute ssh -Y (me)@(linux) -f "emacs --daemon" to start the server initially, and ssh -Y (me)@(linux) -f "emacsclient -c" to start a new client.

The 'scpc' and 'rsyncc' TRAMP methods use Control Master automatically. I still run a ssh-agent on the Linux box, though (to avoid the need to authenticate when opening remote shells from emacs, etc). The 'emacs --daemon' command then becomes something like ssh -Y (me)@(linux) -f "source ~/.ssh/agent-environment && emacs --daemon", presuming that you directed/copied ssh-agent output to that file when you ran it (I don't bother with an identity time-out for this one).

And finally, on that last note, if you are using a similar mechanism to the one I originally mentioned for managing your cygwin ssh-agent in your bash_profile, take careful note of what happens when you start the X server from the Windows shortcut when you do not have an authenticated identity loaded -- nothing at all (until you look in the task manager, and see the ssh-add process sitting there). Consequently, I recommend always using startxwin from the command line, to avoid this.

edit:
(*) That was a bad assumption. I've since realised that restarting the X server actually does prevent me from reconnecting to the emacs daemon afterwards, which is rather unfortunate. Consequently I rely fully upon the desktop library to restore my session. I use auto-save-hook to regularly save my desktop file to keep it up to date. See the Emacs Wiki for this and other options for enhancing the Desktop feature.

4
votes

I used to use the EmacsW32, but had tons of problems like this. They all went away when I switched to Cygwin's Emacs. It's probably already installed. Just type the following at a bash prompt:

XWin -multiwindow &
export DISPLAY=:0.0
emacs

It will be a bit lighter than a Linux VM.

0
votes

Nowadays Cygwin come with emacs-w32 so you don't need to start X server.

I recommend to start it by:

run emacs-w32
run emacsclient-w32 -a "" %FILE_OR_DIR%

Because it is Cygwin Emacs with just native Windows GUI it works well with TRAMP for sshing to Linux hosts.

You can check my script http://hg.defun.work/utils/file/tip/emacs/e.bat as a quick file opener.

UPDATE With modern Windows and UAC it can be better to run Cygwin Emacs as:

cygstart --action=runas run emacs-w32