diff --git a/plugins/README.md b/plugins/README.md index c4a204da..fdb0295b 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -20,6 +20,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina | [bookmarks](bookmarks) | Use named bookmarks managed with symlinks | sh | fzf | | [boom](boom) | Play random music from dir | sh | [moc](http://moc.daper.net/) | | [bulknew](bulknew) | Create multiple files/dirs at once | bash | sed, xargs, mktemp | +| [cleanfilename](cleanfilename) | Clean filename to be more shell-friendly | sh | sed | | [dups](dups) | List non-empty duplicate files in current dir | bash | find, md5sum,
sort uniq xargs | | [chksum](chksum) | Create and verify checksums | sh | md5sum,
sha256sum | | [diffs](diffs) | Diff for selection (limited to 2 for directories) | sh | vimdiff, mktemp | diff --git a/plugins/cleanfilename b/plugins/cleanfilename new file mode 100755 index 00000000..924d5d04 --- /dev/null +++ b/plugins/cleanfilename @@ -0,0 +1,56 @@ +#!/usr/bin/env sh + +# Description: Clean filename (either hovered or selections) +# to be more shell-friendly. +# This script replaces any non A-Za-z0-9._- char +# with underscore (_). +# +# Although the name is 'cleanfilename', but it should work +# on directories too. +# +# Dependencies: sed +# +# Shell: POSIX compliant +# Author: Benawi Adha + +prompt=true +sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection} +IFS=' +' + +cleanup() { + printf "%s" "$1" | sed -e 's/[^A-Za-z0-9._-]/_/g' +} + +if [ -f "$sel" ]; then + targets=$(sed -e "s/\\x0/\\n/g;\$a\\" "$sel" | \ + while read -r i; do + basename "$i"; + done) +else + targets=$1 +fi + +for i in $targets; do + printf "%s --> %s\n" "$i" "$(cleanup "$i")"; +done + +if $prompt; then + echo + printf "Proceed [Yn]? " + read -r input + case "$input" in + y|Y|'') + ;; + *) + echo "Canceled" + exit + ;; + esac +fi + +for i in $targets; do + if [ "$i" != "$(cleanup "$i")" ]; then + mv "$i" "$(cleanup "$i")"; + fi +done