Saturday, April 9, 2011

Configuring text editor in cygwin bash

In this post I will show you how to edit files with your favorite windows text editor from the cygwin bash command line. My favorite editor is notepad++ and to edit a file from cygwin, I type: ed myFile.txt

Here is what you need to do:
  1. Open ~/.bashrc. This should be under CIGWIN_DIR/home/YOUR_USERNAME/.bashrc

  2. Create a variable holding the path of your favorite editor. In my case, I am calling it EDITOR. Change the path inside of double quotes to the windows explorer path. To configure notepad++ I use:
    EDITOR=`cygpath -a "C:\Program Files (x86)\Notepad++\notepad++.exe"`
  3. Add the following function to .bashrc
    ed () {
        FILE=`cygpath -w -a $@`
        echo Editing: $FILE
        `"$EDITOR" $FILE`&
  4. Start a new bash session or run source ~/.bashrc to reload your bashrc. 

You should now be able to edit files just by typing the function name from step 3, in this case it is ed (for edit).
$ cd /tmp
$ ed test.txt
Editing: C:\cygwin\tmp\test.txt
[1] 6440

No comments:

Post a Comment