Monday, March 23, 2009

Cygwin .bashrc goodness

Well, it's been a while since I wrote a blagoweb entry; the new job's been keeping me busy. But then Jon Micklos had to go and write an entry about how to add an ssh:// protocol to the Windows registry, and clearly I can't let him be the only recent Purdue grad in the PNW providing Windows tips on his interblag. Or something. Anyway.

For various reasons, I've found myself using Cygwin rather heavily of late. And by "rather heavily", I mean "eight hours a day". So I've hacked up some Cygwin-specific .bashrc snippets that make life a little easier. Before that, though, a recommendation: don't use Cygwin under cmd.exe. It works, but let's face it, cmd.exe just isn't a very good terminal emulator. Copy/pasting is painful. It's rather slow. And it doesn't play nice with a lot of Cygwin programs. PuTTY is a much better terminal emulator, and there's a hacked-up version here that lets you use it with Cygwin.

The problem now is that a lot of the Windows command-line tools don't play nice under PuTTY. They expect a cmd.exe terminal. And sometimes you might want to run a .cmd or .bat file; there may be some shebang magic you can do, but what I've done is add this to my .bashrc:

function winsh() {
if [ "x${1}" != "x" ]; then
P=${1}
else
P=.
fi
cmd /c start cmd /k "cd `cygpath -aw ${P}`"
}
This will open a new cmd.exe window at the specified directory, or the current directory if you didn't specify one. If you just run cmd, it will run inside the PuTTY window; cmd /c start cmd is the magic that opens up a new cmd.exe window, and the /k option is to send it to the directory of your choice.

The other snippet I put together is based on a useful utility available at OS X's command-line called open, which basically acts like you double-clicked on its argument (if you specify a directory, it opens it in Finder). I just wrote a Windows-equivalent:

function open() {
explorer `cygpath -aw "$1"`
}

With this you can do open /path/to/my.doc and it'll open up in Word just as if you'd double-clicked it.

That's all for today. I'll try to update more often. Until next time, adios!

6 comments:

Murray Barton said...

I'm a regular unix head so the following might only be of use to refugee's from 'nix but any-way I find the following aliases make Cygwin more useful-
alias route='/c/WINDOWS/system32/route.exe'
alias nslookup='/c/WINDOWS/system32/nslookup.exe'
alias ifconfig='/c/WINDOWS/system32/ipconfig.exe'
alias traceroute='/c/WINDOWS/system32/tracert.exe'
alias netstat='/c/WINDOWS/system32/netstat.exe'

Also I find rxvt a fine terminal under Cygwin you might like to give it a go.

aacharya said...

rxvt requires X, no? How is Cygwin's X these days? Is it rootless? Does it integrate as well as, say, X under OSX?

Murray Barton said...

Nope rxvt doesn't require X.

X under cygwin can be rootless if you prefer and it was pretty good last time I used it, I couldn't compare with OSX though I've never used a Mac...

Phil said...

Use the built-in cygstart command, just like your open function.

Warren Young said...

Re: PuTTY as Cygwin terminal. A few months ago, someone forked PuTTY, creating a package that's only useful as a Cygwin terminal, stripped of all the other stuff that's irrelevant in that environment. It's called MinTTY, and is part of the Cygwin distribution now. It even sets up a shortcut for you on startup, so you don't have to hack the cygwin.bat file to use it instead of cmd.exe. I've been using it exclusively for months now, with no problems.

Re: your winsh script. It's written as though you weren't sure if you'd be running on V7 Unix or not. :) Since you know you will have a decent shell, the second line can be much simpler:

if [ -n "$1" ]; then

Oh, go ahead, use curly braces around the 1 if it makes you happy. :)

Re: your use of 'explorer'. It reminds me of one of my favorite tricks: at the shell prompt, say "explorer ." to open a Windows Explorer window in the current directory. The command line isn't always best...

Re: X and rootless issues. Yes, it's rootless, and it can be all but identical to the OS X experience. There's a built-in window manager that uses Windows native windows, but I haven't figured out how to use it yet. By default, it uses twm, barf. I prefer to use WindowMaker, with a third-party theme that works well with Vista. To get that, you write your own .xinitrc file in your home directory. Here's mine:

xhost +
wmaker &
exec xterm

The first line lets me run X programs on remote Linux machines that display locally. I log into the remote machine with ssh, using the X11 forwarding feature of ssh. This command opens the permissions up so X programs on the remote machine can connect to my local X server. This command opens X up wide to the world, so if security matters to you, RTFM for xhost and construct a less permissive command.

The second line runs WindowMaker. (The default xinitrc script runs twm instead.) The Cygwin port of WindowMaker works very well. Since you like OS X, WindowMaker should feel a lot better to you than twm, since it's a clone of NeXTStep's window manager, the predecessor to Aqua.

The last line starts an xterm, so that you have a command window to use immediately, since you probably started X by saying "startx" from your regular Bash prompt window, which will be stuck running startx until you quit X.

stine said...

One of the Windows commands that I have found doesn't work with cygwin is "ipconfig /flushdns", although that may have changed in the latest release of cygwin (i downloaded it today)

Does anyone have a reference for how to return Vim to a non-Emacs state? Or is there a replacement for Vim that is true to sysVr4 vi ????