/awesome-bashrc

🚀 Collection of bash snippets/aliases that will save your time on the terminal

Primary LanguagePython

awesome-bashrc


🚀 Curated list of awesome bashrc snippets that will make your work easier.

Inspiration

Working with bash in daily life, it is very irritating writing the same command multiple times. To avoid that we write aliases/snippets for bashrc and make our life easier.

This repository will have collection of such aliases. Read Contribution Guidelines before contributing.

Contents

C/C++ compile and run

cpp-run() {
    echo "Compiling file..."
    g++ -o "$1" "$1.cpp"
    echo "Compiled! Enter input :D"
    ./"$1"
}
# cpp-run filename

c-run() {
    echo "Compiling file..."
    gcc -o "$1" "$1.c"
    echo "Compiled! Enter input :D"
    ./"$1"
}
# c-run filename

git diff for JS Devs

alias gd="git diff --ignore-all-space 
                    --ignore-space-at-eol 
                    --ignore-space-change 
                    --ignore-blank-lines -- . 
                    ':(exclude)*package-lock.json'"

# Write gd to ignore not important differences.
# Credits: https://www.reddit.com/r/javascript/comments/9i6hl3/alias_for_open_source_js_devs/

git status

alias s="git status"

Upload your package to PyPi.org

This generates the respective dist files and uploads them to PyPi.org by asking your credentials at the end.

alias pyup="python setup.py sdist bdist_wheel && twine upload dist/*"

apt-get update

alias update='sudo apt-get update'

open

Open any file using its default program (eg. pdfs, torrents, etc).

alias open="xdg-open"

List files

alias ll='ls -laht'
alias l='ls -aC'

Tally

tally() {
  if [[ $1 == '--help' ]]; then
    echo "Usage: ls | tally";
    return 0;
  fi
  
  sort | uniq -c | sort -n
}

Check free space on disks

alias df='df -h'

Safe operations with files

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

git branch

This will display the current git branch of any repository you're in.

# Add git branch if its present to PS1
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

# Pass the function to the color parser of the terminal to colorize the branch name.
if [ "$color_prompt" = yes ]; then
    if [[ ${EUID} == 0 ]] ; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\h\[\033[01;34m\] \W $(parse_git_branch)\$\[\033[00m\] '
    else
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w $(parse_git_branch)\$\[\033[00m\] '
    fi
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h \w \$ '
fi
unset color_prompt force_color_prompt

git.io alias

Get shortened git.io URLs from a single command

gurl() {
    curl -i https://git.io -F "url=$1" \
    -F "code=$2"
}

# Usage
# gurl https://github.com/anshumanv anshumanv

After these steps, https://git.io/anshumanv will redirect you to https://github.com/anshumanv

tree alias

Get representation of underlying files and folders as a tree.

alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"

Fast upwards navigation (comes with ZSH)

alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'

Download music from youtube video

You will need mp3-lame library and youtube-dl utility.

alias youtube-mp3="youtube-dl --extract-audio --audio-format mp3"
# Usage
# youtube-mp3 https://youtube.com/{id}

Get saved WiFi keys

alias wifikey="sudo grep -r '^psk=' /etc/NetworkManager/system-connections/"
# Usage (requires sudo)
# wifikey

Take Screenshot of connected ADB Device

alias cap="adb shell screencap -p /sdcard/screen.png && \
           adb pull /sdcard/screen.png && \
           adb shell rm /sdcard/screen.png"
# Usage (requires connected device)
# Saves the screenshot with name screen.png
# cap

Bootstrap your CF Round

cf() {
    mkdir "CF#$1" &&\
    cd "CF#$1" &&\
    touch inp.txt &&\
    touch out.txt && \
    curl -L "https://files.aashutosh.dev/cpp.cpp" -o A.cpp
}

# Usage: cf 549
# The above command initialzes, Your CF Round folder and downloads your sample template.
# https://files.aashutosh.dev/cpp.cpp refers to link of your template

Run Matlab scripts

matlab-run() {
    matlab -nodesktop -nosplash -r "$1"
}
# matlab-run filename

Convert GIF to WebM

Why? => Read this (You can save upto 90% on size of media)

gif2webm() {
    ffmpeg -i $1.gif -c vp9 -b:v 0 -crf 41 $1.webm
}
# gif2webm gif-name-without-format

Python

Framework used for coming up with aliases for Python:

  • To create / make a directory, mkdir is used and to remove a directory rmdir is used. Extending this logic of using mk and rm for aliases.

For example:

  1. pirm : pip remove a package
  2. mkvenv : To create / make a virtual environment

First defining a custom print function to pretty print alias output. This will help us distinguish between command output and alias output

function repeat {
    num="${2:-100}"; printf -- "$1%.0s" $(seq 1 $num);
}

# custom print function for pretty printing aliases/ functions
function print {
    terminalCols=$(tput cols)
    argLen=${#1}
    offset=$(((terminalCols-argLen)/2))

    printf "\n"
    repeat '#' $((offset-1))
    printf " $1 "
    repeat '#' $((offset-1))
    printf "\n"     
}

NOTE: If you are not interested in using these helper functions, you can replace print with echo in all the below code snippets.

Aliases specific to python:
alias 'py'='python'
alias 'pirm'='pip uninstall -y'
alias 'sdist'='rm -rf dist/ ; py setup.py sdist'
alias 'bdist'='rm -rf dist/ ; py setup.py bdist_wheel'

# Pip install 
function pi {
    if [ -z "$1" ]
    then 
        # Looks for setup.py in the current directory and installs the package
        print "Installing from local setup.py file in the current directory"
        pip install .
    else 
        # Installs the package from PyPI 
        print "Fetching from PyPI"
        pip install "$1"
    fi
}

alias 'pii'='pi'
Python virtual environments
To create a python virtual environment in the current directory:
# Create new venv using python3
# If no name is passed will default to .venv
function mkvenv {
    if [ -z "$1" ]
    then
        print "Creating virtualenv: .venv"
        python3 -m venv .venv
        avenv
    else
        print "Creating virtualenv: $1"
        python3 -m venv "$1"
        avenv "$1"
    fi

    print "Upgrading pip"
    pip install pip --upgrade

    print "Installing wheel package"
    pip install wheel
    print "Activated virtualenv"
}

alias 'mkenv'='mkvenv'
To remove a python virtual environment in the current directory
# Remove a virtual env.
# If no name is passed will default to .venv
function rmvenv {
    if [ -z "$1" ]
    then
        print "Removing virtualenv: .venv"
        python3 -m venv .venv
        rm -rf .venv
    else
        print "Removing virtualenv: $1"
        rm -rf "$1"
    fi
}

alias 'rmenv'='rmvenv'
To activate a virtual environment in the current directory.

Going by the general assumption that python virtual environments are named as:

  • .venv : Local virtual environemnt
  • .venv37 : Local virtual environment with Python 3.7
  • .venv38 : Local virtual environment with Python 3.8

The below alias uses fzf to choose a virtual environment to activate when there are multiple virtual environments in the current directory.

# Activate virtual env
# If no name is passed will default to .venv
function avenv {
    # If no paramerter is passed try to activate .venv first. If .venv doesnt exist try with the next closest one. If both .venv37 and .venv38 exist. It will pick .venv37
    # If parameter is passed, try to activate that one
    if [ -z "$1" ]
    then

        if [ -d ".venv" ] 
        then
            source .venv/bin/activate 
            print "Activated virtualenv: .venv" 

        else   
            # Piping the output of find command to fzf to select a virtual env.
            virtual_env=$(find -maxdepth 2  -type d -name ".venv*"  | fzf)  
            print "Activated virtualenv: $virtual_env" 
            source "$virtual_env"/bin/activate
        fi 

    else
         source "$1"/bin/activate; print "Activated virtualenv: $1" || print "Failed to activate virtualenv: $1"

    fi
}

alias 'aenv'='avenv'
To deactivate a virtual environment
# Deactivate virtual env

function dvenv {

    if [ -z "$1" ]
    then
        deactivate || print "Failed to deactivate virtualenv: .venv"
        print "Deactivated virtualenv"
    else
        deactivate "$1" || print "Failed to deactivate virtualenv: $1"
        print "Deactivated virtualenv" 
    fi
}
alias 'denv'='dvenv'
Anaconda Python

Utilizes the mamba executable wherever possible to improve anaconda command execution times.

Follows a similar style of aliases as the above set of python aliases.

Only difference is, aliases are prefixed with c.

#  MAMBA: Currently, only install, create, list, search, run, info and clean are supported through mamba.

#list envs
alias 'clsenv'='mamba env list'
alias 'clsvenv'='clsenv'
To create a conda environment
#create new venv
function cmkvenv {
   mkdir -p /usr/softwares/miniconda3/envs/"$1"
#  mamba create -n "$1" --prefix=/usr/softwares/miniconda3/envs/"$1" python="$2" -y
  conda create  --prefix=/usr/softwares/miniconda3/envs/"$1" python="$2" -y
}
alias 'cmkenv'='cmkvenv'
To remove a conda environment
# remove arbitrary number of conda virtual envs
function crmenv {
  mamba env remove -n "$@"
}

alias 'crmvenv'='crmenv'
To activate/ deactivate a conda environment
#activate venv.
#Can't use mamba for activation and deactivation of env
function caenv {
  conda activate "$1"
}

#deactivate venv
alias "cdvenv"="conda deactivate"
alias "cdenv"="cdvenv"
To add/remove and list conda channel(s)
#add channels in conda
function caddc {
    if [ -z "$1" ]
    then
        echo "Provide a channel name to add!"
        #exit 1;
    else
        mamba config --add channels "$1"
    fi
}

#list channels in conda
function clsch {
  conda config --show channels
}

# remove a channel
function crmch {
    conda config --remove channels "$1"
}
To install / remove a conda package
#Conda install a package
function ci {
    if [ -z "$2" ]
    then
        /usr/softwares/miniconda3/condabin/mamba install -y "$1"
    else
         /usr/softwares/miniconda3/condabin/mamba install -c "$2" "$1"
    fi
}

#Conda remove a package
alias 'crm'='mamba uninstall -y'

License

CC0


File Templates taken from awesome-no-login-web-apps

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY