Hacking up your own shell completion
feltrac.co
feltrac.co
- of course, command completion
- type `echo $` and press tab - fzf through all your environment variables
- type `command ` and press tab - fzf through all programs available in your PATH
- type `builtin ` and press tab - fzf through all of zsh's builtin functions
- select multiple results at once, they'll be concatenated with spaces
- pick files, with the ability to press / to select a directory and automatically restart fzf-tab inside of it
No idea about out-of-the-box support for any of the other completions from maddyboo's post :/.
What I do is that I have custom bash history (with no limit on how many item there can be), that stores also an active directory, and id of the shell session. Then when I hit ctrl-R, it pipe that history into the fzf in special order: things from my current session history, then from the current directory, and then rest. This means, that if I come 2 months later to some older project and want to remember how to run tests / deploy, I just hit ctrl-r, and I find what I need.
However, in my `.bashrc`, I have:
log_bash_persistent_history()
{
[[
$(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
]]
local date_part="${BASH_REMATCH[1]}"
local command_part="${BASH_REMATCH[2]}"
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
then
echo $date_part "|" "$command_part" >> ~/.persistent_history
export PERSISTENT_HISTORY_LAST="$command_part"
fi
}
# Stuff to do on PROMPT_COMMAND
run_on_prompt_command()
{
log_bash_persistent_history
}
PROMPT_COMMAND="run_on_prompt_command"
HISTTIMEFORMAT="%d/%m/%y %T "
alias phgrep='cat ~/.persistent_history|grep --color'
alias hgrep='history|grep --color'
This doesn't save all of the information like the current shell, working directory, etc... but you could easily modify it to include that information. It also doesn't do all of the context-aware piping, you have to rely on grep. But just having a really good persistent history is itself insanely useful.My setup was itself stolen from someone else on HN a few years ago, and I've since forgotten their name.
Here are pointers: This install fzf into your bash, and apply small patch on fzf scripts to use bash history: https://github.com/mic47/linux-configuration/blob/master/scr... Some more fzf stuff to put into your bashrc: https://github.com/mic47/linux-configuration/blob/master/bas... This is .super_history setup: https://github.com/mic47/linux-configuration/blob/master/bas... https://github.com/mic47/linux-configuration/blob/master/bas...
This reminds me that I should probably modularize my setup.
Seems like ncurses with simple structured data and the right hooks could do something close to what CLUI is doing.
Could be worth it for others to check if such a feature exists for the framework they're using.
https://github.com/oilshell/oil/wiki/Shell-Autocompletion
https://github.com/oilshell/oil/wiki/Projects-Already-Doing-...
https://github.com/mbrubeck/compleat (one of the only grammar-based ones, looks nice but doesn't appear to have been used very much)
There are some discussions about cross-shell autocompletion on the #shell-autocompletion channel of https://oilshell.zulipchat.com/, but it didn't go that far.
The Ion work described in the sibling blog post went the furthest in terms of implementation I think, but I haven't heard anything about it in awhile.
By 'declarative' I mean some way of telling the shell how my bin can provide completions itself.
E.g. for the AWS util, I am envisioning something like
aws ec2 describe-instances --instance-id __INSTANCEID__
__INSTANCEID__: $(aws ec2 list-instances --query Instance.ID)
Syntax here is not anything like a well thought out proposal, just a demonstration of what I believe to be a shell agnostic method of describing both 'where can I complete?' and 'how can I compete it?'Features I wanted to include originally were
1 shell completion autogeneration from `--help` texts;
2 support bash/zsh/fish;
3 support toml-like description of command arguments.
At the moment I have a tool that generates completions from `--help` messages and supports bash/zsh. I use it daily and it makes me little bit happier.Actually when I started this project I used fish as my main shell on laptop and first versions of cod did support fish, but then I decided to focus bash/zsh as they look more popular.
I still want to add fish support when I have some time though. It doesn't look complicated.