| 1 |
#!/usr/bin/env sh |
| 2 |
|
| 3 |
# Description: Batch rename selection or current directory with qmv or vidir |
| 4 |
# |
| 5 |
# Notes: |
| 6 |
# - Try to mimic current batch rename functionality but with correct |
| 7 |
# handling of edge cases by qmv or vidir. |
| 8 |
# - Qmv opens with hidden files if no selection is used. Selected |
| 9 |
# directories are shown. |
| 10 |
# - Vidir don't show directories nor hidden files. |
| 11 |
# |
| 12 |
# Shell: POSIX compliant |
| 13 |
# Author: José Neder |
| 14 |
|
| 15 |
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection} |
| 16 |
|
| 17 |
if type qmv >/dev/null 2>&1; then |
| 18 |
batchrenamesel="qmv -fdo -da" |
| 19 |
batchrename="qmv -fdo -a" |
| 20 |
elif type vidir >/dev/null 2>&1; then |
| 21 |
batchrenamesel="vidir" |
| 22 |
batchrename="vidir" |
| 23 |
else |
| 24 |
printf "there is not batchrename program installed." |
| 25 |
exit |
| 26 |
fi |
| 27 |
|
| 28 |
if [ -s "$selection" ]; then |
| 29 |
printf "rename selection? " |
| 30 |
read -r resp |
| 31 |
fi |
| 32 |
|
| 33 |
if [ "$resp" = "y" ]; then |
| 34 |
# -o flag is necessary for interactive editors |
| 35 |
# shellcheck disable=SC2086 |
| 36 |
xargs -o -0 $batchrenamesel < "$selection" |
| 37 |
|
| 38 |
# Clear selection |
| 39 |
if [ -p "$NNN_PIPE" ]; then |
| 40 |
printf "-" > "$NNN_PIPE" |
| 41 |
fi |
| 42 |
elif [ ! "$(LC_ALL=C ls -a)" = ". |
| 43 |
.." ]; then |
| 44 |
# On older systems that don't have ls -A |
| 45 |
$batchrename |
| 46 |
fi |