dotfiles/.local/bin/scripts/edconf

30 lines
1.3 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# Find and edit config files utilizing fzf
2021-09-18 21:09:40 +00:00
alias dedup='awk '"'"'!a[$0]++'"'"
2021-09-20 06:56:42 +00:00
listconf() {
2021-09-21 20:51:19 +00:00
{ cat "$conf_cache";
$(which fd || echo fdfind) --hidden --type file --size -1m --max-depth 1 . ~;
2021-09-21 21:10:39 +00:00
find "${XDG_CONFIG_HOME:-$HOME/.config}" /etc /var/lib/postgres /var/lib/pleroma/static/instance \
-maxdepth 4 -follow \( -name Partitions -o -name mdn -o -name .git -o -name .local \) -prune -o \
! -iname "*.xml" -a ! -iname "*.markdown" -a ! -iname "*.md" -a ! -iname "*.org" \
-type f -readable -exec grep -lI '' {} + 2>/dev/null; } | dedup
}
2021-09-20 06:56:42 +00:00
2021-09-18 21:23:11 +00:00
conf_cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/edconf"
conf_cache="$conf_cache_dir/files"
conf_tmp="${conf_cache}.tmp"
mkdir -p "$conf_cache_dir"
touch "$conf_cache"
2021-09-20 06:56:42 +00:00
sel=$(listconf | fzf -1 -0 --tiebreak=end,length --preview '$(test -r "{}" || echo "sudo") bat --color=always --style=numbers --line-range :200 {}' --query="$1" --history "$conf_cache_dir/searches")
2021-09-18 21:23:11 +00:00
case "$sel" in
"") exit 1;;
/etc/sudoers) sudo visudo;;
/etc/fstab) sudoedit "$sel" && sudo findmnt --verify;;
/etc/default/grub) sudoedit "$sel" && sudo grub-mkconfig -o /boot/grub/grub.cfg;;
2021-09-20 19:34:00 +00:00
/etc/locale.gen) sudoedit "$sel" && sudo locale-gen;;
2021-09-21 20:32:35 +00:00
*) $(test -w "$sel" || echo sudo) "$EDITOR" "$sel";;
2021-09-18 21:23:11 +00:00
esac
echo "$sel" | cat - "$conf_cache" | head -20 >"$conf_tmp" && mv "$conf_tmp" "$conf_cache"