2005-09-20 21:31:55 +08:00
\section function function - create a function
\subsection function-synopsis Synopsis
2014-08-01 20:25:41 +08:00
\fish{synopsis}
2014-08-01 10:37:32 +08:00
function [OPTIONS] NAME; BODY; end
\endfish
2005-09-20 21:31:55 +08:00
\subsection function-description Description
2014-08-01 10:37:32 +08:00
`function` creates a new function `NAME` with the body `BODY`.
2005-09-21 07:42:00 +08:00
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 15:56:01 +08:00
A function is a list of commands that will be executed when the name of the
function is given as a command.
2005-09-20 21:31:55 +08:00
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 15:56:01 +08:00
The following options are available:
2005-09-20 21:31:55 +08:00
2013-07-29 16:06:05 +08:00
- <code>-a NAMES</code> or <code>--argument-names NAMES</code> assigns the value of successive command-line arguments to the names given in NAMES.
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 15:56:01 +08:00
- <code>-d DESCRIPTION</code> or \c --description=DESCRIPTION is a description of what the function does, suitable as a completion description.
2014-08-16 09:14:36 +08:00
- <code>-w WRAPPED_COMMAND</code> or \c --wraps=WRAPPED_COMMAND causes the function to inherit completions from the given wrapped command. See the documentation for \c complete for more information.
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 15:56:01 +08:00
- <code>-e</code> or <code>--on-event EVENT_NAME</code> tells fish to run this function when the specified named event is emitted. Fish internally generates named events e.g. when showing the prompt.
- <code>-j PID</code> or <code> --on-job-exit PID</code> tells fish to run this function when the job with group ID PID exits. Instead of PID, the string 'caller' can be specified. This is only legal when in a command substitution, and will result in the handler being triggered by the exit of the job which created this command substitution.
- <code>-p PID</code> or <code> --on-process-exit PID</code> tells fish to run this function when the fish child process with process ID PID exits.
- <code>-s</code> or <code>--on-signal SIGSPEC</code> tells fish to run this function when the signal SIGSPEC is delivered. SIGSPEC can be a signal number, or the signal name, such as SIGHUP (or just HUP).
2013-07-29 16:06:05 +08:00
- \c -S or \c --no-scope-shadowing allows the function to access the variables of calling functions. Normally, any variables inside the function that have the same name as variables from the calling function are "shadowed", and their contents is independent of the calling function.
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 15:56:01 +08:00
- <code>-v</code> or <code>--on-variable VARIABLE_NAME</code> tells fish to run this function when the variable VARIABLE_NAME changes value.
2005-09-20 21:31:55 +08:00
If the user enters any additional arguments after the function, they
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 15:56:01 +08:00
are inserted into the environment <a href="index.html#variables-arrays">variable array</a>
2014-08-01 10:37:32 +08:00
`$argv`. If the `--argument-names` option is provided, the arguments are
2013-07-29 16:06:05 +08:00
also assigned to names specified in that option.
2005-09-20 21:31:55 +08:00
2012-12-12 06:44:41 +08:00
By using one of the event handler switches, a function can be made to run automatically at specific events. The user may generate new events using the <a href="#emit">emit</a> builtin. Fish generates the following named events:
2008-01-17 06:25:31 +08:00
2014-08-01 10:37:32 +08:00
- `fish_prompt`, which is emitted whenever a new fish prompt is about to be displayed.
- `fish_command_not_found`, which is emitted whenever a command lookup failed.
2008-01-17 06:25:31 +08:00
2005-09-20 21:31:55 +08:00
\subsection function-example Example
2014-08-01 10:37:32 +08:00
\fish
2006-09-19 22:52:03 +08:00
function ll
2014-08-01 10:37:32 +08:00
ls -l $argv
2005-10-12 03:23:43 +08:00
end
2014-08-01 10:37:32 +08:00
\endfish
2005-09-20 21:31:55 +08:00
2014-08-01 10:37:32 +08:00
will run the `ls` command, using the `-l` option, while passing on any additional files and switches to `ls`.
2005-09-21 07:57:18 +08:00
2014-08-01 10:37:32 +08:00
\fish
2005-09-20 21:31:55 +08:00
function mkdir -d "Create a directory and set CWD"
2014-08-01 10:37:32 +08:00
command mkdir $argv
if test $status = 0
switch $argv[(count $argv)]
case '-*'
case '*'
cd $argv[(count $argv)]
return
end
end
2005-09-20 21:31:55 +08:00
end
2014-08-01 10:37:32 +08:00
\endfish
2005-09-20 21:31:55 +08:00
2006-01-24 07:17:06 +08:00
will run the mkdir command, and if it is successful, change the
2005-09-20 21:31:55 +08:00
current working directory to the one just created.
2005-09-21 07:42:00 +08:00