Posts Tagged ‘cygwin

11
Dec
14

Opening a cmd window in the current working directory from cygwin

With the newer version of cygwin I am running, there are a few flaws. These mainly have to do with how it interacts with printing and user input from windows executables.

e.g.

  • The windows python prompt doesn’t display. See here for more details.
  • Using mercurial, it can’t prompt me for a password. It comes up as abort: http authorization required

There are a few little niggly things like this which makes it handy to have the windows console available at your fingertips. You can launch cmd.exe from within cygwin, but that doesn’t solve the problem as you are still using the same input/output from the terminal. So I wrote a very simple script which will launch cmd in the current working directory.

Here it is:

#!/bin/sh
# Written by: DGC

#==============================================================================
usage() {
  cat <<EOF
Usage: $(basename $0) <options>;

Opens a windows command window in the current working directory.

Options:
-h                 Display this message.
EOF
  exit
}

#==============================================================================
# Main
while getopts ":h?" option
do
  case $option in
    h)
      usage
      ;;
    \?)
      echo -e "Invalid option: -$OPTARG \n"
      usage
      ;;
  esac
done

cygstart 'C:\Windows\System32\cmd.exe'

This is slightly overkill for essentially just cygstart 'C:\Windows\System32\cmd.exe', but I like to include a help with all my scripts.

It was helpful to me, so I thought I’d share it.

Advertisements
20
May
13

Keeping History Using the cd Command

If you use shell scripting a lot from the command line, this will probably improve your experience of it. This is for users of linux/mac command line or of cygwin on windows. Anywhere you can use the bash cd command.

Firstly (if you didn’t know) cd remembers the last directory you went to. To access this use;

cd -

This will change to the previous directory you were in. If you want to supercharge this you can add the following to your .bashrc/.profile/… customization file.

# petar marinov, http:/geocities.com/h2428, this is public domain
cd_func ()
{
  local x2 the_new_dir adir index
  local -i cnt

  if [[ $1 ==  "--" ]]; then
    dirs -v
    return 0
  fi

  the_new_dir=$1
  [[ -z $1 ]] && the_new_dir=$HOME

  if [[ ${the_new_dir:0:1} == '-' ]]; then
    #
    # Extract dir N from dirs
    index=${the_new_dir:1}
    [[ -z $index ]] && index=1
    adir=$(dirs +$index)
    [[ -z $adir ]] && return 1
    the_new_dir=$adir
  fi

  #
  # '~' has to be substituted by ${HOME}
  [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"

  #
  # Now change to the new dir and add to the top of the stack
  pushd "${the_new_dir}" > /dev/null
  [[ $? -ne 0 ]] && return 1
  the_new_dir=$(pwd)

  #
  # Trim down everything beyond 11th entry
  popd -n +11 2>/dev/null 1>/dev/null

  #
  # Remove any other occurence of this dir, skipping the top of the stack
  for ((cnt=1; cnt <= 10; cnt++)); do
    x2=$(dirs +${cnt} 2>/dev/null)
    [[ $? -ne 0 ]] && return 0
    [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
    if [[ "${x2}" == "${the_new_dir}" ]]; then
      popd -n +$cnt 2>/dev/null 1>/dev/null
      cnt=cnt-1
    fi
  done

  return 0
}

alias cd=cd_func

This means that you can keep a history of the past 10 directories which you have visited and change between them.

To access this list type

cd --

Which will print a list like.

0  /e/projects/dgc/or14126h_3
1  /e/projects/dgc/or14126h_3/orthotics.dev
2  /c/Users/dgc/Dropbox/Coding
3  /e/projects/dgc
4  /e/projects/dgc/or14126h_2
5  /e/devdisk/dcm/configs
6  /e/devdisk/dcm
7  /e/devdisk
8  /e

And then

cd -5

will take you to entry 5 in the list.

To be clear I did not write this, I found it in the default cygwin .bashrc. The author of this was Petar Marinov.

I have found this very helpful, particularly when using a new python module. This is the process;

  • I would be in my working area
  • download it from pypi
  • cd to my download area
  • untar it
  • cd into it
  • run python setup.py install
  • then want to go back but cd – would get me to my download area.

This function removes that minor inconvenience and I have found it very useful.

Thanks for reading.