GithubHelp home page GithubHelp logo

aashutoshrathi / awesome-bashrc Goto Github PK

View Code? Open in Web Editor NEW
187.0 9.0 22.0 41 KB

๐Ÿš€ Collection of bash snippets/aliases that will save your time on the terminal

Python 100.00%
awesome-list bashrc bashrc-configs hacktoberfest

awesome-bashrc's Introduction

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

awesome-bashrc's People

Contributors

aashutoshrathi avatar alejandrocq avatar anshumanv avatar azemabaptiste avatar damien1994 avatar diningphilosopher64 avatar federicoponzi avatar fireyfly avatar kdmartel avatar matosbrent avatar mohitkyadav avatar particleflux avatar prithaupadhyay avatar thepushkarp avatar zjesko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

awesome-bashrc's Issues

`cpp` alias shadows the C preprocessor binary

Your C (and C++) compiler typically ships with the C preprocessor exposed as a standalone program, cpp (e.g. gcc and clang do). It seems a bit ill-advised to shadow it with an alias, since one might want to use the preprocessor separately at times.

Maybe something like cpp-run would be better as the suggested name? (and c-run for the C equivalent, for consistency)

Add a command to exact search through your aliases

@aashutoshrathi following func is what I use, but as you can see it only reads the .zshrc file. We can check for the shell and determine where to look. I was also ๐Ÿค” if it's possible to check the self file name? If so same function could be used in any number of rc files.

cmdmatch () {
  # --help
  if [[ $1 == '--help' ]]; then
    echo "Usage: cmdmatch <pattern>";
    return 0;
  fi

  # Return matching aliases
  grep "alias" ~/.zshrc | grep "$1";

  # error if no arguments
  if [ $# -eq 0 ]; then
    echo "No arguments supplied"
    return 1
  fi
}

Add command to do git cleanup

clgitb() {
  # Update remotes tracking data
  git fetch origin
  # Delete the refs to remote branches that no longer exist.
  git remote prune origin
  # Remove local fully merged branches
  git branch -vv | grep "origin/.*: gone]" | awk '{print $1}' | xargs git branch -D
}

Add git.io custom URLs for GitHub

Code to be added in bashrc:

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

How to make custom url?

$ gurl https://github.com/aashutoshrathi/awesome-bashrc awe-bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   335  100    48  100   287     34    203  0:00:01  0:00:01 --:--:--   206HT                                                                                                                                  TP/1.1 100 Continue

HTTP/1.1 201 Created
Server: Cowboy
Connection: keep-alive
Date: Fri, 05 Oct 2018 05:15:44 GMT
Status: 201 Created
Content-Type: text/html;charset=utf-8
Location: https://git.io/awe-bash
Content-Length: 48
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.232524
X-Node: 173cfc17-5e63-49f8-98c5-44f0561a55e4
X-Revision: 392798d237fc1aa5cd55cada10d2945773e741a8
Strict-Transport-Security: max-age=31536000; includeSubDomains
Via: 1.1 vegur

https://github.com/aashutoshrathi/awesome-bashrc

Yes, after these steps, https://git.io/awe-bash will redirect you to https://github.com/aashutoshrathi/awesome-bashrc

PR template talks about "apps"

The PR template says something about apps

[Explain what this app is about and why it should be included here.]

Shouldn't this rather be replaced by alias?

Add a tally command

A small function but usefull

tally() {
  sort | uniq -c | sort -n
}

Usage -> ls | tally

Run a command n number of times.

Usecase: Run a flaky test n number of times to test it if fails. But you can run any command.

#!/bin/sh

if [[ "$1" == "-h" ]]
then
    echo "
        Usage: repeat-cmd.sh <number of times to repeat> <command to repeat>
        Example: repeat-cmd.sh 5 "CI=true npm run test -- SomeComponent.test" -s

        -s: Silent mode, does not show any output from the command.
        "
    exit 0
fi

passed=0
totalTests=$1
i=0
while [ $i -ne $totalTests ]
do
    i=$(($i+1))
    if [[ "$3" == "-s" ]]
    then
        eval $2 > /dev/null 2>&1
    else
        echo "----------------------------------------------"
        echo "Test $i/$totalTests"
        echo "----------------------------------------------"
        eval $2
    fi


    if [ $? -eq 0 ]
    then
        passed=$(($passed+1))
        echo "Test $i: PASSED"
    else
        echo "Test $i: FAILED"
    fi
done

echo "Passed: $passed / $totalTests"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.