Friday, September 7, 2007

Fixing up windows pathnames to use in cygwin

I use emacs within Cygwin, and often I want to paste a pathname from an explorer window into emacs and use it there. Unfortunately you have to edit it by hand before it will work.

So for example a path like:

c:\code\haskell\cats\monkeys

Needs to be converted to:

/cygdrive/c/code/haskell/cats/monkeys

The following interactive elisp function does that with the currently selected text:



(defun win32-to-cygwin-path()
"Converts a win32 path into a cygwin happy one"
(interactive)
(save-excursion
(save-restriction
(narrow-to-region (point) (mark))
(goto-char (point-min))
(insert "/cygdrive/")
(goto-char (point-min))
(while (search-forward ":" nil t)
(replace-match "" nil t))
(while (search-forward "\\" nil t)
(replace-match "/" nil t)))))


Nothing much clever going on here, just two search and replaces for the slashes and to removed the colon, and I insert the cygdrive prefix.

3 comments:

Bruce said...
This comment has been removed by the author.
Bruce said...

Cygwin has a tool that you might want to try, `cygpath`. See the online docs for cygpath.

J said...

Thanks Bruce

Any time I post publicly about something I've done in emacs I find that someone has done it before me, so this is no surprise. I'll check it out.

Justin