Created
December 20, 2020 22:32
-
-
Save dougpagani/9b0d2df17f891e3ba62e1f01e6b230ba to your computer and use it in GitHub Desktop.
makes an alias of the last commandline, escaping properly & allowing for review (I have not reviewed this so use at your own risk)
This file contains hidden or 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
# This function avoids having to: | |
# - vim into the right file | |
# - write out the alias syntax | |
# - quote a commandline's embedded quotes, which can get rather gnarly | |
# - reformat from line-squashing | |
# - add negative numbers (will auto-convert) | |
# TODO: Bug with aliasing THIS line: sed 's/\ -/ \\\n\t\-/g' | |
function aliaslast() { | |
#APPEND_LOCATION=~/.bp_local | |
if [[ -f ~/dotfiles/frags/bp_scratch ]]; then | |
APPEND_LOCATION=~/dotfiles/frags/bp_scratch | |
else | |
APPEND_LOCATION=~/.bashrc | |
fi | |
if [[ $1 == -d ]]; then local DBUG=1; shift; fi | |
if [[ $1 == --no-append ]]; then local NO_APPEND=1; shift; fi | |
############################ | |
# Get the line from history | |
if [[ $# == 0 ]]; then | |
subshell_adjusted_num=-1 | |
elif [[ $# == 1 ]]; then | |
# Add a dash (for negative number) if not already present | |
if [[ ${1:0:1} == "-" ]]; then # take it as-is | |
subshell_adjusted_num="$1" | |
else # add a dash | |
subshell_adjusted_num="-${1}" | |
fi | |
else | |
error "Too many args." | |
fi | |
# SAN for shorthand | |
SAN=$subshell_adjusted_num | |
# Debug printing: | |
if [[ $DBUG == 1 ]]; then | |
echo -e 'fc of subshell:\n'"$(fc -l $(( $SAN - 5 )) $SAN)" | |
echo "SAN: $SAN" | |
fi | |
# Get & clean the cmdline | |
rawline="$(fc -ln $SAN $SAN)" | |
lastline="$(echo "$rawline" | | |
sed 's/^[[:space:]]*//g' | | |
sed 's/[[:space:]]*$//g')" | |
echo -e "${YELO}To be aliased:${NC} '$lastline'" | |
# To see if there is a context where alternative-implementations would be more thorough | |
lastline2=$(history | tail -2 | head -1 | sed -r 's/^[ \t]*[0-9]*[ \t]+([^ \t].*$)/\1/') | |
lastline3=$(fc -ln "$(( ${1:0} - 2 ))" | sed '$ d' | sed 's/^[[:space:]]*//g' ) | |
echo -e "$GREY" | |
echo "ALTERNATIVES:" | |
[[ $DBUG == 1 ]] && echo "rawline is: $rawline" | |
echo "lastline2 is: $lastline2" | |
echo "lastline3 is: $lastline3" | |
echo -e "$NC" | |
# Get name of alias from the user, then make it in current-environment | |
printf "${YELO}Enter a name for the [alias]:${NC} " | |
read ALIASNAME | |
alias "${ALIASNAME}"="$lastline" | |
# Save it to a file for future sessions | |
if [[ "$NO_APPEND" == 1 ]]; then | |
echo -e "${ORANGE}TEMPORARY: Not added to bashrc.${NC}" | |
else | |
echo -e ${ELECTRIC_BLUE} | |
alias "$ALIASNAME" | tee -a $APPEND_LOCATION | |
echo -e ${NC} | |
fi | |
} | |
alias al="aliaslast" | |
alias aln="aliaslast --no-append" | |
# ^ for when you want a quick-n-dirty alias for something you'll probably run a lot | |
# - all 3 are basically the same for for-loops | |
# - the fc-way works for quote-lines (ones that dont collapse naturally with semi-colons) | |
# ... but history chokes on that | |
# WORKS FOR: | |
# 1. single lines | |
# 2. multiline quotes | |
# 3. multiline loops, cases | |
# 4. neg-ref | |
# 5. pos-ref | |
# BUG: for aliasing: manpath | tr ':' '\n' | nl, \n triggers a newline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment