How can I set the default editor as nano on my Mac?

32,847

Solution 1

Set the EDITOR and VISUAL environment variables to nano.

If you use bash on macOS, this is easiest done by editing your ~/.bash_profile file and adding the two lines

export EDITOR=nano
export VISUAL="$EDITOR"

to the bottom of the file. If the file does not exist, you may create it. If you use some other shell, modify that shell's startup files instead (e.g. ~/.zshrcforzsh`).

You should set both variables as some tools use one and others may use the other.

You will need to restart your terminal to have the changes take effect.

Solution 2

I had this same challenge when setting up my new MacBook Pro.

To elaborate more on Kusalananda's answer

To switch to your editor of choice (say nano) you will need to add the following lines to your ~/.zshrc file if your default shell is zsh or ~/.bash_profile if your default shell is bash:

export EDITOR=nano
export VISUAL="$EDITOR"

However, a simpler approach to do this will be to use the echo command to insert them into your ~/.zshrc file if your default shell is zsh:

echo 'export EDITOR=nano' >> ~/.zshrc
echo 'export VISUAL="$EDITOR"' >> ~/.zshrc

OR ~/.bashrc if your default shell is bash:

echo 'export EDITOR=nano' >> ~/.bash_profile
echo 'export VISUAL="$EDITOR"' >> ~/.bash_profile

Run the command below to activate the new configuration:

source ~/.zshrc

OR

source ~/.bash_profile

If you need to switch to other editors of choice you can replace nano with your preferred editor:

  • Vim - vim
  • Vi - vi

That's all.

I hope this helps

Solution 3

The answer suggested by @Kusalananda doesnt work with a default install of Catalina.

Apple has changed the shell from bash to zsh as default in Catalina.

You need to rename your .bash_profile as .zprofile.

Then it works.

Share:
32,847

Related videos on Youtube

g_tech
Author by

g_tech

Updated on September 18, 2022

Comments

  • g_tech
    g_tech over 1 year

    I have vim as default editor on my Mac and every time I run commands on Mac terminal, it automatically opens "vim".

    How can I set up "nano" instead and make sure the terminal will open "nano" every time is needed?

  • Ben Racicot
    Ben Racicot about 4 years
    This is a great solution! How can we get the name of other editors for this purpose?
  • Kusalananda
    Kusalananda about 4 years
    @BenRacicot The editors installed by default on macOS are nano, vi/vim, and ed (a very basic line-editor). If you install other editors for used in the shell, e.g. through Homebrew, then you would know what they are (since you installed them).
  • Ben Racicot
    Ben Racicot about 4 years
    Yes, only the common issue online is swapping text edit as default for Sublime Text 3. However, export EDITOR=Sublime Text does nothing.
  • Kusalananda
    Kusalananda about 4 years
    @BenRacicot Any command that uses e.g. $EDITOR will use it as a command. You would set it to the command that you use to launch Sublime from the command line. If the command contains spaces, then it has to be quoted.
  • Mike Eng
    Mike Eng over 3 years
    @BenRacicot this answer may help if you're trying to use a GUI application like Sublime Text stackoverflow.com/a/3539630/370407
  • Kusalananda
    Kusalananda over 2 years
    @Mint It will depend on whatever shell the user is using, not what shell is the default on the system. Note that it is possible to install various shells on macOS, including moderns bash releases. I will however update my answer to say that it depends on the shell.