Skip to content

Instantly share code, notes, and snippets.

@marianposaceanu
Created January 23, 2024 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marianposaceanu/cb8b49d2508059aa4b1ae3cd534bf66b to your computer and use it in GitHub Desktop.
Save marianposaceanu/cb8b49d2508059aa4b1ae3cd534bf66b to your computer and use it in GitHub Desktop.

How to use top_history.sh

Overview

top_history.sh is a script that displays the top 10 most used commands from the zsh history. It generates a bar chart using Gnuplot to visualize the command frequencies.

Requirements

Before using top_history.sh, make sure you have the following requirements installed on your system:

  1. Zsh Shell: This script is designed for use with the Zsh shell. Ensure you have Zsh installed as your default shell.

  2. Gnuplot: Gnuplot is used to create the ASCII bar chart. You must have Gnuplot installed on your system. You can install it using package managers like apt, brew, or yum, depending on your operating system.

Usage

Follow these steps to use top_history.sh:

  1. Download the Script:

    • Download the top_history.sh script to your local machine.
  2. Make the Script Executable:

    • Open a terminal.
    • Navigate to the directory where top_history.sh is located.
    • Run the following command to make the script executable:
      chmod +x top_history.sh
      
  3. Run the Script:

    • Execute the script by running the following command in the terminal:
      ./top_history.sh
      

Example Output

Here's an example of what the script's output might look like:

                              Top 10 Most Used Commands
     4000 +----------------------------------------------------------------+
          | *##*    +     +      +     +      +     +      +     +      +  |
     3500 |-*##*                                                         +-|
          | *##*                                                           |
          | *##*                                                           |
     3000 |-*##*                                                         +-|
          | *##*                                                           |
     2500 |-*##*                                                         +-|
          | *##*                                                           |
     2000 |-*##*                                                         +-|
          | *##*                                                           |
     1500 |-*##*                                                         +-|
          | *##*                                                           |
     1000 |-*##*                                                         +-|
          | *##*  ****                                                     |
          | *##*  *##*   ****  ****                                        |
      500 |-*##*  *##*   *##*  *##*   ****  ****   ****                  +-|
          | *##*  *##*   *##*  *##*   *##*  *##*   *##*  ****   ****  **** |
        0 +----------------------------------------------------------------+
            git    vim   ack    cd  heroku  rails  tig   puma bundle  brew
#!/bin/bash
# top_history.sh: Displays the top 10 most used commands from the zsh history.
# Path to the zsh history file
HISTORY_FILE="$HOME/.zsh_history"
# Check if the history file exists
if [ -f "$HISTORY_FILE" ]; then
# Set the locale to C to handle character encoding issues
export LC_ALL=C
# Process the zsh history file
cat "$HISTORY_FILE" |
sed -e 's/^[^;]*;//' | # Remove everything before the first semicolon
awk '/^[a-zA-Z0-9]/ { # Only process lines that start with alphanumeric characters
CMD[$1]++; # Increment count for each command
count++; # Increment total command count
} END {
# After processing all lines, output command, count, and percentage of total
for (a in CMD) {
print CMD[a] " " CMD[a]/count*100 "% " a; # Format output
}
}' |
grep -v "./" |
column -c3 -s " " -t |
sort -nr |
nl |
head -n10 > top_commands.txt # Save the top commands to a file
# Create a Gnuplot script file
cat <<EOF > gnuplot_script.gp
set term dumb
set title "Top 10 Most Used Commands"
set ylabel "Frequency (%)"
unset key
set boxwidth 0.5
set style fill solid
set xtics rotate by -45
plot 'top_commands.txt' using 2:xtic(4) with boxes
EOF
# Use Gnuplot to create an ASCII bar chart
gnuplot -p gnuplot_script.gp
else
echo "History file not found."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment