Last active
September 4, 2020 03:52
-
-
Save santrancisco/9431f73b9d1a09079d8b76b13a9dfbf9 to your computer and use it in GitHub Desktop.
a function to help record interesting command in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Put this function inside .bashrc for a good time ;) | |
export NOTE="$HOME/terminalnotes.md" | |
function tnote() { | |
## Append what we have first | |
history -a | |
## Traping CTRL+C to return to terminal | |
trap 'return' SIGINT | |
CHOICE=18 ## Set default choice to the command before calling tnote | |
while true | |
do | |
CHOICE=n=$(($CHOICE%22)) | |
clear | |
HISTORY_ENTRIES=`history | tail -n 20` | |
SAVEIFS=$IFS # Save current IFS | |
IFS=$'\n' # Change IFS to new line | |
HISTORY_ARRAY=($(echo "$HISTORY_ENTRIES" | awk '{ $1 = ""; print $0;}' | sed 's/^ *//g';echo "\033[41mEnter your own command\033[0m";echo "\033[41mUse clipboard\033[0m")) # Turn last 20 history entries into array | |
IFS=$SAVEIFS # Change IFS back | |
CURRENTCOMMAND="${HISTORY_ARRAY[$CHOICE]}" | |
echo "Last few entries in your history are:" | |
for option in `seq 0 21` | |
do | |
if [[ $option -eq $CHOICE ]]; then | |
echo -e "\033[93m $option\t${HISTORY_ARRAY[$option]} \033[0m" | |
else | |
echo -e "\033[35m $option\033[0m\t${HISTORY_ARRAY[$option]}" | |
fi | |
done | |
echo "---------------------------------" | |
echo "Default choice: $CURRENTCOMMAND" | |
echo "" | |
exec 3<>/dev/tty | |
read -r -sn1 -p "Use arrow key to choose command: " choice | |
case $choice in | |
A) CHOICE=$(($CHOICE-1)); continue ;; # Up | |
B) CHOICE=$(($CHOICE+1)); continue ;; # Down | |
# C) echo right; continue ;; | |
# D) echo left; continue ;; | |
'') echo "" ;; # Enter key | |
*) continue ;; # Ignore everything else | |
esac | |
cmd="$CURRENTCOMMAND" | |
if [[ $CHOICE -eq 20 ]]; then | |
read -u 3 -p "Enter custom command: " cmd | |
fi | |
if [[ $CHOICE -eq 21 ]]; then | |
cmd=`xclip -selection clipboard -o` | |
fi | |
if [ "$cmd" != "" ]; then | |
echo -e "\nYou picked: \033[0;34m$cmd\033[0m" | |
read -u 3 -p "Enter descriptions: " desc | |
echo -e "\n$desc\n" >> $NOTE | |
echo '```bash' >> $NOTE | |
echo "$cmd" >> $NOTE | |
echo '```' >> $NOTE | |
echo -e "Note is appended to \033[0;32m$NOTE\033[0m\n" | |
break | |
fi | |
echo "Pick again" | |
done | |
} | |
function tnotefind() { | |
pattern=$(echo "$@"|sed 's/ /(.(?<!\\`\\`\\`))*?/g') | |
findregex="(?s)\`\`\`bash(.(?<!\`\`\`))*?$pattern(.(?<!\`\`\`))*?\`\`\`\n" | |
grep -Pzo "$findregex" $NOTE | sed 's/\x0//g' |consolemd | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment