is_mac
Am I on a Mac?When you're constantly switching between linux and MacOS it can be extremely beneficial to have a way to conditionally set shell preferences depending on your environment. I'll stick this snippet at the top of my .zshrc
file and use it in my dotfiles for conditional aliases or setups.
The setup is simple. You define a variable, AM_MAC
and assign it to 0
. Create a function that checks for the operating system type of darwin
and if it matches, set AM_MAC
to 1
. Be sure to call the function after defining it.
AM_MAC=0is_mac() { if [[ $OSTYPE == 'darwin'* ]]; then AM_MAC=1 fi}is_mac
Here's an example of conditionally setting up something homebrew specific on MacOS.
if (( AM_MAC > 0)); then source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zshfi
Here's how you can manage linux only aliases
linux_aliases() { # Custom Apt alias app="sudo apt-get" alias app-remove="sudo apt-get remove" alias app-install="sudo apt-get install" alias app-edit="sudo envedit /etc/apt/sources.list" alias app-search="apt-cache --names-only search" alias app-search-all="apt-cache search" alias app-update="sudo apt-get update && sudo apt-get upgrade" alias app-info="apt-cache showpkg"}# Only Alias apt-get if we are on linuxif ! [ AM_MAC==1 ]; then; linux_aliases; fi
repos <project>
Auto CD Into ReposIf you're like me, you usually have a fairly organized setup where you stick most/all of your projects in one folder. I've set up a function to cd into my project director and either list all the files, or if you pass an additional argument it will cd into that directory. So instead of cd ~/code/repos/<project>
I just type repos <project>
. Saving me literal seconds each day.
repos() { # Navigate to repos director and open target directory is specified if [ -z "$1" ]; then cd ~/code/repos && ls -la return fi cd ~/code/repos/$1}
mkcd
- Make and Change DirectoriesNeed I say more?
# Make and CD into directoryfunction mkcd { if [ ! -n "$1" ]; then echo "Enter a directory name" elif [ -d $1 ]; then echo "\`$1' already exists" else mkdir $1 && cd $1 fi}
lfcd
- Visually Change DirectoriesThis one requires that you have the lf file manager installed. Once that's installed this alias allows you to navigate your file system using lf
and wherever you quit you will change directories into.
# Use lf to switch directories and bind it to ctrl-olfcd () { tmp="$(mktemp)" lf -last-dir-path="$tmp" "$@" if [ -f "$tmp" ]; then dir="$(cat "$tmp")" rm -f "$tmp" [ -d "$dir" ] && [ "$dir" != "$(pwd)" ] && cd "$dir" fi}bindkey -s '^o' 'lfcd\n'
lfcode
- Visually Open VS Code from DirectoryThis one also requires that you have the lf file manager installed. Similar to the above it allows you to navigate your file system visually in the terminal and open your last location in Visual Studio Code.
# Opens Last lf Directory in VSCodelfcode () { tmp="$(mktemp)" lf -last-dir-path="$tmp" "$@" if [ -f "$tmp" ]; then dir="$(cat "$tmp")" rm -f "$tmp" [ -d "$dir" ] && [ "$dir" != "$(pwd)" ] && code "$dir" fi}bindkey -s '^[c' 'lfcode\n'