Created
May 8, 2024 11:22
-
-
Save sleekweasel/a415fd1c5f80f79ec370d1d341340faf to your computer and use it in GitHub Desktop.
A copy-pasteable shell-based interactive line editor, for when I don't have vim in a container and don't want to install it for some reason.
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
#!/bin/bash | |
if ! [[ -f "$1" ]] ; then echo "Usage: $0 file-to-edit" ; exit 42 ; fi | |
FILE=$1 | |
if [[ -f "$FILE~" ]] && ! diff -u $FILE $FILE~ ; then | |
echo Resolve or ignore above diffs. Quitting. ; echo rm $FILE~ ; exit 42 | |
fi | |
cp $FILE $FILE~ | |
help() { echo "Try" ; grep -E ') *#' $0 ; echo "And ^d to abort" ; } | |
help | |
while echo -n ": " ; read ; do | |
case "$REPLY" in | |
d*) # e.g. d - diff vs original | |
diff -u $FILE $FILE~ && echo no diff ;; | |
p*|.p*) # e.g. p or p 10 or p 4,6 or p /hello/ - print line/s | |
cat -n $FILE~ | sed -n "${REPLY#*p} p" ;; | |
s*) # e.g. s 4,6 s/find/replace/ (any sed command like '5d' or '5i insert-before' or '5a insert-after' with \ for leading space ) | |
sed -i@ "${REPLY#s}" $FILE~ ;; | |
qs*) # Quit and save | |
echo "Saving $FILE~ to $FILE" ; mv $FILE~ $FILE ; exit 0 ;; | |
qq*) # Quit without saving | |
echo "Removing $FILE~ to $FILE~~" ; mv $FILE~ $FILE~~ ; exit 0 ;; | |
*) | |
echo "Don't know '$REPLY'" ; help | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment