I'm using Cygwin and am using Console2 as replacement to the default Cygwin terminal. I need to use X11 forwarding, and every time I run startxwin it launches it in a new terminal. I would instead like to have it open in the same terminal (or in a new tab), inside of Console2. Ideally I'd be able to either launch Cygwin with startxwin or launch a new tab with a Cygwin XWin server. This will probably involve editing the .startxwinrc file, but I'm not sure how to go about doing this. I just recently starting using X11 forwarding in Cygwin.

How can I do this?


I'm trying to start an X11 session in Cygwin. This can be done by setting the DISPLAY environment variable to 0.0. The X11 server is then launched by issuing the command startxwin. After issuing that command, a new terminal opens, in a different window. That terminal is extremely basic and quite ugly. What I want to happen is either that terminal appear as a new tab in Console2 or have it run in the same terminal that it was launched from. This way I can continue to work in a terminal that has been optimized to my liking. Alternatively, if it is possible to launch Cygwin with X11 forwarding already enabled, that would be more ideal. I'm currently launching Cygwin by using this command H:\bin\bash.exe --login -i -c "cd /cygdrive/d; exec /bin/bash". The entire reason why I need to use X11 is because I'm SSHing into a remote server that has programs with GUIs that I need to use. I just want to be able to do everything inside of Console2.

  • Not clear. What exactly you are launching, and what is the problem? – Maximus Mar 28 '13 at 7:24
  • @Maximus, See above edit – TehTechGuy Mar 28 '13 at 12:25

I was having fits when switching to console2 trying to consistently get the console to recognize when X was running then to pass it properly to tabs when I set the tabs to connect to other systems. This is the solution I ended up settling upon.

First was to get a cygwin session run through a simple dos bat file to a shell to properly figure out if X was running or not. To do this, I used if/test to see if $DISPLAY was already set. If not, it runs a simple xset call to -display :0.0 to see if X is running. (0.0 is the default for my cygwin set up. You could obviously check others here as well if desired). If it returns true, it then sets DISPLAY accordingly. I added this to the end of the /etc/profile in cygwin, although you could probably put it in .bashrc or some other script that would always run when you log in.

# /etc/profile check to set display if X is running on :0.0
if [ -n $DISPLAY ]; then
  if xset -display :0.0 q &>/dev/null; then
    export DISPLAY=:0.0

When adding it to /etc/profile, you need to invoke a login shell when starting console2. e.g. calling bash --login -i from your dos .BAT file.

To handle going to other systems, I was initially using just a straight up call to ssh from a batch file, but this was skipping over setting this display. To get around that I modified the bat file to instead call bash --login first:

(the setting of DISPLAY was an initial attempt from another suggestion. It doesn't really do anything for most cygwin apps)


@echo off

chdir C:\cygwin\bin

if NOT "x%DISPLAY%" == "x" set DISPLAY=":0.0"

set sshcmd="ssh -Y "

bash --login -c %sshcmd%%1%

It is important to note, the modifications to /etc/profile will not do anything if cygwin's X server (or some other Xwindow server) is not running on :0.0 or if you don't invoke a login shell. But if you use bash -i --login in your Cygwin.bat and point your console2 settings to that file, and use the example Ssh.bat to connect to remote systems (e.g. put C:\cygwin\ssh.bat [remote-host-name] in the 'Shell:' field for the Console tab settings and C:\cygwin\bin as the startup dir)

  • Thanks for the solution. As you said you still have to make sure that the X server is running. It would also be good to modify ssh.bat to accept a username. Other than that, it seems to work well. This is much nicer than using the default X shell. – TehTechGuy Feb 9 '15 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.