1+ # Navigation
2+ alias p="cd ~/Projects"
3+ alias e="code ."
4+
5+ # File/Search/List
6+ alias ls='eza -l --group-directories-first'
7+ alias la="eza -laF --group-directories-first"
8+ alias lsd='eza -l -D'
9+ alias tree='tre --limit 3'
10+ alias recent-file="exa -s modified --reverse | head -n 1"
11+ alias fs="stat -f \"%z bytes\""
12+ alias ds="du -sh"
13+
14+ # https://yazi-rs.github.io/docs/quick-start
15+ function y() {
16+ local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd
17+ yazi "$@" --cwd-file="$tmp"
18+ if cwd="$(command cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
19+ builtin cd -- "$cwd"
20+ fi
21+ rm -f -- "$tmp"
22+ }
23+
24+ ##################################
25+ # Local Written-by-me Code Search
26+ # https://mikebian.co/fast-local-written-by-you-code-search/
27+ ##################################
28+
29+ # create a local index of all git repos
30+ index_local_repositories() {
31+ fd --hidden --type=directory "\.git$" ~/Projects -x sh -c '
32+ repo=$(dirname "$0")
33+ origin=$(git -C "$repo" remote get-url origin 2>/dev/null)
34+ if [[ -z "$origin" ]] || [[ "$origin" == *"github.com/iloveitaly/"* ]]; then
35+ if [[ -z "$origin" ]] || ! git -C "$repo" remote get-url upstream &>/dev/null; then
36+ echo "$repo"
37+ fi
38+ fi
39+ ' {} \; > ~/.local-git-repos
40+ }
41+
42+ # ripgrep code search
43+ rgc() {
44+ if [[ ! -f ~/.local-git-repos ]]; then
45+ index_local_repositories
46+ fi
47+
48+ if [[ $(/usr/bin/find ~/.local-git-repos -mtime +7) ]]; then
49+ # reindex async if it hasn't been done in awhile
50+ index_local_repositories &
51+ fi
52+
53+ # an alternative to `cut` is using -r rg option to mutate the content output
54+ # https://github.com/BurntSushi/ripgrep/issues/2860
55+ rg --hidden --glob '!**/.git/**' "$@" $(cat ~/.local-git-repos) | \
56+ # we don't want column, just file and line
57+ cut -d: -f1-2 | \
58+ # now that we only have file + line, we can remove duplicates
59+ uniq | \
60+ # nth will still display `:`, which is annoying
61+ fzf --ansi --delimiter : --with-nth '1' \
62+ --keep-right \
63+ --preview 'height=$(tput lines); start={2}; context=$((height / 2)); bat --color=always --paging=never --highlight-line {2} --line-range $((start > context ? start - context : 1)):$((start + context)) {1}'
64+ }
65+
66+ ###########################
67+ # Local Filesystem Searches
68+ ###########################
69+
70+ # determine if we are in a node_modules folder or if a valid node_modules path was passed via any argument
71+ function _is_node_modules() {
72+ local node_modules_path=$1
73+
74+ if [[ -z "$node_modules_path" ]]; then
75+ node_modules_path=$(pwd)
76+ fi
77+
78+ [[ "$node_modules_path" =~ node_modules$ ]]
79+ }
80+
81+ function _is_venv() {
82+ local venv_path=$1
83+
84+ if [[ -z "$venv_path" ]]; then
85+ venv_path=$(pwd)
86+ fi
87+
88+ [[ "$venv_path" =~ \.venv$ ]]
89+ }
90+
91+ alias ff="fzf --preview='bat --color=always {}'"
92+
93+ # TODO this should use the `cd` thing and just overload there instead
94+ # fd + fzf, cd into the selected directory
95+ fdd() {
96+ local additional_args=""
97+
98+ if _is_node_modules "$@" || _is_venv "$@"; then
99+ additional_args="--no-ignore-vcs --hidden"
100+ fi
101+
102+ local file_or_directory=$(fd ${=additional_args} "$@" | fzf --preview 'if [ -d {} ]; then exa -1 --color=always {}; elif [ -f {} ]; then cat {}; fi')
103+
104+ # cd into dir or view the file
105+ if [[ -n "$file_or_directory" ]]; then
106+ if [[ -d "$file_or_directory" ]]; then
107+ cd "$file_or_directory"
108+ elif [[ -f "$file_or_directory" ]]; then
109+ cat "$file_or_directory"
110+ fi
111+ fi
112+ }
113+
114+ RIPGREP_LIVEGREP_EXCLUSIONS=("--glob=!.git/**" "--glob=!.venv" "--glob=!.pnpm" "--glob=!node_modules" "--glob=!.tox" "--glob=!cdk.out")
115+
116+ # TODO add fzf support
117+ # search all files in the current directory
118+ rgu() {
119+ # TODO use glob list from find-replace
120+ rg -uu "${RIPGREP_LIVEGREP_EXCLUSIONS[@]}" "$@"
121+ }
122+
123+ # livegrep-like local search
124+ # related: https://github.com/seletskiy/zsh-fuzzy-search-and-edit/blob/master/plugin.zsh
125+ rgg() {
126+ local additional_args=""
127+
128+ # TODO should check search page in $@ as well
129+ if [[ $(pwd) =~ node_modules$ ]] || [[ $(pwd) == *"/.venv/"* ]]; then
130+ # in this case, we want to search the contents of the node_modules directory
131+ # this is a common case for me, trying to debug various package bugs in a project I'm working on
132+ additional_args="--no-ignore-vcs --hidden"
133+ fi
134+
135+ # TODO --bind=\"ctrl-y:execute-silent(echo {} | cut -d: -f1 | tr -d '[:space:]' | pbcopy)\" \
136+ local file_and_location=$(rg --color always --hidden "$@" ${=additional_args} | fzf --ansi --delimiter : --with-nth '1,4' --preview 'height=$(tput lines); start={2}; context=$((height / 2)); bat --color=always --paging=never --highlight-line {2} --line-range $((start > context ? start - context : 1)):$((start + context)) {1}')
137+
138+ # Extracting the file name from the selected line
139+ local file=$(echo "$file_and_location" | awk -F: '{print $1}')
140+
141+ if [[ ! -z "$file" ]]; then
142+ open "$file"
143+ fi
144+ }
145+
146+ # ripgrep file
147+ rgf() {
148+ rg "$@" --files-with-matches | sort -u
149+ }
150+
151+ # ripgrep "only": only display the matching text in the first capture group
152+ # this is helpful for selecting and transforming
153+ rgo() {
154+ params=()
155+
156+ # check if the user is passing in `-r`, if not, then default to a basic option
157+ if ((${@[(I)*-r*]} == 0)); then
158+ params+=("-r \$1")
159+ fi
160+
161+ rg $@ $params --no-filename --no-column --no-line-number --color never --only-matching
162+ }
163+
164+ # find and replace across files in the current directory
165+ function find-replace() {
166+ if [ $# -lt 2 ]; then
167+ echo "Usage: find-replace <find_string> <replace_string>"
168+ return 1
169+ fi
170+
171+ local find_string=$1
172+ local replace_string=$2
173+ local files=( $(rg -uu --glob '!.git/**' --glob '!.venv' --glob '!.pnpm' --glob '!node_modules' --glob '!.tox' --glob '!cdk.out' -l --fixed-strings "$find_string") )
174+
175+ echo "${#files[@]} files found."
176+
177+ for file in "${files[@]}"; do
178+ # sd is modern replacement for sed
179+ sd --preview --fixed-strings "$find_string" "$replace_string" "$file"
180+
181+ read "confirmation?Confirm replace in '$file'. [y/n]:"
182+
183+ if [[ $confirmation =~ ^[Yy]$ ]]; then
184+ sd --fixed-strings "$find_string" "$replace_string" "$file"
185+ fi
186+ done
187+ }
188+
189+ ###########################
190+ # File opening functions
191+ ###########################
192+
193+ # open "yank.tmux:56:109:" at the right line in your terminal
194+ open_in_nano() {
195+ # Read the input string into variables, splitting by ':'
196+ IFS=':' read file line column _other <<< "$1"
197+ # Open the file at the specified line. Nano doesn't support specific column.
198+ nano +$line "$file" < /dev/tty
199+ }
200+
201+ open_in_vscode() {
202+ # git-fuzzy will pass a string that looks like `M bin/run.js` or ` M bin/run.js`
203+
204+ # write a ripgrep command which:
205+ # * ` M bin/run.js` => `bin/run.js`
206+ # * `test/scraper.test.js:25:40` => `test/scraper.test.js:25:40`
207+ # * `test/scraper.test.js` => `test/scraper.test.js`
208+ # * `test/scraper.test.js:25:40: something` => `test/scraper.test.js:25
209+ # assume each of inputs are passed on a single line and are only passed one at a time
210+
211+ local file=$(pcregrep -o2 "(^\s?M\s)?(.*)" <<< "$1")
212+ IFS=':' read file line column _other <<< "$file"
213+
214+ # remove multiple slashes that could have been passed from `file:///` references
215+ file=$(echo "$file" | sed 's|^/{2,}|/|')
216+
217+ code --goto "$file:$line:$column"
218+ }
219+
220+ open_in_cat() {
221+ # TODO jump to specific line number via less
222+ # wrapping doesn't work the way you would think:
223+ # bat Makefile --highlight-line 136 --paging=never --wrap=character --terminal-width=150
224+ # TODO https://github.com/sharkdp/bat/issues/2527
225+
226+ IFS=':' read file line column _other <<< "$1"
227+
228+ # output command executed via local shopt
229+ # setopt local_options xtrace
230+
231+ local height=$(tput lines)
232+ local start=$line
233+ local context=$((height / 2))
234+
235+ bat --color=always $file --highlight-line "$line" --line-range $((start > context ? start - context : 1)):$((start + context))
236+ }
0 commit comments