Last active
September 15, 2021 12:24
-
-
Save godDLL/af7b15efc7d5b0687d0986079afb04b1 to your computer and use it in GitHub Desktop.
Backspace in the Terminal
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
asciinema.org/a/435871
License MIT free to do whatever, yours to fuck up.