DEV Community

Discussion on: Turn any Bash function/alias into a hybrid executable/source-able script.

Collapse
 
thereedybear profile image
Reed

I def like the idea of having a bash functions folder (DIRECTORY!! lol). Personally, i just append an export PATH to my ~/.bashrc. Mainly because i have a fairly specific directory structure for my open source projects & i like to keep that structure going & not everything in my dev/bash/PROJECT folders is necessarily setup to be executed.

I also have a personal library with a cli "framework" i made that i just add functions too, so its tlf [command group] [command].

I don't do any autocompletion though. Thats still something i have to try out. & i think my setup would require some changes to make that work. Didnt know it was so easy to do. But yeah, I'd have to source files to enable it to a greater degree than.

I have a script to start spacevim that would be a good candidate for your approach.

Collapse
 
thefluxapex profile image
Ian Pride

Didnt know it was so easy to do.
-- Reed

You can do far more complicated Bash Completion with functions using COMP variables, compgen, and more:

Generic example:

 $ complete -F func_name command_name
Enter fullscreen mode Exit fullscreen mode

where the func_name is, of course, the name of the function where you do the more complicated stuff with COMP variables and return a COMPREPLY:

function func_name {
    # ... calculate COMP stuff and more ...
    COMPREPLY=( $(compgen -W "<DO_STUFF>") )
   # DO_STUFF: calculated stuff from above or compgen inline expression 
}
Enter fullscreen mode Exit fullscreen mode

But that's stuff you don't always need in a completion most of the time you can do with a simple word (-W) list:

 $ complete -W "-h --help -a --add" command_name 
Enter fullscreen mode Exit fullscreen mode

or for some trickery:

tab toggling through a list of files only in a current directory:

 $ complete -W "$(find -maxdepth 1 -type f)" command_name
Enter fullscreen mode Exit fullscreen mode

or dirs only:

 $ complete -W "$(find -maxdepth 1 -type d)" command_name
Enter fullscreen mode Exit fullscreen mode

A lot can be done with this, including doing background stuff that may not be directly involved in your completion like logging file info, file stamps of latest files etc...

Collapse
 
thereedybear profile image
Reed

Thanks, if I ever get around to putting in the time/effort to set it up, this'll be v. helpful. Long as I remember to come back here lol.

Some comments have been hidden by the post's author - find out more