MacOS Terminal – MacOS Catalina Terminal Tab Doesn’t Remember Previous Commands

command linemacosterminalzsh

I upgraded from El Capitan to Catalina recently; when I click in any terminal tab it used to toggle previous commands executed within that specific tab only, but now it will show commands for all tabs which is very annoying

Is there any setting to fix that?

Best Answer

Your question regards how the shell maintains its command history. In switching from El Capitan, to Catalina, you may have also switched your default shell from bash to zsh, and these shells have different defaults for maintaining their command history. This answer assumes your question is for altering the behavior of the zsh command history.

You have at least two choices:

  1. Restore bash as your shell:

    % chsh -s /bin/bash
    

    This is easily changed if you decide to return to zsh:

    chsh -s /bin/zsh
    
  2. Configure zsh to disable the "aggregation" of command histories see APPEND_HISTORY

    From man zshoptions:

    APPEND_HISTORY
    If this is set, zsh sessions will append their history list to the history file, rather than replace it. Thus, multiple parallel zsh sessions will all have the new entries from their history lists added to the history file, in the order that they exit. The file will still be periodically re-written to trim it when the number of lines grows 20% beyond the value specified by $SAVEHIST (see also the HIST_SAVE_BY_COPY option).

    Accordingly, if you wish to continue using zsh as your shell, you can un-set the APPEND_HISTORY option, and the command histories from your various terminals/shells will not be aggregated into a single history.

    You can check to see which options are set or unset as follows:

    % setopt
    ...
    # lists options that are set
    
    % unsetopt
    ...
    # lists options that are unset
    
    % set -o
    ...
    # lists all options w/ off/on status 
    

    NOTE: setopt only prints the options not enabled by default. The APPEND_HISTORY option is set by default - as designated by the <D> seen in the man page excerpt above.

    Now, this is a bit obtuse (IMO at least), but notice that there is an option appearing in the unsetopt output named noappendhistory. This is explained in the zsh Options documentation as follows:

    In the following list, options set by default in all emulations are marked ; those set by default only in csh, ksh, sh, or zsh emulations are marked , , , as appropriate. When listing options (by ‘setopt’, ‘unsetopt’, ‘set -o’ or ‘set +o’), those turned on by default appear in the list prefixed with ‘no’. Hence (unless KSH_OPTION_PRINT is set), ‘setopt’ shows all options whose settings are changed from the default.

    (emphasis mine)

    It comes down to this: the option to give you the behavior you want is set as follows:

    setopt noappendhistory
    

    Add this command to your ~/.zshrc file using your favorite editor. Once you've done that, source this file as follows:

    % . ~/.zshrc 
    -- OR --
    % source ~/.zshrc