Co-authored-by: EmilySeville7cfg <EmilySeville7cf@gmail.com>
This commit is contained in:
parent
f5125bd1ec
commit
8a3d8c3449
308
fish-shell.md
308
fish-shell.md
|
@ -9,41 +9,81 @@ weight: -1
|
||||||
|
|
||||||
### Keys
|
### Keys
|
||||||
|
|
||||||
| Shortcut | Description |
|
| Shortcut | Description |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `^A ←` _/_ `^E →` | Move to line beginning/end |
|
| `^A ←`/`^E →` | Move to the line beginning/end |
|
||||||
| `Alt ←` _/_ `Alt →` | Move word |
|
| `Alt ←`/`Alt →` | Jump to the previous/next word |
|
||||||
| `^U` | Delete to beginning |
|
| `↑`/`↓` | Switch to the previous/next command |
|
||||||
| `^W` | Delete to previous `/` |
|
| `Alt ↑`/`Alt ↓` | Switch to the previous/next arguments |
|
||||||
| `^D` | Delete next character |
|
| --- | --- |
|
||||||
| `Alt D` | Delete next word |
|
| `^U` | Delete to the beginning |
|
||||||
| `^C` | Cancel line |
|
| `^C` | Cancel the line |
|
||||||
| `Alt P` | Page output |
|
| --- | --- |
|
||||||
| --- | --- |
|
| `Alt H` | Show the command man page description |
|
||||||
| `Alt ↑` _/_ `Alt ↓` | Previous _/_ next arguments |
|
| `Alt W` | Show the short command description |
|
||||||
| `Alt E` _/_ `Alt V` | Open in external editor |
|
|
||||||
| `^L` | Repaint screen |
|
|
||||||
{: .-shortcuts}
|
|
||||||
|
|
||||||
### Help
|
### Sample program
|
||||||
|
|
||||||
| `Alt H` | Help on word (man) |
|
```fish
|
||||||
| `Alt W` | Help on word (short descriptions) |
|
#!/usr/bin/env fish
|
||||||
| `Alt L` | List directory on cursor |
|
|
||||||
{: .-shortcuts}
|
echo 'Hello from Fish!'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Comments
|
||||||
|
|
||||||
|
```fish
|
||||||
|
# my comment
|
||||||
|
```
|
||||||
|
|
||||||
|
### Printing text
|
||||||
|
|
||||||
|
```fish
|
||||||
|
echo 'Hello from Fish!'
|
||||||
|
# or
|
||||||
|
printf '%s\n' 'Hello from Fish!'
|
||||||
|
```
|
||||||
|
|
||||||
|
Print the string with a trailing `\n`.
|
||||||
|
|
||||||
|
### Reading from stdin
|
||||||
|
|
||||||
|
```fish
|
||||||
|
read my_variable
|
||||||
|
```
|
||||||
|
|
||||||
|
Reads the string to a variable `my_variable`.
|
||||||
|
|
||||||
|
### Loops
|
||||||
|
|
||||||
|
```fish
|
||||||
|
for i in (seq 1 10)
|
||||||
|
...
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
## Variables
|
## Variables
|
||||||
|
|
||||||
### Defining and erasing
|
### Defining and erasing
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
set my_variable "Hello from Fish!"
|
# Declare the global/local variable:
|
||||||
```
|
set my_variable 'Hello from Fish!'
|
||||||
|
|
||||||
```fish
|
i# Remove the variable:
|
||||||
set --erase my_variable
|
set --erase my_variable
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Slicing
|
||||||
|
|
||||||
|
```fish
|
||||||
|
echo $my_variable[1..10]
|
||||||
|
echo $my_variable[2..]
|
||||||
|
echo $my_variable[..-2]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Numbers
|
||||||
|
|
||||||
### Incrementing and decrementing
|
### Incrementing and decrementing
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
|
@ -51,62 +91,62 @@ set my_variable (math $my_variable + 1)
|
||||||
set my_variable (math $my_variable - 1)
|
set my_variable (math $my_variable - 1)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Slicing
|
### Arithmetic
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
set my_variable $another_variable[1..10]
|
echo (math 1 + 2)
|
||||||
set my_variable $another_variable[2..]
|
|
||||||
set my_variable $another_variable[..-2]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Arithmetic
|
| Operator | Performs |
|
||||||
|
| --- | --- |
|
||||||
|
| `+` | Addition |
|
||||||
|
| `-` | Subtraction |
|
||||||
|
| `*` | Multiplication |
|
||||||
|
| `/` | Division |
|
||||||
|
| `%` | Modulo |
|
||||||
|
| `^` | Exponentiation |
|
||||||
|
|
||||||
```fish
|
## Strings
|
||||||
math 1 + 2
|
|
||||||
```
|
|
||||||
|
|
||||||
| Operator | Performs |
|
### Matching
|
||||||
| --- | --- |
|
|
||||||
| `+` | Addition |
|
|
||||||
| `-` | Subtraction |
|
|
||||||
| `*` | Multiplication |
|
|
||||||
| `/` | Division |
|
|
||||||
| `%` | Modulo |
|
|
||||||
| `^` | Exponentiation |
|
|
||||||
{: .-shortcuts}
|
|
||||||
|
|
||||||
## String manipulation
|
Match the string against a regular expresion:
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
string match --regex --entire 'Fish' 'Hello from Fish!'
|
string match --regex --entire 'Fish' 'Hello from Fish!'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
| Pattern | Matches |
|
||||||
|
| --- | --- |
|
||||||
|
| `x?` | Zero or one `x` chars |
|
||||||
|
| `x*` | Any count `x` chars |
|
||||||
|
| `x+` | One or more `x` chars |
|
||||||
|
| `x{n}` | n times `x` chars |
|
||||||
|
| `x{n,m}` | n to m times `x` chars |
|
||||||
|
| `x{n,}` | n or more times `x` chars |
|
||||||
|
| `[xy]` | `x` or y char |
|
||||||
|
| `[^xy]` | not `x` or y char |
|
||||||
|
| --- | --- |
|
||||||
|
| `\w` | Word character |
|
||||||
|
| `\d` | Digit character |
|
||||||
|
| `\W` | Not word character |
|
||||||
|
| `\D` | Not digit character |
|
||||||
|
|
||||||
|
Perl compatible regular expressions are described here.
|
||||||
|
|
||||||
|
### Replacing
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
|
# Replaces the first match
|
||||||
string replace --regex 'Fish' 'fish' 'Hello from Fish!'
|
string replace --regex 'Fish' 'fish' 'Hello from Fish!'
|
||||||
|
|
||||||
|
# Replaces all matches
|
||||||
|
string replace --regex --all 'Fish' 'fish' 'Hello from Fish!'
|
||||||
```
|
```
|
||||||
|
|
||||||
| Pattern | Matches |
|
## Conditionals
|
||||||
| --- | --- |
|
|
||||||
| `x?` | Zero or one `x` chars |
|
|
||||||
| `x*` | Any count `x` chars |
|
|
||||||
| `x+` | One or more `x` chars |
|
|
||||||
| `x{n}` | n times `x` chars |
|
|
||||||
| `x{n,m}` | n to m times `x` chars |
|
|
||||||
| `x{n,}` | n or more times `x` chars |
|
|
||||||
| `x{n,}` | n or more times `x` chars |
|
|
||||||
| `[xy] ` | `x` or y char |
|
|
||||||
| `[^xy]` | not `x` or y char |
|
|
||||||
{: .-shortcuts}
|
|
||||||
|
|
||||||
| Class | Description |
|
### If/else
|
||||||
| --- | --- |
|
|
||||||
| `\w` | Word character |
|
|
||||||
| `\d` | Digit character |
|
|
||||||
| `\W` | Not word character |
|
|
||||||
| `\D` | Not digit character |
|
|
||||||
{: .-shortcuts}
|
|
||||||
|
|
||||||
### Conditionals
|
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
if test $my_variable -lt $another_variable
|
if test $my_variable -lt $another_variable
|
||||||
|
@ -118,56 +158,88 @@ else
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
| Operator | Meaning |
|
### Comparisons
|
||||||
|
|
||||||
|
#### Numbers
|
||||||
|
|
||||||
|
| Number operator | Meaning |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `-lt` | [L]ess [t]han |
|
| `-lt` | [L]ess [t]han |
|
||||||
| `-eq` | [Eq]ual |
|
| `-eq` | [Eq]ual |
|
||||||
| `-gt` | [G]reater [t]han |
|
| `-gt` | [G]reater [t]han |
|
||||||
| `-le` | [L]ess than or [e]qual to |
|
| `-le` | [L]ess than or [e]qual to |
|
||||||
| `-ge` | [G]reater than or [e]qual to |
|
| `-ge` | [G]reater than or [e]qual to |
|
||||||
|
| `-ne` | [N]ot [E]qual |
|
||||||
|
|
||||||
|
#### Strings
|
||||||
|
|
||||||
|
| String operator | Meaning |
|
||||||
|
| --- | --- |
|
||||||
|
| `==` | [Eq]ual |
|
||||||
|
| `!=` | [N]ot [E]qual |
|
||||||
|
|
||||||
|
#### Files
|
||||||
|
|
||||||
|
| File operator | Meaning |
|
||||||
|
| --- | --- |
|
||||||
| `-f` | [F]ile exists |
|
| `-f` | [F]ile exists |
|
||||||
| `-d` | [D]irectory exists |
|
| `-d` | [D]irectory exists |
|
||||||
| `-r` | File or directory exists and [r]eadable |
|
| `-r` | File or directory exists and [r]eadable |
|
||||||
| `-w` | File or directory exists and [w]ritable |
|
| `-w` | File or directory exists and [w]ritable |
|
||||||
| `-x` | File or directory exists and e[x]ecutable |
|
| `-x` | File or directory exists and e[x]ecutable |
|
||||||
{: .-shortcuts}
|
|
||||||
|
|
||||||
## Loops
|
|
||||||
|
## Process communication
|
||||||
|
|
||||||
|
### Writing to files
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
for i in (seq 1 10)
|
# Overwrite file
|
||||||
...
|
echo 'Hello from Fish!' > my_file
|
||||||
end
|
|
||||||
|
# Append to file
|
||||||
|
echo 'Hello from Fish!' >> my_file
|
||||||
```
|
```
|
||||||
|
|
||||||
## Command substitution
|
### Piping
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
set my_variable (math $my_variable + 1)
|
my_command | another_command
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Passes the first command stdout output as an input to a second command.
|
||||||
|
|
||||||
|
### Command substitution
|
||||||
|
|
||||||
|
```fish
|
||||||
|
echo (math $my_variable + 1)
|
||||||
|
```
|
||||||
|
|
||||||
|
The `(...)` expression is substituted with the output of the command inside it.
|
||||||
|
|
||||||
|
|
||||||
|
### Process substitution
|
||||||
|
|
||||||
|
```fish
|
||||||
|
echo (math $my_variable + 1 | psub)
|
||||||
|
```
|
||||||
|
|
||||||
|
The `(... | psub)` expression is substituted with a temporary file with the command's output.
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
|
|
||||||
### Defining and erasing
|
### Defining and erasing
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
function my_function --description "My description"
|
# Declare the function
|
||||||
|
function my_function --description 'My description'
|
||||||
···
|
···
|
||||||
end
|
end
|
||||||
```
|
|
||||||
|
|
||||||
```fish
|
# Remove the function
|
||||||
functions --erase my_function
|
functions --erase my_function
|
||||||
```
|
```
|
||||||
|
|
||||||
### Event handling
|
|
||||||
|
|
||||||
```fish
|
|
||||||
function my_hook --on-event my_event
|
|
||||||
···
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
## Events
|
## Events
|
||||||
|
|
||||||
### Emitting
|
### Emitting
|
||||||
|
@ -176,56 +248,74 @@ end
|
||||||
emit my_event
|
emit my_event
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Emits an event that can be picked up by other functions.
|
||||||
|
|
||||||
|
### Event handling
|
||||||
|
|
||||||
|
```fish
|
||||||
|
function my_hook --on-event my_event
|
||||||
|
···
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Reacts to the `my_event` event.
|
||||||
|
|
||||||
## Abbreviations
|
## Abbreviations
|
||||||
|
|
||||||
### Defining and erasing
|
### Defining and erasing
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
abbr --add my_abbreviation echo "Hello from Fish!"
|
# Declare the abbreviation
|
||||||
|
abbr --add grh "git reset --hard HEAD"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
abbr --erase my_abbreviation
|
# Remove the abbreviation
|
||||||
|
abbr --erase grh
|
||||||
```
|
```
|
||||||
|
|
||||||
## Completions
|
## Completions
|
||||||
|
|
||||||
### Defining and erasing
|
### Defining completions
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
complete --command mycommand --arguments 'install uninstall'
|
complete --command mycommand --arguments 'install uninstall'
|
||||||
complete --command mycommand --short-option 'h' --long-option 'help' --description 'Display help'
|
complete --command mycommand --short-option 'h' --long-option 'help' --description 'Display help'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
| Option | Description |
|
||||||
|
| --- | --- |
|
||||||
|
| `--arguments` | Arguments to the command itself or option |
|
||||||
|
| `--short-option` | Short option |
|
||||||
|
| `--long-option` | Long option |
|
||||||
|
| `--no-files` | Don't suggest files |
|
||||||
|
| `--force-files` | Suggest files |
|
||||||
|
| `--condition` | Display the hint only when a given condition is true |
|
||||||
|
| `--description` | Description |
|
||||||
|
|
||||||
|
Declares the completion for a command.
|
||||||
|
|
||||||
|
### Removing completions
|
||||||
|
|
||||||
```fish
|
```fish
|
||||||
complete --command mycommand --erase
|
complete --command mycommand --erase
|
||||||
```
|
```
|
||||||
|
|
||||||
| Option | Description |
|
|
||||||
| --- | --- |
|
|
||||||
| `--arguments` | Arguments to command itself or option |
|
|
||||||
| `--short-option` | Short option |
|
|
||||||
| `--long-option` | Long option |
|
|
||||||
| `--no-files` | Don't suggest files |
|
|
||||||
| `--force-files` | Suggest files |
|
|
||||||
| `--condition` | Display hint only when condition is true |
|
|
||||||
| `--description` | Description |
|
|
||||||
{: .-shortcuts}
|
|
||||||
|
|
||||||
## Useful built-in functions
|
## Useful built-in functions
|
||||||
|
|
||||||
| Condition | Description
|
| Function | Description |
|
||||||
| --- | ---
|
| --- | --- |
|
||||||
| `-n __fish_complete_directories STRING DESCRIPTION` | performs path completion on STRING, allowing only directories, and giving them the description DESCRIPTION.
|
| `__fish_seen_argument` | Check whether the specified argument is used |
|
||||||
| `-n __fish_complete_path STRING DESCRIPTION` | performs path completion on STRING, giving them the description DESCRIPTION.
|
| `__fish_seen_subcommand_from` | Check whether the specified subcommand is used |
|
||||||
| `-n __fish_complete_groups` | prints a list of all user groups with the groups members as description.
|
| `__fish_use_subcommand` | Check whether any subcommand is used |
|
||||||
| `-n __fish_complete_pids` | prints a list of all processes IDs with the command name as description.
|
| --- | --- |
|
||||||
| `-n __fish_complete_suffix SUFFIX` | performs file completion allowing only files ending in SUFFIX. The mimetype database is used to find a suitable description.
|
| `__fish_complete_directories` | Complete directories with the specified letters in their name |
|
||||||
| `-n __fish_complete_users` | prints a list of all users with their full name as description.
|
| `__fish_complete_suffix` | Complete files with the specified suffix |
|
||||||
| `-n __fish_print_filesystems` | prints a list of all known file systems. Currently, this is a static list, and not dependent on what file systems the host operating system actually understands.
|
| --- | --- |
|
||||||
| `-n __fish_print_hostnames` | prints a list of all known hostnames. This functions searches the fstab for nfs servers, ssh for known hosts and checks the /etc/hosts file.
|
| `__fish_complete_users` | List all users |
|
||||||
| `-n __fish_print_interfaces` | prints a list of all known network interfaces.
|
| `__fish_complete_groups` | List all user groups |
|
||||||
| `-n __fish_print_packages` | prints a list of all installed packages. This function currently handles Debian, rpm and Gentoo packages.
|
| `__fish_print_hostnames` | List all host names |
|
||||||
| `-n __fish_use_subcommand` |
|
| `__fish_complete_pids` | List all PIDs |
|
||||||
| `-n __fish_seen_subcommand_from init` |
|
| `__fish_print_filesystems` | List all known filesystems |
|
||||||
|
| `__fish_print_interfaces` | List all network interfaces |
|
||||||
|
|
Loading…
Reference in New Issue