code / bb

Lines2.7K C1.8K Shell331 YAML273 Markdown197 make44
(90 lines)
1 #!/bin/sh
2 # This file defines file actions for bb. Each command will be run with files in
3 # the $@ arguments.
4 #
5 # The format is: ## Action \n { action }
7 ## Edit file(s)
8 {
9 $EDITOR "$@"
12 ## Delete file(s)
14 printf "\033[1mDeleting the following:\n\033[33m$(printf ' %s\n' "$@")\033[0m" | bbunscroll | more
15 bbconfirm
16 rm -rf "$@"
17 bbcmd deselect refresh
20 ## Page through file(s)
22 less -XK "$@"
25 ## Rename file(s)
27 for f; do
28 newname="$(bbask "Rename $(printf "\033[33m%s\033[39m" "${f##*/}"): " "${f##*/}")" || break
29 r="$(dirname "$f")/$newname"
30 [ "$r" = "$f" ] && continue
31 [ -e "$r" ] && printf "\033[31;1m$r already exists! It will be overwritten.\033[0m "
32 && bbconfirm && { rm -rf "$r" || { bbpause; exit; }; }
33 mv "$f" "$r" || { bbpause; exit; }
34 bbcmd deselect:"$f" select:"$r"
35 [ "$f" = "$BB" ] && bbcmd goto:"$r"
36 done
37 bbcmd refresh
40 ## Rename file(s) with regex
42 if ! command -v rename >/dev/null; then
43 printf '\033[31;1mThe `rename` command is not installed. Please install it to use this key binding.\033[0m\n'
44 bbpause; exit 1
45 fi
46 patt="$(bbask "Replace pattern: ")"
47 rep="$(bbask "Replacement: ")"
48 printf "\033[1mRenaming:\n\033[33m$(rename -nv "$patt" "$rep" "$@")\033[0m" | bbunscroll | more
49 bbconfirm
50 rename -i "$patt" "$rep" "$@"
51 bbcmd deselect refresh
54 ## Pass file(s) as arguments to a command
56 cmd="$(bbask '@')"
57 sh -c "$cmd \"\$@\"" -- "$@" || true
58 bbpause
59 bbcmd refresh
62 ## Pipe file(s) to a command
64 cmd="$(bbask '|')"
65 printf '%s\n' "$@" | sh -c "$cmd" || true
66 bbpause
67 bbcmd refresh
71 ## Run file(s)
73 for f; do "$f"; done
74 bbpause
77 ## Open file(s)
79 if [ "$(uname)" = "Darwin" ]; then
80 for f; do open "$f"; done
81 else
82 xdg-open "$f"
83 fi
86 ## Print filename(s)
88 echo "$@"
89 bbpause