Home >System Tutorial >LINUX >How To Manage Functions In Fish Shell On Linux
This tutorial explains how to manage Fish shell functions using the functions
command. We'll cover creating, listing, viewing, editing, saving, copying, and deleting functions, with examples and best practices.
Table of Contents
functions
Command?functions
Command?config.fish
functions
Command OptionsWhat is the functions
Command?
The Fish shell functions
command is a built-in utility for managing user-defined functions. It lets you list, inspect, modify, and remove functions.
When to Use the functions
Command?
Use the functions
command for:
Managing Functions in Fish Shell
You can define functions either within your main configuration file (~/.config/fish/config.fish
) or in separate files within a custom functions directory (~/.config/fish/functions/
).
config.fish
Functions defined in config.fish
load automatically each time you start a new Fish shell.
Example:
function cdls builtin cd $argv[1] and begin echo "Changed to directory: $PWD" timeout 1s ls -l end end
Pros: Simple for small functions; always loaded.
Cons: Can clutter config.fish
; changes require restarting Fish; slower startup with many functions.
Saving functions to ~/.config/fish/functions/
allows Fish to load them only when needed, improving startup speed.
First, define the function:
function cdls builtin cd $argv[1] and begin echo "Changed to directory: $PWD" timeout 1s ls -l end end
Then, save it permanently using funcsave
:
funcsave cdls
This creates ~/.config/fish/functions/cdls.fish
.
Pros: Faster startup; better organization; easier to remove functions.
Cons: Requires an extra funcsave
step.
config.fish
for small, essential functions.Use functions
to display all defined functions:
function cdls builtin cd $argv[1] and begin echo "Changed to directory: $PWD" timeout 1s ls -l end end
View a specific function's code using:
function cdls builtin cd $argv[1] and begin echo "Changed to directory: $PWD" timeout 1s ls -l end end
For example: functions cdls
Edit a function using:
funcsave cdls
This opens the function in your default editor. Save and close to apply changes.
Save a function's definition to a file:
functions
Copy a function using the --copy
flag:
functions <function_name></function_name>
Delete a function using the --erase
flag:
funced <function_name></function_name>
To permanently remove a function from the functions directory, delete the corresponding .fish
file (e.g., rm ~/.config/fish/functions/cdls.fish
). Restart your shell or run exec fish
for changes to take effect.
Summary of functions
Command Options
Command | Description | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
List all functions. | ||||||||||||||
functions <name></name> |
Display the code of function <name></name> . |
||||||||||||||
funced <name></name> |
Edit function <name></name> in your default editor. |
||||||||||||||
functions --erase <name></name> |
Delete function <name></name> . |
||||||||||||||
functions --copy <old> <new></new></old> |
Copy function <old></old> to <new></new> . |
||||||||||||||
functions <name> > file.fish</name> |
Save function <name></name> to file.fish . |
Conclusion
The functions
command is a powerful tool for managing functions in Fish shell, offering efficiency and flexibility for customizing your shell environment. Remember to consult the official Fish shell documentation for more advanced usage.
(Note: The image reference is invalid and cannot be included here.)
The above is the detailed content of How To Manage Functions In Fish Shell On Linux. For more information, please follow other related articles on the PHP Chinese website!