Setting $EDITOR with some Smarts

1 minute read

My editor of choice is Emacs. However, in a pinch I can drive vim or vi just fine1. While I prefer Emacs, I can’t think of a UNIX-like operating system that ships with it by default. Most, if not all, do come with something in the vim/vi family. So, unless I’m going to do some heavy editing on the server, I usually don’t bother installing Emacs.

Most distros set vim/vi as the default editor, however I’m seeing more defaulting to something more “user friendly” like Nano or Pico. Given that, I want set $EDITOR and make sure I get my preferred editor:

export EDITOR=$(type -P emacs || type -P vim || type -P vi)

What does it do? type -P prints the full path of the file what Bash would execute. The || works as you would expect, if type returns a path, execution stops and $EDITOR is set to the value. Otherwise, the next editor is tried. If nothing is found, then $EDITOR is set to blank, which will cause the system default to be use.

I started using UNIX in an age when $EDITOR meant your line editor that would be used if your terminal didn’t support “advanced” features, namely full screen cursor control, I still also set:


Even though it’s probably meaningless.

  1. In fact once someone said to me “What do you mean you’re not a vi user? You’re navigated with hjkl!” Well, that muscle memory comes from years of playing Nethack (and Hack before it).