Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make fish functions private

Tags:

fish

I defined several helper functions that I only use in one specific fish script. I put them inside the wrapper function but you can still find them via autocompletion.

How can I hide them from auto completion and limit their scope to private?

function outer
   function inner_func
       echo "I still find this function via automplete"
   end
end
like image 218
oschrenk Avatar asked Aug 01 '14 21:08

oschrenk


1 Answers

Fish does not have private functions, but it is possible this feature will be available in future versions. In the meantime, try using the following technique, a naming convention, or both.

functions -e function_name

Very close to what you need. You can use functions -e function_name before the end of the block to erase it from the global scope.

function outer
    function inner_func
        echo "I still find this function via automplete."
        echo "Not anymore!!"
        functions -e inner_func
    end
    # Let's test this!
    inner_func
end


$ outer
I still find this function via automplete.
Not anymore!!
$ inner_func
fish: Unknown command 'inner_func'

Naming Convention

  • _my_module_func_name

This does not erase the function from the global scope, but it is a general good practice to avoid overriding existing functions unintentionally.

Notes

Functions declared inside a main function file (or functions inside other functions like inner_func) will be available only after its parent function is invoked at least once (this is fish lazy function autoloading), and exist only for the duration of that particular shell session.

like image 190
Jorge Bucaran Avatar answered Oct 19 '22 20:10

Jorge Bucaran