Skip to content

Instantly share code, notes, and snippets.

@hashier
Created March 9, 2020 12:37
Show Gist options
  • Save hashier/d0bc5396461e3f99116d63cebc59fdfe to your computer and use it in GitHub Desktop.
Save hashier/d0bc5396461e3f99116d63cebc59fdfe to your computer and use it in GitHub Desktop.

Where do the files go?:

~/.config/fish/functions/fish_user_key_bindings.fish ~/.config/fish/functions/fzf-smart-completion-setup.fish

Source:

https://feltrac.co/environment/2020/01/18/build-your-own-shell-completion.html

Changes:

  • Some "" quotes included "" which didn't work (for me), fixed that
  • Fixed to consistent 4 space indent
  • Use setup function to setup smart completion
  • Use fish_user_key_bindings to call setup function

Does it work:

  • Go to a git folder that has some changes
  • Type git add<SPACE> and then ALT-t. If you see changed files and a preview next to it, \o/
function fish_user_key_bindings
fzf_key_bindings
fzf-smart-completion-setup
end
function get-completion-command
set -l cmd (commandline)
switch $cmd
case 'doer *'
echo 'doer -list'
case 'go test *'
echo 'git ls-files | grep _test.go'
case 'yarn test *'
echo 'git ls-files | grep .test.ts'
case 'git add *'
echo 'git diff --name-only'
echo '--bind="ctrl-space:toggle-preview" --preview "git diff --color=always {}" -m'
case '*'
return 1
end
end
function fzf-smart-completion-setup
bind -M insert \et fzf-smart-completion
bind \et fzf-smart-completion
end
function fzf-smart-completion -d "List files and folders"
set -l commandline (__fzf_parse_commandline)
set -l dir $commandline[1]
set -l fzf_query $commandline[2]
# use our cool new completion checker
set -l FZF_CMD (get-completion-command); or return
# fzf edge case and formatting (prevents fzf from taking up the whole screen)
set FZF_HEIGHT 40%
begin
set -lx FZF_DEFAULT_OPTS "--height $FZF_HEIGHT --reverse $FZF_DEFAULT_OPTS $FZF_CMD[2]"
eval "$FZF_CMD[1] | "(__fzfcmd)' -m --query "'$fzf_query'"' | while read -l r; set result $result $r; end
end
if [ -z "$result" ]
commandline -f repaint
return
else
# Remove last token from commandline.
commandline -t ""
end
for i in $result
commandline -it -- (string escape $i)
commandline -it -- ' '
end
commandline -f repaint
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment