~wrm/dotfiles

fc60d200a778200b0e781b88e3c81d4e42398c50 — +wrm 4 months ago 03f7c8b
Improve fc -s hack

Vastly improve function with the superpower of hindsight and man pages.
1 files changed, 3 insertions(+), 11 deletions(-)

M .config/shell/shellrc
M .config/shell/shellrc => .config/shell/shellrc +3 -11
@@ 27,23 27,15 @@ if [ "${SHELL}" = "/bin/dash" ]; then
	# https://www.spinics.net/jists/dash/msg01797.html
	# So we hack around it.
	fcs () {
		if [ -z $1 ]; then
			eval $( fc -ln -1 )
			return
		fi

		old="${1%%=*}"
		if [ $old != "$1" ]; then
		if [ ${#old} -lt ${#1} ]; then
			new="${1#*=}"

			[ -z $2 ] && \
			eval $( fc -ln -1 | sed s/"$old"/"$new"/ ) || \
			eval $( fc -ln $2 $2 | sed s/"$old"/"$new"/ )
			eval $( fc -ln ${2:-"-1"} ${2:-"-1"} | sed "s/$old/$new/" )
		else
			# NB keeping this line for posterity as funniest hack 2021.
			## Source from stdin to retain aliases.
			#fc -ln $1 $1 | . /dev/stdin
			eval $( fc -ln $1 $1 )
			eval $( fc -ln ${1:-"-1"} ${1:-"-1"} )
		fi
	}