Last active
November 9, 2023 21:31
-
-
Save bazzargh/05737c510dd742f99cae8c1d5c50f4f0 to your computer and use it in GitHub Desktop.
insert prepared snippets when you press Esc-j, with fuzzy search
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
# insert prepared snippets when you press Esc-j, with fuzzy search | |
fu-complete() { | |
# the whole command line up to last pipe left of the cursor | |
local prefix=$(printf "%s" "${LBUFFER}"|sed -E 's/(.*[|] *).*/\1/') | |
# everything left of cursor after the last pipe, gets used as the fzf prompt | |
local suffix=$(printf "%s" "${LBUFFER}"|sed -E 's/.*[|] *//') | |
# Grab snippets from ~/.cmd-line-fu . The file can have optional one-line comments before | |
# commands, which appear in fzf inlined and are searchable, eg: | |
# # print first column | |
# awk '{print $1}' | |
# appears as: | |
# awk '{print $1}' ## print first column | |
# and will filter if you type 'column'. | |
# since comments result in long lines and the picker doesn't wrap, | |
# the preview panel is set up to show the wrapped comment and command. | |
local guess=$(\ | |
awk '/^#/{c=" #"$0}/^[^#]/{printf"%s%s\n",$0,c;c=""}' ~/.cmd-line-fu \ | |
| fzf --query="${suffix}" \ | |
--prompt="${suffix}>" \ | |
--preview="printf '%s' {}|sed -E 's/(.*) ##(.*)/#\2\n\1/'|bat -l zsh --color=always --style=plain" \ | |
--preview-window=wrap \ | |
--header="${prefix}" \ | |
) | |
local ret=$? | |
LBUFFER="${prefix}${guess/ ##*/}" | |
zle reset-prompt | |
return $ret | |
} | |
zle -N fu-complete | |
# bind `alt + j` to fu-complete | |
bindkey '\ej' fu-complete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment