2005-09-20 13:31:55 +00:00
\section function function - create a function
\subsection function-synopsis Synopsis
2006-06-08 23:58:11 +00:00
<code>function [OPTIONS] NAME; BODY; end </code>
2005-09-20 13:31:55 +00:00
\subsection function-description Description
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 07:56:01 +00:00
\c function creates a new function \c NAME with the body <code>BODY</code>.
2005-09-20 23:42:00 +00: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 07:56:01 +00: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 13:31:55 +00: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 07:56:01 +00:00
The following options are available:
2005-09-20 13:31:55 +00:00
2013-07-29 08:06:05 +00: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 07:56:01 +00: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 01:14:36 +00: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 07:56:01 +00: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 08:06:05 +00: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 07:56:01 +00: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 13:31:55 +00: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 07:56:01 +00:00
are inserted into the environment <a href="index.html#variables-arrays">variable array</a>
2013-07-29 08:06:05 +00:00
<code>$argv</code>. If the \c --argument-names option is provided, the arguments are
also assigned to names specified in that option.
2005-09-20 13:31:55 +00:00
2012-12-11 22:44:41 +00: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-16 22:25:31 +00: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 07:56:01 +00:00
- \c fish_prompt, which is emitted whenever a new fish prompt is about to be displayed.
- \c fish_command_not_found, which is emitted whenever a command lookup failed.
2008-01-16 22:25:31 +00:00
2005-09-20 13:31:55 +00:00
\subsection function-example Example
2006-09-19 14:52:03 +00:00
<pre>
function ll
2005-09-20 13:31:55 +00:00
ls -l $argv
2005-10-11 19:23:43 +00:00
end
2005-09-20 13:31:55 +00:00
</pre>
will run the \c ls command, using the \c -l option, while passing on any additional files and switches to \c ls.
2005-09-20 23:57:18 +00:00
2005-09-20 13:31:55 +00:00
<pre>
function mkdir -d "Create a directory and set CWD"
2013-01-01 21:56:08 +00:00
command mkdir $argv
2006-01-23 23:17:06 +00:00
if test $status = 0
2005-09-20 13:31:55 +00:00
switch $argv[(count $argv)]
case '-*'
2006-01-23 23:17:06 +00:00
2005-09-20 13:31:55 +00:00
case '*'
cd $argv[(count $argv)]
return
end
end
end
2006-01-23 23:17:06 +00:00
</pre>
2005-09-20 13:31:55 +00:00
2006-01-23 23:17:06 +00:00
will run the mkdir command, and if it is successful, change the
2005-09-20 13:31:55 +00:00
current working directory to the one just created.
2005-09-20 23:42:00 +00:00