Set your default editor on MacOS X

On MacOS and on Unix the default editor is in general vi1 (/usr/bin/vi). This means that when you want to call an editor from an application, vi will be called by default.

For example, suppose you are looking at a file with the command less and you want to edit the file you are looking at. Just press the key v. Less will call the default editor, allowing you to change the content of the file you are looking at.

Now let’s say you don’t like vi and you want to use a more user friendly editor. To do so you just have to define the shell variable EDITOR with the path to your favorite editor. At the shell prompt, type the line export EDITOR=/usr/bin/pico. Now your default editor has become pico. Next time, the program will call pico rather than vi.

You can save this line into your ~/.bashrc file so you won’t have to type this command every time you start a new shell.

Vi and pico are tty based editors; they run inside your Terminal.app. You can also call Cocoa based editors such as Xcode or Emacs. For that you just need to set the EDITOR variable with the full path to the binary file.

fred$ vi ~/.bashrc
# for Xcode
export EDITOR=/Developer/Applications/Xcode.app/Contents/MacOS/Xcode
# form Emacs
export EDITOR=/Applications/Emacs.app/Contents/MacOS/Emacs
~
~
:wq

Now when you are browsing a file with less and you want to edit this file, a new Emacs or Xcode window will open.

Every time an application calls the editor a new window will open. The cool thing you can do with Emacs is to use its client server feature. Instead of opening a new window, the emacsclient program will inform your running emacs to visit the file you are actually browsing.

To use this feature you need to start the server in your emacs. Edit your ~/.emacs file and add the line (start-server) somewhere near the end of your .emacs file.

fred$ vi ~.emacs
;; start emacs server
(server-start)
~
~
:wq

You need to set your EDITOR variable with the emacsclient program.

fred$ vi ~/.bashrc
#
export EDITOR=/Applications/Emacs.app/Contents/MacOS/bin/emacsclient
~
~
:wq

Now, the file will open in your existing emacs, instead of running and opening a new instance of emacs for each file.

1 On some system vi has been replaced by vim for improved vi

 

Leave a message

(Required)
(Required and not displayed)
(Optional)
obfuscated letters Enter the text shown in the image