142

Is there a way to make files opened for editing in the terminal open in Textedit instead?

For example, where a command might open a file for editing (like git commit), instead of opening that file in vim or emacs, it would open in Textedit (or perhaps another text editing application of your choosing, such as Coda or Sublime).

And as a bonus question, is there any way to specifically configure git to automatically open the file created after running git commit in an editor from the applications directory?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Diogenes
  • 2,697
  • 3
  • 26
  • 27

8 Answers8

173

Most programs will check the $EDITOR environment variable, so you can set that to the path of TextEdit in your bashrc. Git will use this as well.

How to do this:

  • Add the following to your ~/.bashrc file:
    export EDITOR="/Applications/TextEdit.app/Contents/MacOS/TextEdit"
  • or just type the following command into your Terminal:
    echo "export EDITOR=\"/Applications/TextEdit.app/Contents/MacOS/TextEdit\"" >> ~/.bashrc

If you are using zsh, use ~/.zshrc instead of ~/.bashrc.

Community
  • 1
  • 1
alternative
  • 12,703
  • 5
  • 41
  • 41
  • 22
    Perfect, this does it. I also ran this command to make sure git works the way I would expect it to. This `git config --global --unset-all core.editor` then `git config --global --add core.editor "open -W -n"`. – Diogenes Jul 04 '11 at 20:27
  • Are you using a different shell? – alternative Apr 12 '13 at 01:22
  • 6
    in my case, I am using `zsh` so obviously I need to update the `.zshrc` config file. – Malloc Oct 08 '13 at 15:32
  • 3
    use quotes if the path to your editor has spaces, e.g. `"echo "export EDITOR=\"/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl\"" >> ~/.bashrc"` And you may have to reload your .bashrc with: `source ~/.bashrc` – Mars Redwyne Oct 10 '13 at 19:08
  • Thanks, I've set nano editor as default by this. Here's how to add it to OSX: [http://hints.macworld.com/article.php?story=20021017065800302](http://hints.macworld.com/article.php?story=20021017065800302) – Micer Nov 05 '13 at 00:09
  • Mars Redwyne, I tried the quotation marks for spaces (export EDITOR=/Applications/Sublime\"Text.app/Contents/MacOS/Sublime\"Text), as well as just a slash with the space (export EDITOR=/Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text), but neither worked, even after running the source command. Am I doing something wrong? – Michelle Glauser Jun 13 '14 at 07:49
  • @Michelle Glauser your quotes should go like EDITOR="/Applications/.../Sublime Text" or whatever. Across the entire EDITOR export, not just something inside it. (A whole month late on this response... oops) – alternative Jul 14 '14 at 14:01
48

For anyone coming here in 2018 using iTerm:

  • go to iTerm -> Preferences -> Profiles -> Advanced -> Semantic History
  • from the dropdown, choose Open with Editor and from the right dropdown choose your editor of choice

For anyone coming here in 2022 and using iTerm2:

iTerm2 moved some things around so now it's found under

  • iTerm -> Settings -> Preferences -> Profiles -> Advanced -> Semantic History
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
32

Use git config --global core.editor mate -w or git config --global core.editor open as @dmckee suggests in the comments.

Reference: http://git-scm.com/docs/git-config

Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • 4
    ...and setting it to use `open` means that you automatically get the finder default. So changes in the finder are propagated naturally. – dmckee --- ex-moderator kitten Aug 22 '10 at 01:14
  • 4
    `open` uses Launch Services to determine the file type, mostly based on its extension. If the file has an unknown extension, it'll fail to open the file. Generally, your CLI editor should be something that assumes text files... – Gordon Davisson Aug 22 '10 at 19:39
  • Some (very) explicit instructions would be very helpful...the reference is extremely terse. – Diogenes Nov 01 '10 at 04:54
  • 2
    @Diogenes: See my answer on a different post if you still need more explicit help: http://stackoverflow.com/questions/6435246/trouble-on-setting-the-git-core-editor/6435291#6435291 – X-Istence Jun 22 '11 at 05:49
  • 4
    Better be `git config --global core.editor "open -W"` (otherwise you'll get a `Aborting commit due to empty commit message.` error). – rsenna Jun 26 '13 at 13:16
  • I agree with @Gordon Davisson. Setting your editor to something that is not a text editor is incorrect. – alternative Nov 05 '13 at 14:02
21

For OS X and Sublime Text

Make subl available.

Put this in ~/.bash_profile

[[ -s ~/.bashrc ]] && source ~/.bashrc

Put this in ~/.bashrc

export EDITOR=subl
Community
  • 1
  • 1
Kirk Strobeck
  • 17,984
  • 20
  • 75
  • 114
  • I found that this does not work correctly with `chsh`. The file opens, but any changes are lost. I had to use `chsh -s /bin/zsh` to switch shells manually. – Cryptc Dec 30 '18 at 23:59
6

Set your editor to point to this program:

/Applications/TextEdit.app/Contents/MacOS/TextEdit

With SVN, you should set SVN_EDITOR environment variable to:

$ export SVN_EDITOR=/Applications/TextEdit.app/Contents/MacOS/TextEdit

And then, when you try committing something, TextEdit will launch.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
2

For Sublime Text 3:

defaults write com.apple.LaunchServices LSHandlers -array-add '{LSHandlerContentType=public.plain-text;LSHandlerRoleAll=com.sublimetext.3;}'

See Set TextMate as the default text editor on Mac OS X for details.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KFunk
  • 2,956
  • 22
  • 33
0

If you want the editor to work with git operations, setting the $EDITOR environment variable may not be enough, at least not in the case of Sublime - e.g. if you want to rebase, it will just say that the rebase was successful, but you won't have a chance to edit the file in any way, git will just close it straight away:

git rebase -i HEAD~
Successfully rebased and updated refs/heads/master.

If you want Sublime to work correctly with git, you should configure it using:

git config --global core.editor "sublime -n -w"

I came here looking for this and found the solution in this gist on github.

mrapacz
  • 889
  • 8
  • 22
0

make Sublime Text 3 your default text editor: (Restart required)

defaults write com.apple.LaunchServices LSHandlers -array-add "{LSHandlerContentType=public.plain-text;LSHandlerRoleAll=com.sublimetext.3;}"

make sublime then your default git text editor git config --global core.editor "subl -W"