Home/Guides/How to set up zsh on a remote Mac mini: config files, prompt and history

How to set up zsh on a remote Mac mini: config files, prompt and history

Updated · airankrush

What this guide covers

When you finish, the terminal prompt shows which machine you are on, PATH is correct in both interactive and background use, and a copy of your config sits in your own repository. You need: basic terminal skills.

Steps

  1. Confirm the default shell and do not bother replacing it

    echo $SHELL should print /bin/zsh, the macOS default. Keep it. The zsh that ships with the system is maintained through system updates and does not depend on anything you install. If you switch the login shell to the Homebrew version and Homebrew then breaks, you cannot open a working terminal on the remote machine at all, and sorting that out is far more trouble than on your own computer.

  2. What each of the two files is for

    ~/.zprofile is read once when a login shell starts. It suits environment settings such as PATH, and the Homebrew shellenv line goes here. ~/.zshrc is read every time an interactive terminal opens. It suits aliases, the prompt, completion and version manager initialization. The system does not create either file for you by default, so touch one if it is missing.

  3. Remember: background jobs do not read .zshrc

    Scheduled jobs, jobs that start at login and scripts launched by other programs are not interactive terminals and do not read ~/.zshrc. On a remote machine, unattended jobs like these are the bulk of the work. So put any PATH settings that background jobs also need in ~/.zprofile and have the jobs start through zsh -lc, or simply write full command paths in the job. Also avoid putting commands in .zshrc that print text or wait for input, because they interfere with whatever is calling the shell.

  4. Make the prompt say this is the remote machine

    When the local and remote terminals look the same, it is easy to type a delete or push command in the wrong place. Add a line to ~/.zshrc that shows the username and hostname and sets them apart with color:

    PROMPT='%F{yellow}[remote]%f %n@%m %1~ %# '

    %n is the username, %m is the hostname, and %1~ is the name of the current directory. Do not change the system's computer name for the sake of appearance; the platform uses the machine name to identify the device.

  5. History: long enough, but with no secrets in it

    On a remote machine you come back only every few days, so the history is your working notes. Make it larger and share it across terminals:

    HISTSIZE=50000
    SAVEHIST=50000
    setopt SHARE_HISTORY HIST_IGNORE_SPACE

    HIST_IGNORE_SPACE means a command typed with a leading space is not recorded in history, which helps when a command contains a secret. Better still, do not type secrets on the command line at all.

  6. Keep plugins and themes minimal

    Prompt themes and plugin frameworks pull scripts from the internet, and those scripts run every time a terminal opens. Install only ones whose source is clear and whose behavior you understand. If startup becomes noticeably slow, measure it with time zsh -lic exit and comment items out one at a time to find the cause. A fancy prompt has an extra cost in a remote desktop: character art and a frequently refreshing status bar add on-screen changes.

  7. Put the config in your own repository

    After the rental ends the machine is reclaimed and reinstalled, and the .zshrc, .zprofile, .tmux.conf and .gitconfig you tuned all disappear. Create a private config repository and put them in it. On the next machine, clone it and create a few symlinks, and you are back where you were. Before committing, check that no secrets or tokens are hard-coded in the files.

FAQ

How do I apply the config after changing it?

source ~/.zshrc reloads it in the current terminal, or just open a new window. If you changed ~/.zprofile, a new window is the safer choice. Old shells inside tmux sessions that are already running do not update automatically.

I broke .zshrc and the terminal shows an error as soon as it opens. What now?

Open the .zshrc in your user directory with Finder or a text editor (press Command+Shift+. in Finder to show hidden files) and fix the offending line. This only affects your own account, and the machine does not need a restart.

Can I change the global shell config under /etc?

There is no need. Your account-level config files already cover everything you need. Leave the system-level files to the system and the platform, in the state they were delivered in.

Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support

Back to home · All guides