2005-09-20 21:31:55 +08:00
\section function function - create a function
\subsection function-synopsis Synopsis
2006-06-09 07:58:11 +08:00
<code>function [OPTIONS] NAME; BODY; end </code>
2005-09-20 21:31:55 +08: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 15:56:01 +08:00
\c function creates a new function \c NAME with the body <code>BODY</code>.
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.
- <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>
2013-07-29 16:06:05 +08: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 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
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
- \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-17 06:25:31 +08:00
2005-09-20 21:31:55 +08:00
\subsection function-example Example
2006-09-19 22:52:03 +08:00
<pre>
function ll
2005-09-20 21:31:55 +08:00
ls -l $argv
2005-10-12 03:23:43 +08:00
end
2005-09-20 21:31:55 +08: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-21 07:57:18 +08:00
2005-09-20 21:31:55 +08:00
<pre>
function mkdir -d "Create a directory and set CWD"
2013-01-02 05:56:08 +08:00
command mkdir $argv
2006-01-24 07:17:06 +08:00
if test $status = 0
2005-09-20 21:31:55 +08:00
switch $argv[(count $argv)]
case '-*'
2006-01-24 07:17:06 +08:00
2005-09-20 21:31:55 +08:00
case '*'
cd $argv[(count $argv)]
return
end
end
end
2006-01-24 07:17:06 +08:00
</pre>
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