Useful bashrc/zshrc functions
For those who pretty much live in the command line, some bash-functions prove useful. These are the ones I have in my zshrc.
This one is for extracting files. I found it somewhere on the internet, but couldn’t find the source.
x(){
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*.tar.xz) tar xf $1 ;;
*) echo "Unable to extract '$1'" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
This one is to get the RSS feed for a given youtube channel, so I can subscribe to it using my feed-reader of choice.
youtube-to-rss(){
echo "Is the channel defined by a code (1) or by a name (2)?"
read type_of_channel_name
if [ $type_of_channel_name = 1 ]
then
echo "What is the channel's code?:"
read -r youtube_id
echo "https://www.youtube.com/feeds/videos.xml?channel_id=$youtube_id"
elif [ $type_of_channel_name = 2 ]
then
echo "What is the channel's name?:"
read -r youtube_id
echo "https://www.youtube.com/feeds/videos.xml?user=$youtube_id"
fi
}
This one is to search for a given string in all files of the CWD. I can never remember the correct flags and using the man-pages is tiresome.
find_string(){
echo "!!! Searching, in the current directory, recursively (r), with line numbers (n) and whole word search (w) !!!"
grep -rnw . -e "$1"
}
(I no longer use this. Now ripgrep is much faster)
I also have an alias that calls topydo, my to-do software of choice. Basically, I call “today” and it gives me the items that are tagged for today. I can also add new to-dos for today by simply doing “today vacuum”. The to-do file is synced across all my devices, phone and computers (I use a raspberry PI for syncing).
today() {
if [ -z "$1" ]
then
EDITOR=nvim topydo ls @today
else
EDITOR=nvim topydo add ""$@" @today"
fi
}
The function below is an alternative to terminal_velocity. I do “noty”, choose the note I want to open/edit and it opens with my editor of choice. It also supports deleting a note and creating a new one. fzf is obviously a requirement.
noty(){
local editor=nvim
local notes_path=~/my_notes
if [ -z $1 ] || [[ "$1" == "-d" ]]; then # To open or delete a note
note=$(ls $notes_path | fzf)
if [ -n "$note" ]; then
note=$notes_path/$note
if [ -z "$1" ]; then
$editor $note
elif [[ "$1" == "-d" ]]; then # do noty -d to delete a note
\rm -i $note
fi
fi
elif [[ "$1" == "-n" ]]; then # To create a new note called blabla, do "noty -n blabla"
note=$notes_path/$2
if [ ! -f $note ]; then # if file doesn't exist, open it with my editor
$editor $note
else
echo "A file with the same name/path already exists."
fi
elif [[ "$1" == "-h" ]]; then
echo $'noty -n for creating new note. \nnoty -d for deleting a note. \nnoty -h for this help. \nJust "noty" to select a note and open it with your text editor.'
else
echo "Option not available in the function."
fi
}
Other than these 5 small-functions, I pretty much only use aliases, CLI (command line interface) programs and useful zsh plugins. I use several zsh plugins, one example is the zsh-autosuggestions. Autojump and fzf are command-line programs that I use everyday and that severely improve productivity.