Creating a productive UNIX-like environment under Windoz

Jan Labanowski, Computational Chemistry List, Ltd.

When you click on pics, they should open in another tab/window in their natural size.

I was adding to this document over the years. It is inconsistent since some notes come from WindowsXP, some from Windows7 and of course, the Cygwin underwent changes over this period of time. So be prepared that the current pics will be slightly different than the ones you see in the doc.

MS-Windoz is very lean as comes to sensible editors and command line processing etc. You need do install scores of additional packages if you want to do productive software development under Windoz. One package that will facilitate development of UNIX software is Cygwin. It is a Linux like environment that runs on Windoz. However, Cygwin is not UNIX. It is just a port of popular utilities and open source software to run under Windoz. So when you type in your Cygwin window:

$ echo $OS
Windows_NT

that is, your operating system is Windows_NT !!!. Cygwin offers familiar tools for those who use(d) Unix or Solaris. It also offers X-Window environment. It is configured to manage the X-Window functionality in a way similar to Linux, When I am forced to use Windows, I usually start from installing Cygwin under Windoz. There are some gotchas during installation, so pay attention.

Prerequisites

You need to assess your working environment before you install Cygwin:

If you are behind a proxy server, it is convenient to set the http_proxy Environment Variable under Windoz. This is required in the case when you have a restrictive proxy server. In the case of one company we do not have it, so you do not need to do it. But I will tell you anyhow. You can double check by opening your MS Windoz Internet Explorer and checking the configuration information as:
[Tools]->[Internet Options]->[Connections]->[LAN Setting]. If you get:

you do not have to do the http_proxy setting. If you get something like:

You need to get the file from the URL given and look at it. At the end of the file you will see something like return "PROXY http-proxy.mydomain.com:8080";
that provides the address of our proxy server.

Creating a http_proxy User Environment Variable

You do not have do to it in the One Company environment!!!

Now, you may need to create a variable http_proxy under Windows. The reason for this is, that some Cygwin programs use protocols like FTP to fetch updates or additional modules/libraries (you will need them, if you want to add modules to perl installed under Cygwin from CPAN repositories). This is done by:

[Start]->[Settings]->[Control Panel]->[System]->[Advanced]->[Environment Variables]

Click on [New] under User variables for YourId:

and as Variable name: enter: http_proxy while for Variable value: enter: http://labanj1:MyVeryStrongPassword@http-proxy.mydomain.com:8080 (of course, replace my login id labanj1 with your login id, and the string MyVeryStrongPassword with your actual password). This is not safe, but this is Windoz. Then click OK. Unfortunately, every time you change your password, you will need to Edit this variable. Note: the Environment Variable is sometimes not visible until next reboot of Windoz, so reboot your desktop computer now just in case. As I said, this step is not necessary, but will help you up the speed if you want to use the Cygwin distribution from the FTP site rather then HTML. To tell you the truth, I am not sure if it is necessary at all for some users that have advanced privileges.

Installing Cygwin (long...)

Now you need to download the setup.exe, that is a Cygwin installer that will download and install Cygwin on your computer. Currently, there are 2 setup options: for 32-bit (setup-x86.exe) and for 64-bit (setup-x86_64.exe). Choose the one that corresponds to your windows version. Go to the site: http://cygwin.com/install.html and read it (I mean it, unless you already used Cygwin for a long time). Then click on setup.exe and Save/Run the setup.exe:

When you run the setup.exe (e.g., by double clicking) you will see the following window:

Click on [Next] and choose the [Install from Internet] which is a default:

Click [Next]. Now, you will have to make some choices depending on the fact if you have Administrator privileges. If you do not, you cannot install fully functional Cygwin (though even the partially functional Cygwin is better than Windoz). You usually do not have access to the top folder on your C: drive. They usually give you some writable folder under C:\Users\<YourLoginName> where you can create a directory cygwin that will be a top root directory for your installation. Make sure that the directory path for your Cygwin installation does not have spaces in it, or you will have a lot of problems, since you will have to use quotes to access it for every and any command. First of all, your username should not have spaces in it. If it has, you are hosed, and your sysadmins are idiots.

The next thing is the scope of the Cygwin installation. If you HAVE admin privileges, install Cygwin for All Users. If you do not, install Cygwin for Just Me. Check also if you have a D: drive as some companies partition the drives to give you the System Drive C: and the User Drive D:. Make sure that you do a right choice since your future lavel of frustration depends on it.

Click [Next] and accept default (unless you know better):

Click on [Next] and select Use Internet Explorer Proxy Setting. In the Some Company environment you will use the Direct Connection

but if you were behind the Proxy Server you would have to use the 2nd choice:

and [Next] to get the list of mirror sites where the Cygwin can be downloaded from. I used the http://mirror.msc.anl.gov since it worked for me in the past, so I am using it again (the people from Argonne National Lab seem to do a good job of keeping Cygwin up to date).

After you click on [Next] you will get a humongous list of software packages that is available for download within Cygwin. If you do not select anything explicitly, the setup.exe installer will only install a Base system that is pretty useless and limited. By clicking on [+] buttons you will see what packages are available. Now, you can activate the individual packages by clicking on the Skip (it will then change to a version number or a Keep or whatever). Of course, when there is no Skip by the package name, it is already selected. By clicking on the entry in this column multiple times you can go through all the options for the package. Here are some suggestions what you may want to include. But do not despair, since you can run setup.exe again to add or remove packages:

...
Archive
  p7zip
  sharutils
  unzip
  zip
...
Database
  perl-DBI
...
Devel
  ascii
  binutils
  bison
  gcc4-*   # download gcc4 rather than gcc since newer CPAN modules need it
  make
  openssl-devel
  patchutils
  perl-ExtUtils-Depends
  perl-Ext-Utils-PkgConfig
  zlib-devel
Doc
  cygwin-doc
  cygwin-x-doc
  man
  perl-manpages
  textinfo
  xorg-docs
  xpdf
Editors
  emacs
  emacs-X11
  joe
  nano
  ted
  vim
  xemacs
  xemacs-emacs-common
  xemacs-tags
...
Graphics
  GraphicsMagic
  ImageMagic
  ImageMagic-doc
  bmp2png
  epstool
  ghostscript
  ghostscript-fonts-other
  ghostscript-fonts-std
  gnuplot
  gv
  jpeg
  perl-Graphgics-Magic
  perl-Image-Magic
  plot-utils
  tiff
  xfig
Interpreters
  emacs-el
  gawk
  perl
...
Libs
  zlib
  
Math
  bc
  xcalc
...
Net
  openssh
  openssl
  perl-Net-Libproxy
  stunnel
...
Perl
  perl
  perl-XML-Simple
  perl-libwin32
  perl-ming
  pwget
Publishing
  pdftk
  
...
System
  util-linux
...
Text
  a2ps
  aspell
  aspell-en
  groff
  gv
  less
  test2html
  tidy
  
Utils
  bzip2
  cpio
  diffutils
  gnupg
  ncurses
  
Web
  curl
  links
  lynx
  webcheck
  wget
  wput
X11
  font-adobe-dpi100
  font-adobe-dpi75
  font-alias
  font-bitstream-dpi100
  font-bitstream-dpi75
  font-daewoo-misc
  font-dec-misc
  font-encodings
  font-isas-misc
  font-jis-misc
  font-util
  font-xfree86-type1
  fontconfig
  freetype2
  imake
  xauth
  xclock
  xfig
  xfig-lib
  xfontsel
  xhost
  xinit
  xkeyboard-config
  xlsfonts
  xman
  xmore
  xorg-docs
  xorg-scripts
  xorg-server
  xorg-server-common
  xorg-util-macros
  xpdf
  xset
  xterm
  xwd
  xwud

When you click on [Next], the thing will start downloading the stuff you requested, but you may get an additional screen that lists dependencies (some packages need other packages)

Make sure that Select required packages (RECOMMENDED) is selected and click on [Next]

Now it will download and download and download.... It may take a few hours, so let it run and do something else in the meantime. Finally, after the stuff is downloaded and installed, you will get a window like:

Make sure the Create icon on Desktop and the Add icon to Start Menu are selected. Then click on [Finish]. You may also get an error screen. Just do what it suggests, unless you cannot make any sense of it (sometimes nobody knows what they are actually saying). If this happens, just try to install Cygwin again from scratch but skip some packages, and plan on downloading them later. If still no go, then try to install only default Basic Cygwin and talk to some guru about your personal problems or google. It worked for me, but some things depend on how your Windoz is set up.

Updating Cygwin

The standard way to update Cygwin is essentially the same as installing it. That is, you either find a setup.exe file in your Downloads directory with a Windoz Browser (once called My Computer and in Windoz 7 it is called Computer... I bite my tongue here...). You can also always get the new setup.exe file from the Cygwin home page http://www.cygwin.com/. This is a preferred way, especially when you did not update Cygwin for a while. The reason being that the setup.exe gets stale too, and it is always good to use the latest one.. However, you need to remember to provide the same directory basic information as during installation. Usually it will be OK since Cygwin remembers but pay attention. Namely, in the examples above the Root Directory was was C:\cygwin, the Install For Just Me and the Local Package Directory was also the same as provided before. Therefore your dialogs:

and

.

should look the same as when you installed the Cygwin the first time. However, there is a package for Cygwin that resembles the debian apt-get or Red Hat yum/rpm things that is available from: https://code.google.com/p/apt-cyg/. Unfortunately, they are fuzzy as to how to get (i.e., download) the thing. I downloaded it last time by going to their github at https://github.com/transcode-open/apt-cyg, clicked on apt-cyg, then clicked on [Raw] button in the right left menu above, and just saved the file from the browser. Then:

cp .../apt-cyg /usr/local/bin
chmod 755 /usr/local/bin

Now you will be able to get the packages for Cygwin without starting the setup.exe everytime you need something.

You can also install the apt-cyg their way. Here is how. As a prerequisite you need to have some packages that I already installed, namely: wget, tar, gawk, bzip2, shutdown, subversion and vim. It is easy to find if they were installed before with the which command. If you get

$ 
which vim /usr/bin/vim

the vim is installed. But if you get:

$ which subversion
which: no subversion in (/cygdrive/d/....

you need to install it. You can do it in a traditional, GUI way described above. Then you need to fetch the apt-cyg from the subversion repo on googlecode by using the following command under Cygwin:

$ svn --force export http://apt-cyg.googlecode.com/svn/trunk/ /bin/
A    /bin
A    /bin/apt-cyg
Exported revision 18.
$ chmod +x /bin/apt-cyg
$

There is also a write-up about it: http://www.fir3net.com/Cygwin/cygwin-package-installation.html.

Using Cygwin

Note that now, on your Desktop, you should have a Cygwin Terminal icon.

You can double-click on it to get a very basic Cygwin shell window. It is really dumb and is similar to DOS Command Window (Oh well, I take it back, it is not that dumb):

You can get the same Cygwin shell window by going through the Start menu, namely: [Start]->[Programs]->[Cygwin]->[Cygwin Terminal]. The Cygwin shell window is very limited as to its graphics and GUI capabilities but you are ready to type in some Unix commands like pwd (what is my current directory?), date (what is current date?), ls -la (list all files including the hidden ones), which startxwin (where exactly is the startxwin command located on the file system?), etc. Note that my Cygwin directories are located under Windoz D:\cygwin directory and your may be different. The Cygwin uses the Unix style of file and path naming, so you will not see Drive Letters and backslashes, and other enhanced (i.e., non-POSIX, non-standard, incompatible, etc) M$ Windoz creations. Cygwin has a regular root directory, i.e., /. The / in Cygwin corresponds to D:\cygwin (or wherever you installed the Cygwin). So can you access the Windoz files from Cygwin? Elementary, my dear Watson. To use the regular Windoz file path notation you would need to double the backslash characters (under Unix, the \ is the shell escape character, not a file separator, so you would need to double it to use it verbatim. For example:

LT-N0638|~
$ ls -l D:\\cygwin
cygwin warning:
  MS-DOS style path detected: D:\cygwin
  Preferred POSIX equivalent is: /
  CYGWIN environment variable option "nodosfilewarning" turns off this warning.
  Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
total 913
drwxr-xr-x 1 jlabanowski Domain Users      0 Mar  4 16:25 bin
drwxr-xr-x 1 jlabanowski Domain Users      0 Mar  4 16:18 cygdrive
-rw-r--r-- 1 jlabanowski Domain Users     57 Mar  4 16:25 Cygwin.bat
-rw-r--r-- 1 jlabanowski Domain Users 157097 Mar  4 16:25 Cygwin.ico
-rw-r--r-- 1 jlabanowski Domain Users  53342 Mar  4 16:25 Cygwin-Terminal.ico
drwxr-xr-x 1 jlabanowski Domain Users      0 Mar  4 16:16 dev
drwxr-xr-x 1 jlabanowski Domain Users      0 Mar  4 16:25 etc
drwxr-xr-x 1 jlabanowski Domain Users      0 Mar  4 17:05 home
drwxr-xr-x 1 jlabanowski Domain Users      0 Nov 22  2010 lib
drwxr-xr-x 1 jlabanowski Domain Users      0 Mar  5 16:39 tmp
drwxr-xr-x 1 jlabanowski Domain Users      0 Mar  4 16:09 usr
drwxr-xr-x 1 jlabanowski Domain Users      0 Mar  4 16:24 var
LT-N0638|~

There is a better way... Under the Cygwin's /cygdrive directory, you have all you Windoz drives mounted. That is, you can easily access all files on your local computer even if they are on mapped drives. You can also access files on shares like:

If the stuff does not work, it is because your default RC scripts were installed in a different directory. Due to Windoz setting, the Cygwin installer gets confused sometimes and puts the things like .bashrc someplace else. Since I installed my Cygwin in D:\cygwin, my Cygwin's HOME directory is D:\cygwin\home\jlabanowski where I can place the RC. Here are examples of my files: .bashrc.bashrc, .bash_profile.bash_profile and .profile. To use these file, you need to change my id to your id in some places.

Notice that Cygwin uses slashes / instead the backslashes \. The backslash \ on Cygwin is an escape character (for the uninitiated, the escape character on Windoz is caret ^).

Annoying Aero Shake

The Microsoft strives in constantly providing new and useless functionality to their Windows platform. This way they can claim that they are the leader in software development. For the end user, however, it results in constantly learning new useless stuff that they do not need and spending time in reverting or disabling the new features of Windows to the state before the change. An example of such a feature is a new functionality Aero Shake. If you move a window around with a mouse and accidently "shake" a selected window close to the border of the screen, all your windows are minimized and placed on your task bar. When this happens I have to painstakingly click on all my icons on the task bar to restore the windows on my screen. Since shaking happens to me more often with getting older, it is terribly annoying for me. There is no easy way to disable this feature. The gurus in Microsoft decided that this is a must have for people who watch porn at the office and need to hide their windows quickly when the boss is coming.

You need to edit the registry to disable this thing. I will not describe the procedure in details, since very good overviews are already available on the Web: http://lifehacker.com/disable-windows-aero-shake-with-a-registry-tweak-51293097 and http://www.askvg.com/how-to-disable-aeroshake-in-windows-7/ and more, when you Google for it. Basically you need to add the new Registry key under HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows, Key: Explorer, Value: NoWindowMinimizingShortcuts as a DWORD value of 1. Then of course, you need to reboot the Windoz. This will prevent future shaking out of windows phenomenon from reoccuring.

The X-window under Cygwin

However, to do real stuff, you need the X-window server and the xterm window. If you followed the instructions above, you should have these packages installed. First, you need to start the X-window server, to be able to run the X-window applications. If you type the startxwin command in the Cygwin shell window you can either start the X-Window server (startxwin will spew a lot of text (and errors) and start a basic xterm on your Desktop), or you get an error message like this:

$ startxwin
startxwin: Another X server instance is running on DISPLAY :0
: Resource temporarily unavailable

If you get an error, that means the X-window server is already running. I usually just kill it. To to this I type the following in the Cygwin Terminal window:

ps -ef | grep XWin
jlabanow     5292  1 pty0 08:45:13 /usr/bin/XWin
kill -9 5292
startxwin

Do not worry about a lot of chatter that you get:

The above just kills the running Xwin server and then starts a new one. Another way of starting an xterm is to try to type xterm in the Cygwin Terminal. My .bashrc sets the DISPLAY variable to the default value, so it should work (I talk about it a bit later).

After successful startxwin execution you should get the following basic xterm window like:

If you got it, you can skip the remedies below and go directly to xterm customization instructions. If you get the error, it means that the X-Window server already runs and Cygwin refused to start another one. You could kill it as described above, or reuse it. The X-Window server may already be running, for example, some other package (like the Hummingbird server) had started it before, or you already started the X-Window server in Cygwin and forgot about it, or you clicked on some stuff under [Start]->[Cygwin-X]. If you get such a message, you need to tell the Cygwin where the X-Window server is running. This is done via setting an environment variable called DISPLAY that points to the machine address and X-Window server instance. The syntax for the value of the DISPLAY variable is <host>:<display_no>.<screen>. Since you are running X-Window server on your local machine (127.0.0.1), you are only running one server and one screen, I assume. In this case, you can set the DISPLAY variable by typing the following in your Cygwin shell window:

export DISPLAY=127.0.0.1:0.0

Since most of these pieces have defaults, you can even type:

export DISPLAY=:0.0

or even

export DISPLAY=:0

When you now type:

xterm &

(note, the & is there to run the xterm in the background without blocking the command prompt in the Cygwin shell window that would wait until the application exits) you will fire your basic xterm like the one shown above.

xterm looks customization

You can create a customized xterm instance by using command line options of the xterm command. There are tons of them, so it is good to wrap them in some simple scripts that will customize fonts, sizes, colors, terminal name on the top bar, etc. Type: man xterm in the Cygwin shell window, and keep hitting space until it finishes (if you loose your patience, just hit q). I created a simple wrapper for xterm with bigger fonts and nicer colors. To use them, go to the basic xterm window shown above and type (or cut the stuff below and paste) it in the basic xterm window by clicking on the mouse middle button):

cd
mkdir bin
cd bin

cat > xterm3 <<EOF
#!/bin/bash
if [ "x$DISPLAY" = "x" ]; then
  export DISPLAY=:0.0
fi
xterm -fg "Aquamarine" -bg "Dark Red" -fs 10  -sb -sl 1000 -tn "xterm3" -T "xterm3" -font -adobe-courier-medium-r-normal-*-14-*-*-*-*-*-*-* &
EOF

cat > xterm6 <<EOF
#!/bin/bash
if [ "x$DISPLAY" = "x" ]; then
  export DISPLAY=:0.0
fi

xterm -fg "#822222" -bg "#F0F6DC" -fs 10  -sb -sl 1000 -title "xterm6" -n "xterm6" -T "xterm6" -font -adobe-courier-medium-r-normal-*-14-*-*-*-*-*-*-* &
EOF

cat > xterm7 <<EOF
#!/bin/bash
if [ "x$DISPLAY" = "x" ]; then
  export DISPLAY=:0.0
fi

xterm -fg "#624262" -bg "#F0E6CC" -fs 10  -sb -sl 1000 -title "xterm7" -n "xterm7" -T "xterm7" -font -adobe-courier-medium-r-normal-*-14-*-*-*-*-*-*-* &

EOF


chmod 755 xterm3 xterm6 xterm7

cd
export PATH=$HOME/bin:$PATH


i.e., create bin subdirectory, then files: xterm3, xterm6 and xterm7, then add execute permissions to files, and finally export the PATH environment variable with the bin subdirectory prepended, so the shell can find it without you typing the path every time. My belowed xterms are xterm3 and xterm9 but you are welcome to spend hours to tweak colors and fonts. Now you can type the xtermX in your basic Xterm plain vanilla xterm from above and get a beauty like:

Note that under Cygwin you cut/past differently than under Windoz. If you have a standard 3-button mouse, and you did not change its default configuration under Windoz, you should be able to select by dragging a mouse over text with the left button depressed, and then paste it by clicking on the middle button once you positioned your mouse in the place where the text should go. The Cygwin uses Windoz device driver/services for mouse operation, and since out-of-the-box Windoz does not provide auto-focus, you cannot normally use this X-window feature in Cygwin. You still have to left-click on a window to make it active. While there is a utility to add the auto-focus to Windoz, it is only for the where eagles dare and it would require your phone conference with the Help Desk to have it installed. I still do not know why Windoz did not implement the auto-focus (i.e., your window under Linux usually auto-selects when you place a mouse pointer in it) as default. I often end up typing in some random window while my mouse pointer is in the window I wanted to type. If this happens, I curse loudly and then click on the mouse. There is a general agreement that MS Windoz was created for aliens with three hands. They must know something that we, the mortals, do not know...

Secure shell (ssh) and Secure copy (scp)

While I cannot provide you with Unix tutorial here, the important functionality is to log in to an account on some other machine (for example to the Linux server from your xterm under Cygwin. Windoz does not have native a ssh and while you can install putty it does not have all the bells and whistles of the Linux ssh. Cygwin comes with ssh and scp and the related utilities. For example I can log into my server from the Cygwin's xterm

I can edit files there and have a similar feel like I am on Cygwin.

You may have noticed that I logged in to the server.ccl.net without using a password. Obviously, I would not live the server without authentication. So there must be a trick. ssl allows you to generate a pair of public encrytion keys and than use them for authentication rather than a traditional login/password. It is especially convenient when you want to execute some commands on the remote system from inside the script. You do not have to enter your password in plain text inside the script to log in. You can just authorize one account to access another account without a password. Before it can be done you need to generate the pair of keys. There is a utility ssh-keygen that will do it for you.

Just type ssh-keygen -t rsa and keep hitting [Enter] until it stops asking you for stuff. Just take all the defaults. It will save a pair of keys (public and private) under the hidden directory .ssh in your HOME directory.

$ ls -l .ssh
total 5
-rw-------+ 1 root None 1679 Jan 14 23:43 id_rsa
-rw-r--r--+ 1 root None  394 Jan 14 23:43 id_rsa.pub

Note the protections. The private key id_rsa is only readable by the owner, while the public key id_rsa.pub is readable by anyone. This is the key you usually share with others so there is no reason to make it a sekret.

Now, you need to transfer this id_rsa.pub file to the account that you want to access without a password. Initially you can copy it to some file on the remote system using the scp, for example:

$ scp -P 2345 .ssh/id_rsa.pub  jkl@server.ccl.net:my_public_cygwin_key
The authenticity of host '[server.ccl.net]:2345 ([66.93.212.15]:2345)' can't be established.
RSA key fingerprint is 6f:bz:cx2:5a:4b:ey7:6f:1b:4c:fc:29:74:9d:b2:7c:a7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[server.ccl.net]:2345,[66.93.212.15]:2345' (RSA) to the list of known hosts.
jkl@server.ccl.net's password:
id_rsa.pub                            100%  394     0.4KB/s   00:00

Note two things here:

Now we have the public key from our Cygwin copied to the remote computer. Now we need to append this public key to the .ssh/authorized_keys on this computer.

Or if you want it in the text format:

root@root-PC /cygdrive/c/cygwin64/home/root
$ ssh -p 2345 jkl@server.ccl.net
jkl@server.ccl.net's password: *********
Last login: Wed Jan 14 23:15:48 2015 from d143-67-21-87.try.wideopenwest.com
[jkl@server ~]$ ls -l my_public_cygwin_key
-rw-r--r--. 1 jkl jkl 394 Jan 14 23:55 my_public_cygwin_key
[jkl@server ~]$ ls -l .ssh/authorized_keys
-rw-------. 1 jkl jkl 407 Jan  4  2014 .ssh/authorized_keys
[jkl@server ~]$ cat my_public_cygwin_key >>  .ssh/authorized_keys
[jkl@server ~]$ ls -l .ssh/authorized_keys
-rw-------. 1 jkl jkl 801 Jan 15 00:24 .ssh/authorized_keys
[jkl@server ~]$ exit
logout
Connection to server.ccl.net closed.

root@root-PC /cygdrive/c/cygwin64/home/root
$ ssh -p 2345 jkl@server.ccl.net
Last login: Thu Jan 15 00:23:21 2015 from d143-67-21-87.try.wideopenwest.com
[jkl@server ~]$

As you can see, the 1st time when we logged in to append the public key we had to provide the password. Next time, we were logged in without a password. Note also the permissions. The authorized_keys file should by only readable by the owner. The recent editions of ssh will not even allow you to log in, if your authorized_keys file has less restrictive permissions.

Some programs to install manually

You can always add programs/packages to Cygwin by clicking on Cygwin's [setup.exe] icon on the Desktop (or finding this utility in your Windoz [Download] directory). Select the options that you chose when you installed Cygwin. Remember on selecting the [Just me] if you are not an ADMIN, since this one does not sometimes default to what you selected originally. You will get eventually to the [Select Packages] window where you select packages to install. You can type the name of the package/program that you want to install there, and then unfold the groups and click on the program, and install the thing by continuing with the click on [Next] at the bottom. You can usually do it without the need to kill a running Cygwin, though obviously, it is probably safer to do it when your Cygwin is not running. There are however programs that are not on the Cygwin's [Select Package] menu. For example, the applypatch that is needed by the perl's cpan installer. I did this in the following manner:

Emacs

The Cygwin has the emacs editor. If you know and like vi, just skip this part. The Cygwin has vi as any other Unix system. If you realize that emacs is a more powerful editor and want to learn it, Cygwin is a good place to start. The emacs has in fact two variants: a plain text version, and the graphical version. If you type emacs for the first time in a Cygwin xterm you will get something like:

If you type emacs -nw instead (i.e., requesting a text only emacs), you will get the following inside your xterm:

For a newbie the graphics emacs may be easier to operate. But I use text emacs, since I hate GUIs. Do not follow me, but rather Follow the Force. If you are new, go through Emacs Tutorial and then start your life time journey of learning emacs as you go. Remember at least two commands: CTRL/g will kill the current emacs command, and two-keystroke sequence: CTRL/x CTRL/c will get you out of emacs, so you do no have to reboot your computer to kill the damned thing.

I noticed that the recent (2014) emacs added an electric mode as a default. They added it even on PLAIN TEXT FILES (what was the idea behind it, beats me...). I immediately disable it, either by placing:

(when (fboundp 'electric-indent-mode) (electric-indent-mode -1))
in the $HOME/.emacs directory, or type:

M-x electric-indent-local-mode
command when already in emacs.

Customizing your Cygwin

I do not want to write a book. But when you go to your Cygwin home directory and type ls -la you will see at least .bashrc and .profile. The .bashrc is for the customization of your Bash shell, while .profile is for your Unix shell in general. I.e., the .bashrc would be a place to change your prompt, while .profile would be a place to change your PATH. But it all depends on what you want to do. For example, I added the following at the end of my .profile:

PATH=$PATH:/home/labanj1/bin
export PATH

while the following was appended to my .bashrc:

alias emacs="emacs -nw"
export PS1='\w\[\e[0m\]\n\$ '

To learn more about it, read, for example, http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html and scores of other pages that you can find on Google.

Windows and Cygwin Paths

Cygwin uses Unix style paths while Windoz using something strange. Moreover, the Windoz Drives (both physical and mapped) are present in the Cygwin's /cygdrive directory. On my computer:

$ ls -l /cygdrive
total 88
d---------+ 1 TrustedInstaller TrustedInstaller 0 Apr 24 03:00 c
drwx------+ 1 SYSTEM           SYSTEM           0 Apr 24 03:00 d
drwx------+ 1 Administrators   ????????         0 Apr 22 10:21 i
drwx------+ 1 Administrators   ????????         0 Apr  3 16:33 j
drwx------+ 1 ????????         ????????         0 Apr 24 17:03 p
drwx------+ 1 Administrators   ????????         0 Apr 10 10:31 t
drwx------+ 1 Administrators   Domain Users     0 Apr 24 07:46 u
$

Therefore, the Windoz path I:\some\dir\some\file corresponds to the Cygwin's path /cygdrive/i/some/dir/some/file. There is a Cygwin's utility, cygpath, that converts the Windows paths to the Cygwin's paths (do man cygpath or Google). However, if you use the mouse to copy and paste, this would be inconvenient.

Spaces in the directory names and Windoz %PATH%

To make Windows incompatible with Unix and other operating systems, the MS teams put a lot of effort into malicious naming of directories and files. The names often contain spaces and other characters that need to be escaped on the command line in Linux. Many Cygwin shell scripts do not expect such things and do not quote environment variables. For example:

$ ProgFilesDir="Program Files"
$ cd /cygdrive/c/$ProgFilesDir
bash: cd: /cygdrive/c/Program: No such file or directory

When I need to use the Windows paths and they contain the spaces or other junk in their name, I create Windowz junctions that do not contain the junk characters. The junctions are similar to Unix links. You can create them in the Command Prompt Windoz terminal. Often you will need administrator privileges (i.e., you need to right click on the Command Prompt icon and select [Run as administrator]). For example, the Windoz naming for the Program Files was a well thought out effort on the part of MS. In my case I ran the following commands in the Command Prompt:

mklink /j  C:\Program_Files  "C:\Program Files"
mklink /j  C:\Program_Files_x86 "C:\Program Files (x86)"

If you do dir you will see:

Then, when you use your links rathe than original names, you will be fine:

$ ProgFilesDir=Program_Files
$ cd /cygdrive/c/$ProgFilesDir
$ pwd
/cygdrive/c/Program_Files
$ ls -l
total 49
...
drwx------+ 1 SYSTEM           SYSTEM             0 Nov 19 17:24 Java
...
drwx------+ 1 SYSTEM           SYSTEM             0 Nov 23 23:02 MySQL
...

Once you have the links created you can use these links rather than original names in your shell scripts, in your .profile or your .bashrc. Before Cygwin shows you the Cygwin shell window, it converts the Windoz environment variables, like %PATH% to the Cygwin's file path syntax. You can help Cygwin, and do the conversion and overriding yourself. For example, if you see:

$ echo $JAVA_HOME
C:\Program Files\Java\jdk1.8.0_25

you can override the JAVA_HOME in your .bashrc as:

export JAVA_HOME=C:\\Program_Files\\Java\\jdk1.8.0_25

Double backslashes? Windoz style path? Why? Since we are running the Windoz Java, not a Cygwin Java. And Windoz Java wants Windoz paths, since it knows it is on Windoz, and Cygwin's OS is Windoz:

$ echo $OS
Windows_NT

Some installers on Windoz are buggy and may insert strange things into the Windoz %PATH% variable. Since Cygwin inherits the Windoz %PATH% and converts it to the Cygwin's $PATH you may want to check what comes out. You can Cut/Paste the contents of Command Prompt Windoz terminal by left clicking on the small [C:\.] icon in the upper left corner of the Command Prompt, then select [Edit] -> [Select all] (it will select the text in the window into the Cut/Paste buffer and you can see recerse video change) and then you can Paste the text into your favorite Cygwin's editor with a click on the Mouse middle button. You can then examine it and convert to a Cygwin's $PATH. In my case I had (Note: a lot of stuff was removed):

C:\Users\jkl>echo %PATH%
...C:\Vagrant\bin;C:\Program Files\Java\jdk1.8.0_25\bin;%PATH
C:\Users\jkl>

i.e., something was inserting the %PATH instead of %PATH% into the Windows environment %PATH% variable. This is inocuous, but annoying. I did not have time to chase this thing. So I rewrore the Windows %PATH% by editing the .profile (I actually rewrote the /etc/profile that is run first). Then at the top .profile (or /etc/profile) I entered the line:

export PATH=.../cygdrive/c/Vagrant/bin:/cygdrive/c/Program_Files/Java/jdk1.8.0_25/bin

Again, the ... means that a lot of stuff was removed.

I use these Windoz junctions a lot. For example, rather than installing the maven under Cygwin directory, I unpacked it under /cygdrive/c/Program_Files, then updated the Windoz %PATH% (through Control Panel) to include mavens bin directory. Since /cygdrive/c/Program_Files is owned and writable by the Administrator, I unzipped the apache-maven-3.2.5-bin.zip as the Administrator. Cygwin does not have a Linux sudo command, but it has a cygstart that you can convert to a pseudo sudo by creating a small script and placing it in the /usr/local/bin (stolen from http://stackoverflow.com/questions/4090301/root-user-sudo-equivalent-in-cygwin). I decided to do the alias in my .bashrc:

alias sudo="cygstart --action=runas $@"

Then I unzipped the maven archive as:

$ cd /cygdrive/c/Program_Files
$ sudo unzip ~/downloads/apache-maven-3.2.5-bin.zip
    #  Windoz asked me to login as Admin
$ ls -l | more
total 85
drwxr-xr-x+ 1 root  None   0 Dec 14 12:30 apache-maven-3.2.5
....
$ sudo chown -R jkl apache-maven-3.2.5
    #  Windoz asked me to login as Admin
$ ls -l | more 
total 85
drwxr-xr-x+ 1 jkl   None   0 Dec 14 12:30 apache-maven-3.2.5

Finally, I appended the C:\Program_Files\apache-maven-3.2.5\bin to the %PATH environment variable via Control Panel. Rebooted Windoz, and added the following environment variables to my .bashrc in Cygwin:

export M2_HOME=C:\\Program_Files\\apache-maven-3.2.5
export M2=C:\\Program_Files\\apache-maven-3.2.5\\bin
export MAVEN_OPTS="-Xms256m -Xmx512m"

And if you did not mess up, you will get:

$ echo $OS
Windows_NT
$ echo $JAVA_HOME
C:\Program_Files\Java\jdk1.8.0_25
$ echo $M2_HOME
C:\Program_Files\apache-maven-3.2.5
$ echo $M2
C:\Program_Files\apache-maven-3.2.5
$ mvn -v
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T12:29:23-05:00)
Maven home: C:\Program_Files\apache-maven-3.2.5
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: C:\Program_Files\Java\jdk1.8.0_25\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

Note that I had to use the Windoz path notation, and since Cygwin is kinda Unix, I had to double the backslashes when defining environment variables, since the single backslash would be treated as escaping the character that follows it. Why not just use the Cygwin paths starting from /cygdrive/c/Program_Files/...? The maven and plenty of other Java based utilities query first the Java system properties, like os.name, file.separator, etc. Since our Java is the Windoz Java, all these thigs are set to Windoz stuff.

Concluding remarks

If you found bugs, something that does not make sense, you have suggestions for corrections or additions, let me know: jkl at ccl dot net. If you found it useful, make a link from your site, or mention it on some blog.

Jan Labanowski