3 # Description: Clean filename or dirname (either hovered or selections)
4 # to be more shell-friendly. This script cleans
5 # non A-Za-z0-9._- characters.
6 # and replaces it with underscore (_).
8 # It supports cleaning single/double quote, newline,
9 # leading, trailing spaces.
12 # to be continued (つづく).mp4 -> to_be_continued______.mp4
13 # [work] stuff.txt -> _work__stuff.txt
14 # home's server -> home_s_server
15 # qwe\trty -> __qwe_rty
17 # And if there are two almost similar filenames
18 # like: 'asd]f' and 'asd f' both will be renamed to 'asd_f',
19 # to avoid overwriting, the last file will be prepended by _.
20 # So they will be: 'asd_f' and '_asd_f'
28 sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
31 # printf "%s" "$1" | sed -e 's/[^A-Za-z0-9._-]/_/g'
32 printf "%s" "$1" | sed 's/[^A-Za-z0-9._-]/_/g' | sed ':a;N;$!ba;s/\n/_/g'
35 if [ -s "$sel" ]; then
37 while IFS= read -r -d '' i || [ -n "$i" ]; do
38 targets+=( "$(basename "$i")" )
44 for i in "${targets[@]}"; do
45 printf "%s -> %s\n" "$i" "$(cleanup "$i")";
50 printf "Proceed [Yn]? "
62 for i in "${targets[@]}"; do
63 if [ "$i" != "$(cleanup "$i")" ]; then
65 if [ -e "$(cleanup "$i")" ]; then
68 mv "$i" "$tmp$(cleanup "$i")";
73 if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
74 printf "-" > "$NNN_PIPE"