Friday, 2 March 2012

gedit something in OS X terminal

Trying out OS X after using Ubuntu, I often found myself typing into the terminal "gedit <something>" to do a quick edit of a file, which didn't work out of the box. If you're here and you also didn't want to give up that old habit, here's one way to get that terminal command to work again. (I got this to work with gedit 3.2.6 under OS X 10.7.3, so your mileage might vary with other setups...)

1) install the gedit app
(This can be found at their project page: http://projects.gnome.org/gedit/

Caution: if you don't know what "sudo" is, how to write shell scripts, or what file permissions are, read up on it before doing the next few steps or you could potentially screw up your system.
 
2) create a shell script named 'gedit' in a folder defined in your PATH (in my case, I just put it in /usr/bin), and have it open a file using gedit

In the terminal, create a new shell script named 'gedit' using a text editor. In this example we use the 'nano' text editor.

sudo nano /usr/bin/gedit

Type in the following script:
#!/bin/bash

open -a /Applications/gedit.app/Contents/MacOS/gedit $1


Save.


This lets you do simple "gedit something.txt" or "gedit" commands from the terminal. You can change this script as you desire for more complicated behavior.


3) make the script executable
sudo chmod +x /usr/bin/gedit


To test if this worked out, open up a file using gedit in the terminal, e.g. "gedit myfavoritefile.txt"


4) how to undo this
To undo all of this, simply delete the /usr/bin/gedit script you created and uninstall gedit.

1 comment:

  1. Hello,

    Thank you for your help. I am just wondering if it's also possible to edit this script so you can create a file with gedit as well.

    For example, typing 'gedit newfile.c' gives the following error:
    'The file /Users/me/newfile.c does not exist.'

    Thank you!

    ReplyDelete