dotfiles/sh/.config/sh/alias
Marty Oehme 7eeeb9e85d
sh: Add clear and rerun function
Added simple function to clear the screen but additionally re-run the
last executed command.

Mirroring my clear screen alias `cl`, this one is executed with `cll`
(clear-and-last) currently.

It comes in useful for me every now and again because I sometimes just
need a quick way to get rid of 'clutter' on my terminal (e.g. running
multiple rg invocations after each other, or diffs, or anything that
spews a bunch of stuff on screen and I want to find specific things
afterwards).
I am very used to running `cl` and then the second to last history item.
This makes those additional keystrokes unnecessary.
2024-06-05 12:10:26 +02:00

124 lines
2.8 KiB
Bash

#!/usr/bin/env sh
#
# Global aliases for any shell
exist() { type "$1" >/dev/null 2>&1; }
# Avoid aliases which I did not create -- unalias EVERYTHING
unalias -a
if alias v >/dev/null 2>&1; then
if exist vim; then
alias v="vim"
else
alias v="vi"
alias vim="vi"
fi
fi
# exit shell mimicks vim
alias :q="exit"
# ls defaults
if exist exa; then
alias l="exa -l --git --git-ignore --group-directories-first"
alias L="exa -hal --grid --git --group-directories-first"
# a recursive tree
# - usually want to change levels recursed with -L2 -L3 or similar
alias ll="exa --tree -L2 --group-directories-first"
alias LL="exa -a --tree -L2 --group-directories-first"
alias lla="exa --tree --group-directories-first"
alias LLA="exa -a --tree --group-directories-first"
else
alias l="ls -lhF"
alias L="ls -lAhF"
fi
# cd defaults
alias ..="cd .."
alias ...="cd ../.."
alias ~="cd ~"
alias md="mkdir -p"
# clear my screen
alias cl="clear"
# clear screen AND re-run last command
# Is presumably not fully portable, see
# https://overflow.hostux.net/questions/24223811/getting-last-executed-command-from-script#
cll() {
lastfun="$(fc -n -l -1 -1)"
clear
eval "$lastfun"
}
# Display current external ip address
alias myip="curl -s icanhazip.com"
# fzf
if exist fzf; then
# Display fuzzy-searchable history
alias fzfhistory="history -l -E -D 0 | fzf --tac --height 20"
fzman() {
man "$(apropos --long "$1" | fzf | awk '{print $2, $1}' | tr -d '()')"
}
# ripgrep-all to fzf search through any documents
if exist rga; then
fzrga() {
RG_PREFIX="rga --files-with-matches"
open "$(
FZF_DEFAULT_COMMAND="$RG_PREFIX '$1'" \
fzf --sort --preview="[[ ! -z {} ]] && rga --pretty --context 5 {q} {}" \
--phony -q "$1" \
--bind "change:reload:$RG_PREFIX {q}" \
--preview-window="70%:wrap"
)"
}
fi
# quickly fzy search a sqlite database result
if exist sqlite3; then
fzql() {
sqlite3 -header "$1" "$2" | fzf --header-lines=1 --layout=reverse --multi --prompt='fzql> '
}
fi
fi
# vifm
if exist vifm; then
alias vm=vifm
alias vmm='vifm ${PWD}'
fi
# default image viewer
if exist imv-folder; then
iv() {
if [ "$#" -eq 1 ] && [ -f "$1" ]; then
imv-folder "$1"
else
imv "$@"
fi
}
else
for cmd in vimiv imv nsxiv sxiv feh; do
# shellcheck disable=SC2139
# since we *want* it defined at definition
if exist "$cmd"; then alias iv="$cmd"; break; fi
done
fi
# distrobox
if exist distrobox; then
alias db=distrobox
fi
if exist sc-im; then
alias sc=sc-im
fi
if exist tidy-viewer; then
tv() {
tidy-viewer -a -e "$@" | less -S -R
}
fi