]> Freerunner's - dotfiles.git/commitdiff
lf: add bulk-rename function
authorAndre Ramnitz <tux.rising@gmail.com>
Mon, 8 Jan 2024 21:44:20 +0000 (22:44 +0100)
committerAndre Ramnitz <tux.rising@gmail.com>
Sun, 18 Aug 2024 16:18:53 +0000 (18:18 +0200)
from this fine dude:
https://github.com/gokcehan/lf/issues/149#issuecomment-470960434

config/lf/lfrc

index 4a5ae92b48856ac0bf75445335697eacdeeebdb9..a376cd80c810793dc6424a88af22cb5dbffc8da9 100644 (file)
@@ -78,6 +78,35 @@ cmd zip ${{
     rm -rf $1
 }}
 
+# add bulk-renaming of files
+cmd bulk-rename ${{
+       index=$(mktemp /tmp/lf-bulk-rename-index.XXXXXXXXXX)
+       if [ -n "${fs}" ]; then
+               echo "$fs" > $index
+       else
+               echo "$(ls "$(dirname $f)" | tr ' ' "\n")" > $index
+       fi
+       index_edit=$(mktemp /tmp/lf-bulk-rename.XXXXXXXXXX)
+       cat $index > $index_edit
+       $EDITOR $index_edit
+       if [ $(cat $index | wc -l) -eq $(cat $index_edit | wc -l) ]; then
+               max=$(($(cat $index | wc -l)+1))
+               counter=1
+               while [ $counter -le $max ]; do
+                       a="$(cat $index | sed "${counter}q;d")"
+                       b="$(cat $index_edit | sed "${counter}q;d")"
+                       counter=$(($counter+1))
+                       
+                       [ "$a" = "$b" ] && continue
+                       [ -e "$b" ] && echo "File exists: $b" && continue
+                       mv "$a" "$b"
+               done
+       else
+               echo "Number of lines must stay the same"
+       fi
+       rm $index $index_edit
+}}
+
 cmd on-select &{{
     lf -remote "send $id set statfmt \"$(eza -ld --color=always "$f")\""
 }}