1

I want to create a cron job. Launching a terminal window, and entering crontab -e opens my current crontab, but opens it in the editor vi. I would prefer to use the nano editor.

How do I configure my system (Mojave) to launch nano instead of vi?

  • cron has been deprecated for launchd You should be using that instead. – Allan Feb 22 at 4:24
  • @Allan: Yep, I've used launchd, but cron is just simpler to use. Deprecated?... we'll see. The deprecation was announced in 2005 (IIRC), yet... – Seamus Feb 22 at 6:54
  • @Allan: But I do get your point. I feel that my subject line was misleading, so I've changed that. – Seamus Feb 22 at 6:59
1

The editor that is launched when you enter crontab -e at the command line is set in an environment variable. Specifically, the default editor that is launched in your terminal window is controlled by the environment variable labeled VISUAL, or $VISUAL. You can see all of the environment variables for the shell you're using by typing env at the command prompt; e.g.:

My-MacBook-15:~ seamus$ env
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/8_/s3jxzj5150s9f3d3y5k4wzdr0000gp/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.f9meUexEfE/Render
TERM_PROGRAM_VERSION=421.1
TERM_SESSION_ID=CECBC621-33BB-45FD-96E3-B414DBE99F65
USER=seamus
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.LVyDWNJZbD/Listeners
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/opt/X11/bin
PWD=/Users/seamus
LANG=en_US.UTF-8
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/seamus
LOGNAME=seamus
DISPLAY=/private/tmp/com.apple.launchd.tzdryqvBdL/org.macosforge.xquartz:0
SECURITYSESSIONID=186a7
_=/usr/bin/env  

You may note, as in the above example, that the VISUAL environment variable is not included in this list. This only means that for your current username, the VISUAL environment variable has not been changed from the system's default. You can confirm this (or check the value of the the VISUAL environment variable) in a compact manner as follows:

My-MacBook-15:~ seamus$ echo $VISUAL

My-MacBook-15:~ seamus$  

The blank line indicates that no value for $VISUAL has been set for this user, and the system default will be used.

CHANGING THE DEFAULT EDITOR FOR THE CURRENT SESSION:

To set the VISUAL environment variable so that nano is launched as the default editor for all editing tasks initiated from a terminal window for this user, simply set the environment variable as follows:

My-MacBook-15:~ seamus$ VISUAL=nano
My-MacBook-15:~ seamus$ export VISUAL  

Or, if you prefer:

My-MacBook-15:~ seamus$ export VISUAL=nano 

You can verify this setting as follows:

My-MacBook-15:~ seamus$ echo $VISUAL
nano
My-MacBook-15:~ seamus$  

And that will do it! From this point forward (until your shell or your system is re-started), any time an editing task is launched from a command in a terminal window for this user, nano will be used instead of the system default. And of course the system's default editor will be restored when the system (or the shell) is re-started. If you don't wish to perform a re-start to restore the default editor, simply specify vi as the editor:

My-MacBook-15:~ seamus$ export VISUAL=vi

CHANGING THE DEFAULT EDITOR TO PERSIST ACROSS ALL SESSIONS:

To change the default editor permanently (that is, to persist across re-starts) is also easily accomplished. Simply create a hidden file in the user's home directory (~/), and set the VISUAL environment variable in it, all accomplished conveniently in a single line:

My-MacBook-15:~ seamus$ echo export VISUAL=nano >> ~/.bash_profile 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .