Guest User

macOS Sequoia /etc/zshrc_Apple_Terminal

a guest
Jun 24th, 2025
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.36 KB | Source Code | 0 0
  1. # Zsh support for Terminal.
  2.  
  3.  
  4. # Working Directory
  5. #
  6. # Tell the terminal about the current working directory at each prompt.
  7. #
  8. # Terminal uses this to display the directory in the window title bar
  9. # and tab bar, and for behaviors including creating a new terminal with
  10. # the same working directory and restoring the working directory when
  11. # restoring a terminal for Resume. See Terminal > Preferences for
  12. # additional information.
  13.  
  14. if [ -z "$INSIDE_EMACS" ]; then
  15.  
  16.     update_terminal_cwd() {
  17.     # Identify the directory using a "file:" scheme URL, including
  18.     # the host name to disambiguate local vs. remote paths.
  19.  
  20.     # Percent-encode the pathname.
  21.     local url_path=''
  22.     {
  23.         # Use LC_CTYPE=C to process text byte-by-byte and
  24.         # LC_COLLATE=C to compare byte-for-byte. Ensure that
  25.         # LC_ALL and LANG are not set so they don't interfere.
  26.         local i ch hexch LC_CTYPE=C LC_COLLATE=C LC_ALL= LANG=
  27.         for ((i = 1; i <= ${#PWD}; ++i)); do
  28.         ch="$PWD[i]"
  29.         if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
  30.             url_path+="$ch"
  31.         else
  32.             printf -v hexch "%02X" "'$ch"
  33.             url_path+="%$hexch"
  34.         fi
  35.         done
  36.     }
  37.  
  38.     printf '\e]7;%s\a' "file://$HOST$url_path"
  39.     }
  40.  
  41.     # Register the function so it is called at each prompt.
  42.     autoload -Uz add-zsh-hook
  43.     add-zsh-hook precmd update_terminal_cwd
  44. fi
  45.  
  46.  
  47. # Resume Support: Save/Restore Shell State
  48. #
  49. # Terminal assigns each terminal session a unique identifier and
  50. # communicates it via the TERM_SESSION_ID environment variable so that
  51. # programs running in a terminal can save/restore application-specific
  52. # state when quitting and restarting Terminal with Resume enabled.
  53. #
  54. # The following code defines a shell save/restore mechanism. Users can
  55. # add custom state by defining a `shell_session_save_user_state` function
  56. # or an array of functions `shell_session_save_user_state_functions` that
  57. # write restoration commands to the session state file at exit. The first
  58. # argument of the function is the pathname of the session state file in
  59. # which to store state. e.g., to save a variable:
  60. #
  61. #   shell_session_save_user_state() { echo MY_VAR="'$MY_VAR'" >> "$1"; }
  62. #
  63. # or:
  64. #
  65. #   save_my_var() { echo MY_VAR="'$MY_VAR'" >> "$1"; }
  66. #   shell_session_save_user_state_functions+=(save_my_var)
  67. #
  68. # During shell startup the session file is executed and then deleted.
  69. # You may save/restore arbitrarily complex/large state by writing it
  70. # to some other file(s) and writing command(s) to the state file that
  71. # restore that data. You should typically use the TERM_SESSION_ID
  72. # as part of your file or directory names.
  73. #
  74. # The default behavior arranges to save and restore the shell command
  75. # history independently for each restored terminal session. It also
  76. # merges commands into the global history for new sessions. Because of
  77. # this it is recommended that you set HISTSIZE and SAVEHIST to larger
  78. # values.
  79. #
  80. # You may disable this behavior and share a single history by setting
  81. # SHELL_SESSION_HISTORY to 0. The shell options INC_APPEND_HISTORY,
  82. # INC_APPEND_HISTORY_TIME and SHARE_HISTORY are used to share new
  83. # commands among running shells; therefore, if any of these is enabled,
  84. # per-session history is disabled by default. You may explicitly enable
  85. # it by setting SHELL_SESSION_HISTORY to 1.
  86. #
  87. # Note that this uses the precmd hook to enable per-session history the
  88. # first time for each new session; if that doesn't run, the per-session
  89. # history won't take effect until the first restore.
  90. #
  91. # The save/restore mechanism as a whole can be disabled by setting an
  92. # environment variable (typically in `${ZDOTDIR:-$HOME}/.zshenv`):
  93. #
  94. #   SHELL_SESSIONS_DISABLE=1
  95.  
  96. if [ ${SHELL_SESSION_DID_INIT:-0} -eq 0 ] && [ -n "$TERM_SESSION_ID" ] && [ ${SHELL_SESSIONS_DISABLE:-0} -eq 0 ]; then
  97.  
  98.     # Do not perform this setup more than once.
  99.     SHELL_SESSION_DID_INIT=1
  100.  
  101.     # Set up the session directory/file.
  102.     SHELL_SESSION_DIR="${ZDOTDIR:-$HOME}/.zsh_sessions"
  103.     SHELL_SESSION_FILE="$SHELL_SESSION_DIR/$TERM_SESSION_ID.session"
  104.     mkdir -m 700 -p "$SHELL_SESSION_DIR"
  105.  
  106.     #
  107.     # Restore previous session state.
  108.     #
  109.  
  110.     if [ -r "$SHELL_SESSION_FILE" ]; then
  111.     . "$SHELL_SESSION_FILE"
  112.     /bin/rm "$SHELL_SESSION_FILE"
  113.     fi
  114.  
  115.     #
  116.     # Note: Use absolute paths to invoke commands in the exit code and
  117.     # anything else that runs after user startup files, because the
  118.     # search path may have been modified.
  119.     #
  120.  
  121.     #
  122.     # Arrange for per-session shell command history.
  123.     #
  124.  
  125.     shell_session_history_allowed() {
  126.     # Return whether per-session history should be enabled.
  127.     if [ -n "$HISTFILE" ]; then
  128.         # If this defaults to off, leave it unset so that we can
  129.         # check again later. If it defaults to on, make it stick.
  130.         local allowed=0
  131.         if [[ -o INC_APPEND_HISTORY ]] || [[ -o INC_APPEND_HISTORY_TIME ]] || [[ -o SHARE_HISTORY ]]; then
  132.         allowed=${SHELL_SESSION_HISTORY:-0}
  133.         else
  134.         allowed=${SHELL_SESSION_HISTORY:=1}
  135.         fi
  136.         if [ $allowed -eq 1 ]; then
  137.         return 0
  138.         fi
  139.     fi
  140.     return 1
  141.     }
  142.    
  143.     if [ ${SHELL_SESSION_HISTORY:-1} -eq 1 ]; then
  144.     SHELL_SESSION_HISTFILE="$SHELL_SESSION_DIR/$TERM_SESSION_ID.history"
  145.     SHELL_SESSION_HISTFILE_NEW="$SHELL_SESSION_DIR/$TERM_SESSION_ID.historynew"
  146.     SHELL_SESSION_HISTFILE_SHARED="$HISTFILE"
  147.  
  148.     shell_session_history_enable() {
  149.         (umask 077; /usr/bin/touch "$SHELL_SESSION_HISTFILE_NEW")
  150.         HISTFILE="$SHELL_SESSION_HISTFILE_NEW"
  151.         SHELL_SESSION_HISTORY=1
  152.     }
  153.  
  154.     # If the session history already exists and isn't empty, start
  155.     # using it now; otherwise, we'll use the shared history until
  156.     # we've determined whether users have enabled/disabled this.
  157.     if [ -s "$SHELL_SESSION_HISTFILE" ]; then
  158.         fc -R "$SHELL_SESSION_HISTFILE"
  159.         shell_session_history_enable
  160.     else
  161.         # At the first prompt, check whether per-session history should
  162.         # be enabled. Delaying until after user scripts have run allows
  163.         # users to opt in or out. If this doesn't get executed (probably
  164.         # because a user script inadvertently removed the hook), we'll
  165.         # check at shell exit; that works, but doesn't start the per-
  166.         # session history until the first restore.
  167.  
  168.         shell_session_history_check() {
  169.         if [ ${SHELL_SESSION_DID_HISTORY_CHECK:-0} -eq 0 ]; then
  170.             SHELL_SESSION_DID_HISTORY_CHECK=1
  171.             shell_session_history_allowed && shell_session_history_enable
  172.             # Uninstall this check.
  173.             autoload -Uz add-zsh-hook
  174.             add-zsh-hook -d precmd shell_session_history_check
  175.         fi
  176.         }
  177.         autoload -Uz add-zsh-hook
  178.         add-zsh-hook precmd shell_session_history_check
  179.     fi
  180.  
  181.     shell_session_save_history() {
  182.         shell_session_history_enable
  183.        
  184.         # Save new history to an intermediate file so we can copy it.
  185.         fc -AI
  186.        
  187.         # If the session history doesn't exist yet, copy the shared history.
  188.         if [ -f "$SHELL_SESSION_HISTFILE_SHARED" ] && [ ! -s "$SHELL_SESSION_HISTFILE" ]; then
  189.         echo -ne '\n...copying shared history...' >&2
  190.         (umask 077; /bin/cp "$SHELL_SESSION_HISTFILE_SHARED" "$SHELL_SESSION_HISTFILE")
  191.         fi
  192.        
  193.         # Save new history to the per-session and shared files.
  194.         echo -ne '\n...saving history...' >&2
  195.         (umask 077; /bin/cat "$SHELL_SESSION_HISTFILE_NEW" >> "$SHELL_SESSION_HISTFILE_SHARED")
  196.         (umask 077; /bin/cat "$SHELL_SESSION_HISTFILE_NEW" >> "$SHELL_SESSION_HISTFILE")
  197.         /bin/rm "$SHELL_SESSION_HISTFILE_NEW"
  198.        
  199.         # If there is a history file size limit, apply it to the files.
  200.         if [ -n "$SAVEHIST" ]; then
  201.         echo -n 'truncating history files...' >&2
  202.         fc -p "$SHELL_SESSION_HISTFILE_SHARED" && fc -P
  203.         fc -p "$SHELL_SESSION_HISTFILE" && fc -P
  204.         fi
  205.         echo -ne '\n...' >&2
  206.     }
  207.     fi
  208.  
  209.     #
  210.     # Arrange to save session state when exiting the shell.
  211.     #
  212.  
  213.     shell_session_save() {
  214.     # Save the current state.
  215.     if [ -n "$SHELL_SESSION_FILE" ]; then
  216.         echo -ne '\nSaving session...' >&2
  217.         (umask 077; echo 'echo Restored session: "$(/bin/date -r '$(/bin/date +%s)')"' >| "$SHELL_SESSION_FILE")
  218.        
  219.         # Call user-supplied hook functions to let them save state.
  220.         whence shell_session_save_user_state >/dev/null && shell_session_save_user_state "$SHELL_SESSION_FILE"
  221.         local f
  222.         for f in $shell_session_save_user_state_functions; do
  223.         $f "$SHELL_SESSION_FILE"
  224.         done
  225.        
  226.         shell_session_history_allowed && shell_session_save_history
  227.         echo 'completed.' >&2
  228.     fi
  229.     }
  230.  
  231.     # Delete old session files. (Not more than once a day.)
  232.     SHELL_SESSION_TIMESTAMP_FILE="$SHELL_SESSION_DIR/_expiration_check_timestamp"
  233.     shell_session_delete_expired() {
  234.     if [ ! -e "$SHELL_SESSION_TIMESTAMP_FILE" ] || [ -z "$(/usr/bin/find "$SHELL_SESSION_TIMESTAMP_FILE" -mtime -1d)" ]; then
  235.         local expiration_lock_file="$SHELL_SESSION_DIR/_expiration_lockfile"
  236.         if /usr/bin/shlock -f "$expiration_lock_file" -p $$; then
  237.         echo -n 'Deleting expired sessions...' >&2
  238.         local delete_count=$(/usr/bin/find "$SHELL_SESSION_DIR" -type f -mtime +2w -print -delete | /usr/bin/wc -l)
  239.         [ "$delete_count" -gt 0 ] && echo $delete_count' completed.' >&2 || echo 'none found.' >&2
  240.         (umask 077; /usr/bin/touch "$SHELL_SESSION_TIMESTAMP_FILE")
  241.         /bin/rm "$expiration_lock_file"
  242.         fi
  243.     fi
  244.     }
  245.    
  246.     # Update saved session state when exiting.
  247.     shell_session_update() {
  248.     shell_session_save && shell_session_delete_expired
  249.     }
  250.     autoload -Uz add-zsh-hook
  251.     add-zsh-hook zshexit shell_session_update
  252. fi
  253.  
Advertisement
Add Comment
Please, Sign In to add comment