Skip to content

Commit 503711a

Browse files
prime-runkRHYME7
andauthored
add best fzf aliases ever (HyDE-Project#569)
* add best fzf aliases ever * add nvim to .zshrc | default same as hypr/hyde.conf * null nvim fallsback to vim * fix max_depth variable in fzf funtions * check this out * make fcd great again * fix fcd:listed files in last level and exclude .cache dir * change ffcd max-depth to 7 since its blazingly fast * remove ffsd * restored and renamed, ffe and ffec * add fzf_options for preview --------- Co-authored-by: Khing <[email protected]>
1 parent 55721cb commit 503711a

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

Configs/.zshenv

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,72 @@ function _load_omz_on_init() {
129129
fi
130130
}
131131

132+
# best fzf aliases ever
133+
_fuzzy_change_directory() {
134+
local initial_query="$1"
135+
local selected_dir
136+
local fzf_options=('--preview=ls -p {}' '--preview-window=right:60%')
137+
fzf_options+=(--height "80%" --layout=reverse --preview-window right:60% --cycle)
138+
local max_depth=7
139+
140+
if [[ -n "$initial_query" ]]; then
141+
fzf_options+=("--query=$initial_query")
142+
fi
143+
144+
#type -d
145+
selected_dir=$(find . -maxdepth $max_depth \( -name .git -o -name node_modules -o -name .venv -o -name target -o -name .cache \) -prune -o -type d -print 2>/dev/null | fzf "${fzf_options[@]}")
146+
147+
if [[ -n "$selected_dir" && -d "$selected_dir" ]]; then
148+
cd "$selected_dir" || return 1
149+
else
150+
return 1
151+
fi
152+
}
153+
154+
_fuzzy_edit_search_file_content() {
155+
# [f]uzzy [e]dit [s]earch [f]ile [c]ontent
156+
local selected_file
157+
selected_file=$(grep -irl "${1:-}" ./ | fzf --height "80%" --layout=reverse --preview-window right:60% --cycle --preview 'cat {}' --preview-window right:60%)
158+
159+
if [[ -n "$selected_file" ]]; then
160+
if command -v "$EDITOR" &>/dev/null; then
161+
"$EDITOR" "$selected_file"
162+
else
163+
echo "EDITOR is not specified. using vim. (you can export EDITOR in ~/.zshrc)"
164+
vim "$selected_file"
165+
fi
166+
167+
else
168+
echo "No file selected or search returned no results."
169+
fi
170+
}
171+
172+
_fuzzy_edit_search_file() {
173+
local initial_query="$1"
174+
local selected_file
175+
local fzf_options=()
176+
fzf_options+=(--height "80%" --layout=reverse --preview-window right:60% --cycle)
177+
local max_depth=5
178+
179+
if [[ -n "$initial_query" ]]; then
180+
fzf_options+=("--query=$initial_query")
181+
fi
182+
183+
# -type f: only find files
184+
selected_file=$(find . -maxdepth $max_depth -type f 2>/dev/null | fzf "${fzf_options[@]}")
185+
186+
if [[ -n "$selected_file" && -f "$selected_file" ]]; then
187+
if command -v "$EDITOR" &>/dev/null; then
188+
"$EDITOR" "$selected_file"
189+
else
190+
echo "EDITOR is not specified. using vim. (you can export EDITOR in ~/.zshrc)"
191+
vim "$selected_file"
192+
fi
193+
else
194+
return 1
195+
fi
196+
}
197+
132198
function _load_post_init() {
133199
#! Never load time consuming functions here
134200
_load_persistent_aliases
@@ -190,6 +256,8 @@ function _load_if_terminal {
190256
ZDOTDIR=/tmp
191257
zle -N zle-line-init _load_omz_on_init # Loads when the line editor initializes // The best option
192258

259+
# Below this line are the commands that are executed after the prompt appears
260+
193261
autoload -Uz add-zsh-hook
194262
# add-zsh-hook zshaddhistory load_omz_deferred # loads after the first command is added to history
195263
# add-zsh-hook precmd load_omz_deferred # Loads when shell is ready to accept commands
@@ -216,12 +284,17 @@ function _load_if_terminal {
216284
.3='cd ../../..' \
217285
.4='cd ../../../..' \
218286
.5='cd ../../../../..' \
219-
mkdir='mkdir -p' # Always mkdir a path (this doesn't inhibit functionality to make a single dir)
287+
mkdir='mkdir -p' \
288+
ffec='_fuzzy_edit_search_file_content' \
289+
ffcd='_fuzzy_change_directory' \
290+
ffe='_fuzzy_edit_search_file'
220291

221292
fi
222293

223294
}
224295

296+
#? Override this environment variable in ~/.zshrc
297+
225298
# cleaning up home folder
226299
PATH="$HOME/.local/bin:$PATH"
227300
XDG_CONFIG_DIR="${XDG_CONFIG_DIR:-"$(xdg-user-dir CONFIG)"}"

Configs/.zshrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212

1313
#  This is your file 
1414
# Add your configurations here
15+
# export EDITOR=nvim
16+
export EDITOR=code

0 commit comments

Comments
 (0)