Just a few useful .bashrc changes

Table of Contents

I found it useful to write wrappers for some commands and put them in ~/.bashrc. When:

  • If command is too long to memorise
  • If command is not self-explainary

Here are some:

parallel() {
    nohup $@ > /dev/null 2>&1 &  
}

Usage

parallel command arg1 arg2 arg3 ...

Example

parallel kate .

This will open current folder in kate editor.

Record video from screen with ffmpeg

capturescreen() {
    ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0+0,0 $1
}

Usage

capturescreen outfile.mp4

git branch, but sorted by last commit date and with description

gitbranches() {
    branch=""
    branches=`git branch $@`
    while read -r branch; do
        clean_branch_name=${branch//\*\ /}
        description=`git config branch.$clean_branch_name.description`
        lastcommitdate=`git for-each-ref --sort=committerdate "refs/heads/**/${clean_branch_name}" --format='%(committerdate:short)'`
        if [ "$clean_branch_name" != "$branch" ]; then
            printf "\033[0;32m";
        fi;
        printf "%-15s %s\n" "$branch    [$lastcommitdate]   $description"
        if [ "$clean_branch_name" != "$branch" ]; then
            printf "\033[0m";
        fi;
    done <<< "$branches"
}

Useful if you use branch per task, like in GitFlow. You can pass git branch attributes to this function as usual

Example

gitbranches --no-merged

Init envvars for my current projects

initmyenv() {
    if ! [[ "$PATH" =~ "$HOME//scripts:" ]]
    then
        PATH="$HOME//scripts:$PATH"
    fi
    export PATH
    export PROJECT1_DBCONNECT="Server=localhost;Database=project1;User Id=sa;Password=somepw;"
    export PROJECT2_DBCONNECT="Server=localhost;Database=project2;User Id=sa;Password=somepw;"
    ...
}

Btw, whis way you do not leak you db password to git. Search 12factor for more )

Show git graph in text mode

gitgraph() {
    git log --graph --full-history --all --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
}

Help for my ustom commands

helb() {
    echo ".. i need sombody, heeeelb";
    echo "
        parallel <cmd> <args>   - run command in parallel and forward stderr and stdout to /dev/null
        gitgraph                - display git log in nice form, with branch tree
        gitbranches             - display git branch with descriptions
        capturescreen   <outfile> - wrapper for ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0+0,0 file.mp4
    ";
    # also show my custom scripts
    ls -1 ~/scripts
}

I put call to helb at the end of .bashrc, so every time I run new console I can see it. That’s it )