Cygwin Hints and Tips

Introduction

This is intended for those who are relatively new to a unix environment, but there's probably some nuggets in here for die-hard unix fans too!

Documentation

Install the packages cygwin-doc and cygwin-x-doc (X-Windows).

Documentation can be found in sub-directories of /usr/share/doc/ - also try 'info cygwin' or 'man cygwin'. These packages provide documentation on Cygwin for the Windows user, including the description of usefull utilities unique to Cygwin.

-- Frank Dean - 21 Oct 2005

/usr/share/doc/Cygwin/ contains package specific README files, with extra information relating to installing and running under Cygwin. See also /usr/share/doc/cygwin-doc-x.x -- Frank Dean - 17 Aug 2008

Package Management

Installing, upgrading and uninstalling packages is done through the Cygwin setup.exe program. Read the instructions on the Cygwin website for details on how to select and view packages.

To perform task like listing package contents, use the 'cygcheck' utility. E.g. 'cygcheck -l cygwin-doc' will list all the files installed by the cygwin-doc package.

Finding Packages

Search for packages at http://cygwin.com/packages/

-- Frank Dean - 21 Oct 2005

.bashrc

Useful bits to considering including .bashrc

#!/bin/sh

# Not sure whether this is quite right!
# _XKB_RULES_NAMES
# /usr/X11R6/bin/setxkbmap -rules xfree86 -layout gb -model pc105

HTTP_PROXY=proxy.server.co.uk:8888
TERM=cygwin
CVSROOT=:local:/cygdrive/e/cvsroot
export TERM HTTP_PROXY CVSROOT

Terminal not fully functional?

If you get a message from cygwin that your terminal is not fully functional it may be because the utility doesn't recognise your terminal type, having examined the TERM environment variable.

  • echo $TERM

to see what it's set to.

  • export TERMcygwin=
  • export TERMvt100=
  • export TERMxterm=

should help, although some key mappings may not work correctly.

Error executing shell

If you get errors to the effect that cygwin or X cannot start a shell, it may be caused because the SHELL environment variable has been set in DOS/Windows. Deleting the environment variable in Windows should fix the problem, but obviously may impact whatever software installation set it in the first place.

Home directory

The environment variable HOME dictates where cygwin believes your home directory is (as represented by the tilde character '~')

You can change this within your Windows environment by defining an environment variable of HOME as "%HOMEDRIVE%%HOMEPATH%\My Documents" (Environment variables are set under "Start|Control Panel|System|Advanced|Environment Variables" for most versions of Windows)

or include the following in your .bashrc file

  • export HOME/cygdrive/c/my\ documents=

Filenames

Spaces can be included in filenames by using the backslash character.

Example:

  • cd /cygdrive/c/Program\ Files

Misc

Apologies - just quick notes here for now - will clarify one day!

Most emacs key strokes work in command line - include link to emacs docs

C means CTRL therefore CTRL-A is C-A

M means meta (alt or esc key) therefore Alt-A or Esc-A is M-A

  • C-a - Move caret to begin of line
  • C-e - Move caret to end of line
  • C-d - Delete character
  • C-k - Delete to end of line
  • C-f - Forward character
  • M-f - Forward word
  • C-b - Back character
  • M-b - Back word

Common Commands

  • C-z - puts Emacs in background
  • %emacs on command line brings it back
  • jobs - shows background jobs

Tab Completion

Generally, hitting the tab key whilst entering a command assumes you are trying to complete a filename, and will complete the file as far as can be done without ambiguity. If the partially type filename is ambiguous, a system beep is emitted and compeletion occurs up to the point of ambiguity. Pressing tab a second time shows all possible combinations, after which you can type some more, press tab and son on.

Mount Command

You can mount DOS paths with the mount command. Enter 'mount' on it's own to see a list of current mounts. It's probably good practice to create a directory where you intend to mount the drive. This doesn't seem necessary, but a warning is issued if you do not.

Example:

  • mkdir /mnt
  • mkdir /mnt/ps
  • mount 'c:/mydir/subdir' /mnt/ps

'man mount' will tell you more.

Note the c:\ drive is mapped to /cygdrive/c/. Changing directory to 'c:' changes your current directory to /cygdrive/c.

Example:

  • cd c:

Command history

The history command lists previous commands. These are persisted in ~/.bash_history which is saved when you exit the cygwin shell. I'm not too sure what happens with saving history where you have multiple shells open (Probably only saves the last one to be closed). Entering a command line beginning with exclamation mark followed by the history line number re-excecutes that command.

Example:

  • history | grep mount

Lists all previous commands that included 'mount'.

  • !532

Executes item 532 from the history buffer.

  • !532:p

Brings up item 532 in the command buffer for editing and potential execution.

Watching log files

Shows the last ten lines of the log file and thereafter list any further items written to the log.

  • tail -f my.log

Counting lines of code

The following provides a simple line count. Includes blank lines. Due to the way xargs works, may output more than one sub-value. Total of sub-values will be the grand total.

  • find -iname '*.java' | xargs awk 'BEGIN {n=0;} {n++;} END {print "Lines = " n;}'

You will get errors that files cannot be found if a filename includes any spaces.

In that case run the command in two stages use sed to replace spaces with backslash spaces.

  • find -iname '*.java' > temp.txt

Replace spaces with backslash space using sed or favourite editor.

  • cat temp | xargs awk 'BEGIN {n=0;} {n++;} END {print "Lines = " n;}'

Creating a patch file

To create a patch file between two directories, copy the directories such that they each lie in directories called 'old' and 'new' within a common directory.

  • mydiffs/old/myoldversion
  • mydiffs/new/mynewversion

Create the patch file by changing to the 'mydiffs' directory and execute the following

  • diff -wNaur old new > mypatchfile

options

  • -w - Ignore all white space
  • -N - Treat absent files as empty
  • -a - Treat all files as text
  • -u - Output default 3 lines of unified context
  • -r - Recursively compare subdirectories

The patch is applied by the recipient changing to their equivalent of the 'myoldversion' directory and executing the following

  • patch -p1 < mypatchfile

The -p1 option strips off the first directory from the filenames in the batch file, i.e. 'old' and 'new'.

MD5 Checksum

Computes an MD5 message digest.

A simple mechanism to reduce the possiblity that a file has been tampered with or otherwise corrupted.

  • md5sum myfile

Setting the backspace key

Type the following, but press the backspace key (or whichever you wish to use) in place of [key]. Thereafter that key will perform a backspace.

  • stty erase [key]

Set terminal editing mode

This works under Solaris, at least.

Change the shell to the korn shell, then you can change the edit keys to vi or emacs.

  • ksh
  • set -o emacs

Running X applications

Method 1

From DOS shell

  • C:\cygwin\usr\X11R6\bin\startxwin.bat
  • telnet remoteipaddress
  • execute the application in the telnet session

Method 2

From DOS shell

  • C:\cygwin\usr\X11R6\bin\startxwin.bat
  • ssh -l loginid -X _remoteipaddress (you may not need the -X parameter)
  • execute the application in the ssh session

Method 3

  • startx
  • xhost+ Warning This allows anyone to connect to the X server!
  • DISPLAY=myipaddress:0
  • export DISPLAY
  • telnet remoteipaddress
  • execute the application

-- Frank Dean - 2 July 2003

Related Topics: CygwinPostgreSQL, LinuxHintsAndTips, AwkUtils