Skip to content

Instantly share code, notes, and snippets.

@NickHatBoecker NickHatBoecker/README.md
Last active Jun 20, 2019

Embed
What would you like to do?
List aliases in a table like view

List your aliases in a table like view.

Save list_aliases.sh in your home directory in a bin directory. Create a file named .aliases in your home directory. Add all your aliases like this:

alias ..="cd .." # Navigate one directory down
alias ...="cd ../.." # Navigate two directories down
alias ip="ipconfig getifaddr en0" # Get local ip
alias gen-pwd="pwgen -s -N 1 -cny 32" # Generate password with 32 length

Next include your new .aliases file in your ~/.bash_profile (create it, if necessary):

source ~/.aliases
alias aliases='bash ${HOME}/bin/list_aliases.sh'

Restart terminal and type aliases. Now you can see a table with all your aliases and comments.

#!/bin/bash
INPUT_FILE="${HOME}/.aliases"
COMMAND_COLOR="\033[38;5;114m"
COMMENT_COLOR="\033[38;5;245m"
COLOR_RESET="\033[0m"
###
# @param number of characters to show, eg 1
# @param character to show, eg "-"
###
printWhitespaceCharacter () {
for (( c=0; c<=$1; c++ ))
do
echo -n "$2"
done
}
###
# Get longest command and comment
###
getMaxLengths () {
MAX_COMMAND_LENGTH=0
MAX_COMMENT_LENGTH=0
while IFS= read -r line
do
LINE_WITHOUT_ALIAS=$(echo "$line"| sed 's/[^ ]* *//')
COMMAND=$(echo "$LINE_WITHOUT_ALIAS" | sed 's/=.*//g')
COMMENT=$(echo "$LINE_WITHOUT_ALIAS" | sed 's/.*#//g')
COMMAND_LENGTH=${#COMMAND}
COMMENT_LENGTH=${#COMMENT}
if [ $COMMAND_LENGTH -gt $MAX_COMMAND_LENGTH ]; then
MAX_COMMAND_LENGTH=${COMMAND_LENGTH}
fi
if [ $COMMENT_LENGTH -gt $MAX_COMMENT_LENGTH ]; then
MAX_COMMENT_LENGTH=${COMMENT_LENGTH}
fi
done < "$INPUT_FILE"
}
###
# Print separator based on longest command and comment
###
printSeparatorLine () {
COMMAND_SEPARATOR=`expr $MAX_COMMAND_LENGTH`
COMMENT_SEPARATOR=`expr $MAX_COMMENT_LENGTH + 2`
echo "+$(printWhitespaceCharacter $COMMAND_SEPARATOR "-")$(printWhitespaceCharacter $COMMENT_SEPARATOR "-")+"
}
getMaxLengths
###
# Print headline
###
printHead () {
# Substract characters for "COMMAND" and " COMMENT"
COMMAND_WHITESPACE_LENGTH=`expr $MAX_COMMAND_LENGTH - 7`
COMMENT_WHITESPACE_LENGTH=`expr $MAX_COMMENT_LENGTH - 8`
echo ""
echo "$(printSeparatorLine)"
echo "| COMMAND$(printWhitespaceCharacter $COMMAND_WHITESPACE_LENGTH " ")| COMMENT$(printWhitespaceCharacter $COMMENT_WHITESPACE_LENGTH " ")|"
echo "$(printSeparatorLine)"
}
echo "$(printHead)"
###
# Run script
###
while IFS= read -r line
do
LINE_WITHOUT_ALIAS=$(echo "$line"| sed 's/[^ ]* *//')
COMMAND=$(echo "$LINE_WITHOUT_ALIAS" | sed 's/=.*//g')
COMMENT=$(echo "$LINE_WITHOUT_ALIAS" | sed 's/.*#//g')
# COMMAND
THIS_COMMAND_LENGTH=$(echo ${#COMMAND})
COMMAND_LENGTH=`expr $MAX_COMMAND_LENGTH - $THIS_COMMAND_LENGTH - 1`
echo -ne "| ${COMMAND_COLOR}${COMMAND}${COLOR_RESET} "
# Fill up with whitespace
printWhitespaceCharacter $COMMAND_LENGTH " "
# COMMENT
THIS_COMMENT_LENGTH=$(echo ${#COMMENT})
COMMENT_LENGTH=`expr $MAX_COMMENT_LENGTH - $THIS_COMMENT_LENGTH - 1`
echo -ne "|${COMMENT_COLOR}${COMMENT}${COLOR_RESET} "
# Fill up with whitespace
printWhitespaceCharacter $COMMENT_LENGTH " "
echo "|"
echo $(printSeparatorLine)
done < "$INPUT_FILE"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.