Last active
June 14, 2024 16:01
-
-
Save bionicles/10591b6ff6c3600a5eb4c548bb7b2fd7 to your computer and use it in GitHub Desktop.
Selected Bash Functions, Revision 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BEGIN APACHE-2.0 OR MIT SEGMENT | |
# edit your bashrc and bash_profile with vs code | |
alias eb="code ${HOME}/.bash_profile $HOME ${HOME}/.bashrc " | |
# source bash_profile (i.e. reload these functions) without losing conda or cwd | |
function sb { | |
local cur_dir=$(pwd) | |
local cur_env=${CONDA_DEFAULT_ENV:-$MY_DEFAULT_ENV} | |
source ~/.bash_profile | |
cd $cur_dir | |
conda activate $cur_env | |
} | |
export -f sb | |
# cat (show contents of) bashrc and bash_profile | |
function cb { | |
echo -e '\n~/.bashrc contents:\n' | |
bp ~/.bashrc | |
echo -e '\n~/.bash_profile contents:\n' | |
bp ~/.bash_profile | |
echo -e '\n' | |
} | |
export -f cb | |
# list all of the things | |
function la() { | |
local dir="${1:-.}" | |
dir=$(realpath "$dir") | |
echo "$ eza --time-style relative -laFh $dir" | |
command eza --time-style relative -laFh "$dir" | |
} | |
export -f la | |
# list all of the things, sorted smallest to largest | |
function laz() { | |
local dir="${1:-.}" | |
dir=$(realpath "$dir") | |
echo "$ eza --time-style relative --sort=size -laFh $dir" | |
command eza --time-style relative --sort=size -laFh "$dir" | |
} | |
export -f laz | |
# list all of the things, sorted largest to smallest | |
function lazr() { | |
local dir="${1:-.}" | |
dir=$(realpath "$dir") | |
echo "$ eza --time-style relative -s size -laFh $dir" | |
command eza --time-style relative -s size -laFhr "$dir" | |
} | |
export -f las | |
# list only directories | |
function lsd() { | |
local dir="${1:-.}" | |
dir=$(realpath "$dir") | |
echo "eza --time-style relative -ladFh $dir/*/" | |
command eza --time-style relative -ladFh "$dir"/*/ | |
} | |
export -f lsd | |
# display file line counts in ascending order | |
function wcl() { | |
echo "Lines:" | |
echo "$ wc -l * | sort -k 1" | |
command wc -l * | sort -k 1 | |
} | |
export -f wcl | |
# cat (show contents of) all the files in a directory | |
function cata() { | |
for file in "${1:-.}"/*; do echo -e "file: $file"; bp "$file"; echo; done; | |
} | |
export -f cata | |
# recursively show all the files in a directory using fd, ignoring binary files | |
function catr() { | |
local dir="${1:-.}" | |
for file in $(fd --exclude '*.{bin,exe,o,a,so,dll}' --size -1M . "$dir" 2> /dev/null); do | |
echo -e "file: $file" | |
echo "---" | |
batn "$file" | |
echo | |
done | |
} | |
export -f catr | |
# bat (show a file) without interactive paging | |
function bp() { | |
bat -pP "$1" && echo | |
} | |
export -f bp | |
# an alias for bat with line numbers | |
function batn() { | |
echo "Start $path" | |
bat --style=numbers --paging=never "$1" | |
echo "End $path" | |
} | |
export -f batn | |
# ripgrep search terminal history | |
function hrg() { | |
echo "$ history | rg $1" | |
command history | rg "$1" | |
} | |
export -f hrg | |
# print all of the makefiles in a directory tree | |
function bat_makefiles() { | |
find . -name 'node_modules' -prune -o -name 'Makefile' -print | while read path; do | |
echo "Start $path" | |
bat --style=numbers --paging=never "$path" | |
echo "End $path" | |
done | |
} | |
export -f bat_makefiles | |
# END APACHE-2.0 OR MIT SEGMENT | |
# BEGIN GPL-3.0 LICENSED SEGEMENT | |
# this is a nice working version of interactive ripgrep which is much simpler which i found on github and slightly modified | |
# https://github.com/DanielFGray/fzf-scripts/blob/master/igr | |
# this one opens vs code when you hit enter on a selection | |
function irg() { | |
declare preview='bat --color=always --style=header,numbers -H {2} {1} | grep -C3 {q}' | |
while getopts ':l' x; do | |
case "$x" in | |
l) list_files=1 | |
preview='bat --color=always --style=header,numbers {1} | grep -C3 {q}' | |
;; | |
esac | |
done | |
shift $(( OPTIND - 1 )) | |
unset x OPTARG OPTIND | |
rg --color=always -n ${list_files:+-l} "$1" 2> /dev/null | | |
fzf -d: \ | |
--ansi \ | |
--query="$1" \ | |
--phony \ | |
--bind="change:reload:rg -S -n ${list_files:+-l} --color=always {q}" \ | |
--bind='enter:execute:echo "code -rg {1}:{2}" && code -rg {1}:{2}' \ | |
--preview-window='up' \ | |
--preview="[[ -n {1} ]] && $preview" | |
} | |
export -f irg | |
# ` --bind='enter:execute:echo "vi {1}" && vi {1}' \` opens vi when you hit enter on a selection | |
# (needs jump-to-line with {1}:{2} but that seemed buggy) | |
function irgv() { | |
declare preview='bat --color=always --style=header,numbers -H {2} {1} | grep -C3 {q}' | |
while getopts ':l' x; do | |
case "$x" in | |
l) list_files=1 | |
preview='bat --color=always --style=header,numbers {1} | grep -C3 {q}' | |
;; | |
esac | |
done | |
shift $(( OPTIND - 1 )) | |
unset x OPTARG OPTIND | |
rg --color=always -n ${list_files:+-l} "$1" 2> /dev/null | | |
fzf -d: \ | |
--ansi \ | |
--query="$1" \ | |
--phony \ | |
--bind="change:reload:rg -S -n ${list_files:+-l} --color=always {q}" \ | |
--bind='enter:execute:echo "vi {1}" && vi {1}' \ | |
--preview-window='up' \ | |
--preview="[[ -n {1} ]] && $preview" | |
} | |
export -f irgv | |
# window options | |
# --preview-window=OPT Preview window layout (default: right:50%) | |
# [up|down|left|right][,SIZE[%]] | |
# [,[no]wrap][,[no]cycle][,[no]follow][,[no]hidden] | |
# [,border-BORDER_OPT] | |
# [,+SCROLL[OFFSETS][/DENOM]][,~HEADER_LINES] | |
# [,default] | |
# END GPL-3.0 LICENSED SEGEMENT |
Author
bionicles
commented
Jun 3, 2024
- edited to add "-S" on the ripgrep (smart case search)
- edited irg/irgv with preview and added the preview options
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment