Skip to content

Instantly share code, notes, and snippets.

@godDLL
Last active September 15, 2021 12:24
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 godDLL/af7b15efc7d5b0687d0986079afb04b1 to your computer and use it in GitHub Desktop.
Save godDLL/af7b15efc7d5b0687d0986079afb04b1 to your computer and use it in GitHub Desktop.
Backspace in the Terminal
function bs --description 'Backspace over last line'
set -l EDITED '\e[7m'
if test -z "$argv"
# default
echo -ne -- '\e[A\e[K\e[A' # up, clear line, up
else
set -l N $argv[1] # -5 to clear five up, 5 to type over fifth line
if test '+-' = +(string sub -l 1 -- "$N")
# clear N lines up
set -l C (seq 0 (string sub -s 2 -- "$N"))
for T in $C
echo -ne -- '\e[A\e[K'
end
return
end
# or type over line N, clearing it first in style
echo -ne -- '\e['$N'A'$EDITED'\e[K \e['(math -s0 $COLUMNS' - 1')'b\e[G'
while read -z -n 1 -P '' -l X
# use Enter/Return to finish typing
if test \n = "$X"
echo -ne -- '\e[m'
break
end
end
# this is just like using N-1 except it's not
echo -ne -- '\e['$N'B\e[A\e[m' # N down, up, reset
end
end
@godDLL
Copy link
Author

godDLL commented Sep 15, 2021

asciinema.org/a/435871
License MIT free to do whatever, yours to fuck up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment