Modify ZShell Defaults in macOS


Making Terminal More Useful!

I’m finding myself using the Terminal quite a bit more in my job. I spent a few minutes over the past few days looking at different ways to make the default terminal layout in macOS better. While there are many plugins out there for doing this (Oh-My-Zsh), I wanted to do something a little more straightforward.

How You Change zsh Default Layouts

Armin Briegel has a great article about customizing the zsh prompt in his moving to zsh series. The basics come down to the following - make some changes to the file at ~/.zshrc and then enjoy the fruits of your labor! This file is also where you can add zsh functions (e.g. reusable pieces of code.)

My Current zsh Defaults

Here’s the contents of my ~/.zshrc file:

PROMPT='%(?.%F{green}√.%F{red}?%?)%f %B%F{240}%1~%f%b %# '

export PATH=$HOME/.gem/ruby/X.X.0/bin:$PATH
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH

movc () {
    ffmpeg -i $1 -c:v libx264 -preset fast "${1%.mov}"-opt.mov
    newname="${1%.mov}"-opt.mov
    origsize=`du -k "$1" | cut -f1`
    newsize=`du -k $newname | cut -f1`

    echo  -e '\n'$1   $origsize"KB"
    echo  $newname   $newsize"KB"
}

mp4c () {
  ffmpeg -i $1 -c:v libx264 -preset fast "${1%.mp4}"-opt.mp4
  newname="${1%.mp4}"-opt.mp4
  origsize=`du -k "$1" | cut -f1`
  newsize=`du -k $newname | cut -f1`

  echo  -e '\n'$1   $origsize"KB"
  echo  $newname   $newsize"KB"
}

mpgc () {
    ffmpeg -i $1 -c:v libx264 -preset fast "${1%.mpg}"-opt.mpg
    newname="${1%.mpg}"-opt.mpg
    origsize=`du -k "$1" | cut -f1`
    newsize=`du -k $newname | cut -f1`

    echo  -e '\n'$1   $origsize"KB"
    echo  $newname   $newsize"KB"
}

# Move 'up' so many directories instead of using several cd ../../, etc.
up() { 
    cd $(eval printf '../'%.0s {1..$1}) && pwd; 
}

Breaking Down the Defaults

The first line (PROMPT='%(?.%F{green}√.%F{red}?%?)%f %B%F{240}%1~%f%b %# ') was pulled straight from Armin’s Customizing the zsh prompt article. I love it because it’s generally very minimal, but when you start diving into the filesystem it starts to give you an idea of where you’re at. He’s also embedded a return-code indicator, which is handy when running long commands.

The next three export lines are probably recognizable by anyone building Jekyll websites.

The next 3 sections of code are functions. In my job, I create a number of video files that get shared via Powerpoints and/or posted to YouTube. These 3 functions are shortcut command lines to use FFmpeg and drastically reduce the file size on video content. Basically, I can take an MP4 file output from Camtasia, and run mp4c video.mp4 to kick off an FFmpeg compression that outputs that same file (reduced in size) as video-opt.mp4. Credit to jprichards for the code for this one.

The last function I found on Reddit and find myself doing this exact thing constantly. It’s going to be handy being able to simply type up 3 to change directories.


See also