What commands are you running?



As a software developer I use the terminal a lot. As I look for ways to save time and become more efficient it would make sense to reflect upon my terminal usage. I couldn’t say with certainty which commands I use the most. I’ve hammered them into the spinal cord.

Luckily there is the bash history. Making a historytop command is relatively simple. This command prints your top ten most used commands.

function historytop {
    history | cut -c 8- | sort | uniq -c | sort -nr | head -10
}

Breakdown of the Command

Let’s break down how the command works.

You can type history in your terminal to see your bash history. This history is usually capped at a length of 500 and each command is prefixed with line numbers:

  633  mkdir yada
  634  cd yada
  635  ls -ahl
  636  cd ..
  637  ls -ahl
  638  cd
  639  exit

We are not interested in the line number. The cut -c 8- remedies that by removing the first 7 letters from each line:

mkdir yada
cd yada
ls -ahl
cd ..
ls -ahl
cd
exit

Next is sort | uniq -c which groups them up with a count:

   1 cd
   1 cd ..
   1 cd yada
   1 exit
   2 ls -ahl
   1 mkdir yada

The sort -nr stands for sort numeric reverse, which places the high ones at the top:

   2 ls -ahl
   1 mkdir yada
   1 exit
   1 cd yada
   1 cd ..
   1 cd

Finally we use head -10 to pick the first ten lines only.

Reflection and Improvements

#1: At the top of the list we have git status and git diff. Now that I see the statistics I realise I have a habit of typing these commands before every commit to see I’m not committing the wrong stuff. I also need to see the output to figure out a good commit message.

Let’s make a single letter bash alias i as in “information”:

alias i='git diff && git status'

#2: I use git push quite often.

Let’s make a single letter bash alias p as in git push:

alias p='git push'
__git_complete p _git_push

#3: It would seem it’s common for me to check out the develop branch.

Let’s make a couple of single letter bash aliases d as in develop and m as in master:

alias m='git checkout master'
alias d='git checkout develop'

#4: Finally committing is a common action.

Let’s make a single letter alias c as in commit:

alias c='git add --all && git commit'

A Personal “Journey”

I feel the aliases above will be really useful to me. However they may not be optimal for you. Chances are your workday looks different than mine. As such making bash aliases is likely a personal journey.

Perhaps in that sense quite similar to “dotfiles”? You could copy an exiting dotfiles repo, but it might not suit you. The reflection and iterative improvement process itself is of importance.