I spend an increasing amount of time in a shell terminal. This post documents my zprofile, mainly for myself so that I can reference the aliases and other bits of config that are in there. Hopefully if you are here then you’ll find it useful too.

.zprofile

The .zprofile file is a script that gets executed when you start a new login shell using the zsh shell (Z Shell). It’s one of the initialisation files that zsh reads when it starts up.

there are other initialisation files that you can hook in to, but I stick most of my stuff in the .zprofile file. Here’s a quick rundown of the sequence in which zsh reads its initialisation files:

  • .zshenv: This file is sourced on all invocations of the shell. It’s often used for setting environment variables.
  • .zprofile: Sourced only for login shells. This file is typically used for commands that should run once per user login. For example, setting up paths or starting services that should run at login could be placed here.
  • .zshrc: Sourced for interactive shells. This is the file you’d typically put all your aliases, functions, shell options, and prompt definitions in, as it’s read every time you start a new terminal session.
  • .zlogin: Also sourced only for login shells, but after .zshrc. Sometimes used to start processes that should run after the entire shell setup is complete.
  • .zlogout: Sourced when a login shell exits. This is for commands that execute at the end of your session, such as clearing temporary files or notifying you of certain events.

Whenever I make changes to my .zprofile file then I run the following command from the shell to reload it…

    | => exec zsh -l

My .zprofile file…

    #  ---------------------------------------------------------------------------
    #  
    #  This holds all of the zsh configuration.
    #
    #  Sections:
    #  1.  Env config
    #  2.  Paths
    #  3.  Aliases
    #  4.  Evals
    # 
    #  ---------------------------------------------------------------------------


    #   -------------------------------
    #   1. Env config
    #   -------------------------------

    setopt always_to_end          # When completing a word, move the cursor to the end of the word
    setopt append_history         # this is default, but set for share_history
    setopt auto_cd                # cd by typing directory name if it's not a command
    setopt auto_list              # automatically list choices on ambiguous completion
    setopt auto_menu              # automatically use menu completion
    setopt auto_pushd             # Make cd push each old directory onto the stack
    setopt completeinword         # If unset, the cursor is set to the end of the word
    setopt correct_all            # autocorrect commands
    setopt extended_glob          # treat #, ~, and ^ as part of patterns for filename generation
    setopt extended_history       # save each command's beginning timestamp and duration to the history file
    setopt glob_dots              # dot files included in regular globs
    setopt hash_list_all          # when command completion is attempted, ensure the entire  path is hashed
    setopt hist_expire_dups_first # # delete duplicates first when HISTFILE size exceeds HISTSIZE
    setopt hist_find_no_dups      # When searching history don't show results already cycled through twice
    setopt hist_ignore_dups       # Do not write events to history that are duplicates of previous events
    setopt hist_ignore_space      # remove command line from history list when first character is a space
    setopt hist_reduce_blanks     # remove superfluous blanks from history items
    setopt hist_verify            # show command with history expansion to user before running it
    setopt histignorespace        # remove commands from the history when the first character is a space
    setopt inc_append_history     # save history entries as soon as they are entered
    setopt interactivecomments    # allow use of comments in interactive code (bash-style comments)
    setopt longlistjobs           # display PID when suspending processes as well
    setopt no_beep                # silence all bells and beeps
    setopt nocaseglob             # global substitution is case insensitive
    setopt nonomatch              ## try to avoid the 'zsh: no matches found...'
    setopt noshwordsplit          # use zsh style word splitting
    setopt notify                 # report the status of backgrounds jobs immediately
    setopt numeric_glob_sort      # globs sorted numerically
    setopt prompt_subst           # allow expansion in prompts
    setopt pushd_ignore_dups      # Don't push duplicates onto the stack
    setopt share_history          # share history between different instances of the shell
    HISTFILE=${HOME}/.zsh_history
    HISTSIZE=100000
    SAVEHIST=${HISTSIZE}

    #   -------------------------------
    #   2. Paths
    #   -------------------------------
    path+=('~/development/flutter/bin')

    #   -------------------------------
    #   3. Aliases
    #   -------------------------------

    alias cp='cp -iv'                           # Preferred 'cp' implementation
    alias mv='mv -iv'                           # Preferred 'mv' implementation
    alias mkdir='mkdir -pv'                     # Preferred 'mkdir' implementation
    alias ll='ls -FGlAhp'                       # Preferred 'ls' implementation
    alias less='less -FSRXc'                    # Preferred 'less' implementation
    cd() { builtin cd "$@"; ll; }               # Always list directory contents upon 'cd'
    alias cd..='cd ../'                         # Go back 1 directory level (for fast typers)
    alias ..='cd ../'                           # Go back 1 directory level
    alias ...='cd ../../'                       # Go back 2 directory levels
    alias .3='cd ../../../'                     # Go back 3 directory levels
    alias .4='cd ../../../../'                  # Go back 4 directory levels
    alias .5='cd ../../../../../'               # Go back 5 directory levels
    alias .6='cd ../../../../../../'            # Go back 6 directory levels
    alias edit='subl'                           # edit:         Opens any file in sublime editor
    alias f='open -a Finder ./'                 # f:            Opens current directory in MacOS Finder
    alias ~="cd ~"                              # ~:            Go Home
    alias c='clear'                             # c:            Clear terminal display
    alias which='type -all'                     # which:        Find executables
    alias path='echo -e ${PATH//:/\\n}'         # path:         Echo all executable Paths
    alias show_options='shopt'                  # Show_options: display bash options settings
    alias fix_stty='stty sane'                  # fix_stty:     Restore terminal settings when screwed up
    alias cic='set completion-ignore-case On'   # cic:          Make tab-completion case-insensitive
    mcd () { mkdir -p "$1" && cd "$1"; }        # mcd:          Makes new Dir and jumps inside
    trash () { command mv "$@" ~/.Trash ; }     # trash:        Moves a file to the MacOS trash
    ql () { qlmanage -p "$*" >& /dev/null; }    # ql:           Opens any file in MacOS Quicklook Preview
    alias DT='tee ~/Desktop/terminalOut.txt'    # DT:           Pipe content to file on MacOS Desktop
    alias kill='kill -9'

    alias cpwd='pwd | tr -d "\n" | pbcopy'      # Copy the working path to clipboard
    alias cl="fc -e -|pbcopy"                   # Copy output of last command to clipboard

    alias myip='curl ip.appspot.com'            # myip:         Public facing IP Address

    #   lr:  Full Recursive Directory Listing
    #   ------------------------------------------
    alias lr='ls -R | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/   /'\'' -e '\''s/-/|/'\'' | less'

    #   mans:   Search manpage given in agument '1' for term given in argument '2' (case insensitive)
    #           displays paginated result with colored search terms and two lines surrounding each hit.            Example: mans mplayer codec
    #   --------------------------------------------------------------------
    mans () {
        man $1 | grep -iC2 --color=always $2 | less
    }


    #   -------------------------------
    #   4. Evals
    #   -------------------------------

    eval "$(/opt/homebrew/bin/brew shellenv)"

    eval "$(oh-my-posh init zsh --config ~/jandedobbeleer.omp.json)"