Some Config Ideas for your ZSH

gokayburuc

gokayburuc.dev

Posted on August 24, 2024

Some Config Ideas for your ZSH

Introduction

ZSH shell users often aim to personalize their systems with short edits on the .zshrc file, to provide faster access and to do multiple tasks with a single and short command. Below I am sharing a few ideas from my own .zshrc configurations with you.

All you need to do is save the boilerplate codes I shared to your .zshrc file and activate them with the source ~/.zshrc command.

FFMPEG Webp file Converter

Requires ffmpeg

This function converts your .webp files into .png files.

## INFO: FFMPEG WEBP CONVERTER
convert_webp() {
  for i in *
  do

  if [[ "${i##*.}" == "webp" ]]; then

      echo "WEBP FILE => $i"

      # show filename
      filename="${i%.*}"

      # show Extension
      extension="${i##*.}"

      ## webp converter
      ffmpeg -i "$filename" "$filename.png"

    fi
  done
  }

Enter fullscreen mode Exit fullscreen mode

Usage:

$convert_webp

Enter fullscreen mode Exit fullscreen mode

Creating New Obsidian Note From Terminal

Requires pre-defined obsidian vault

To create new obsidian markdown note from your terminal you can use this function:

vn(){
  ov  # open vault
  local filename="$1"
  if [[ -z "$filename" ]]; then
    echo "Usage: vn <filename>"
    return 1

  fi
  #use your own path <filepath>
  nvim "00_inbox/${filename}.md" # enter file-name and directly start writing
}
Enter fullscreen mode Exit fullscreen mode

This function changes directory into your vault and creates a new markdown note with the given filename.

Usage:

$vn my-first-article

Enter fullscreen mode Exit fullscreen mode

Zoxide

Requires zoxide

This is the zsh config for the zoxide

# zoxide
eval "$(zoxide init zsh)"
Enter fullscreen mode Exit fullscreen mode

Now you can use fast folder navigations only putting the prefix z and you can discover the world of zoxide.

Usage:

# cd into $HOME/Documents/vaults/  directly
z Doc/vau
Enter fullscreen mode Exit fullscreen mode

Fast Folder Access

Sometimes i need shortcuts to access my folders directly. So i declared them all.

## foldershortcuts
alias ob="cd /$HOME/Documents/workspace/static_website/hugo/gokayburuc_weblog/"
alias oc="cd /$HOME/.config/nvim/"
alias ok="cd /$HOME/.config/kitty/"
alias od="cd /$HOME/Downloads/"
alias om="cd /$HOME/Documents/workspace/static_website/mdbook/golang_cookbook/"
alias ov="cd /$HOME/Documents/vaults/secondbrain/"
alias ow="cd /$HOME/Documents/workspace"
Enter fullscreen mode Exit fullscreen mode

Those are shortcut to my directories. Frequiently used paths are now accessible with only 2 letters from commandline.

Usage:

# goes into directly into the $HOME/Documents/vaults/secondbrain/
$ov
Enter fullscreen mode Exit fullscreen mode

Tmux Shortcuts

# tmux shortcuts
alias tconf="nvim ~/.tmux.conf"
alias t="tmux"
alias tn="tmux new"
alias ta="tmux attach-session"
alias trw="tmux rename-window"
alias trs="tmux rename-session"

Enter fullscreen mode Exit fullscreen mode

Usage:

# creates new section with name my-session
$tn my-session

# attaches session 0
$ta -t0

# renames window as conf
$trw conf

# renames session as home
$trs home


Enter fullscreen mode Exit fullscreen mode

Listen Music From Your Terminal

Requires mpv , fzf , rg

Listening music via mpv from your terminal:

# listen music
alias lmusic='cd ~/Music/ && rg --files --glob "*.mp3" | fzf --multi --reverse --prompt="Song: " | xargs mpv --audio-display=no'
Enter fullscreen mode Exit fullscreen mode

Usage:

  1. Search favorite songs and add to your playlist using TAB

Adding New Paths to a List

This code block controls every path and duplicates and export every path in list to the $PATHENV directly.

# Define an array of directories to add to PATH
path=(
    $path                           # Keep existing PATH entries
    $HOME/bin
    $HOME/.local/bin
    $HOME/dotnet
    $SCRIPTS
    $HOME/.krew/bin
    $HOME/.rd/bin                   # Rancher Desktop
    # $HOME/*/bin(N)                # Add all bin directories under HOME
    # /usr/local/*/bin(N)           # Add all bin directories under /usr/local
)

# Remove duplicate entries and non-existent directories
typeset -U path
path=($^path(N-/))

# Export the updated PATH
export PATH

Enter fullscreen mode Exit fullscreen mode

Usage:

This code block add new paths and checks duplicates automatically, finally adds to your PATH. Just add your new path into the brackets with given format.

Extended Glob Operations

·# Enable Zsh's extended globbing and null_glob options
setopt extended_glob null_glob
Enter fullscreen mode Exit fullscreen mode

Enables extended glob in your filesystem for your ZSH.

extended_glob

  • In zsh: The extended_glob option allows for more advanced pattern matching in filenames. It enables extended globbing features such as ** for recursive directory matching, ^ for negating patterns, and more.
  • In bash: The equivalent feature is controlled by the shopt -s extglob command. When enabled, it allows extended pattern matching.

null_glob

  • In zsh: The null_glob option makes filename patterns that do not match any files expand to an empty list rather than remaining as the original pattern. This can be useful to avoid errors when patterns don’t match any files.
  • In bash: The equivalent functionality can be achieved by setting the nullglob option using shopt -s nullglob. When enabled, if a pattern does not match any files, it expands to nothing instead of remaining as the pattern.

Conclusion

This article is written for the .zshrc capability and is meant to show you what you can do by modifying your .zshrc in the terminal.

💖 💪 🙅 🚩
gokayburuc
gokayburuc.dev

Posted on August 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Some Config Ideas for your ZSH
bash Some Config Ideas for your ZSH

August 24, 2024