OSDN Git Service

ec4c8b9035129801521cc2c41d05ee2206abdc45
[twpd/master.git] / fish-shell.md
1 ---
2 title: Fish shell
3 category: CLI
4 layout: 2017/sheet
5 prism_languages: [fish]
6 updated: 2018-01-31
7 weight: -1
8 ---
9
10 ### Keys
11
12 | Shortcut            | Description                 |
13 | ---                 | ---                         |
14 | `^A ←` _/_ `^E →`   | Move to line beginning/end  |
15 | `Alt ←` _/_ `Alt →` | Move word                   |
16 | `^U`                | Delete to beginning         |
17 | `^W`                | Delete to previous `/`      |
18 | `^D`                | Delete next character       |
19 | `Alt D`             | Delete next word            |
20 | `^C`                | Cancel line                 |
21 | `Alt P`             | Page output                 |
22 | ---                 | ---                         |
23 | `Alt ↑` _/_ `Alt ↓` | Previous _/_ next arguments |
24 | `Alt E` _/_ `Alt V` | Open in external editor     |
25 | `^L`                | Repaint screen              |
26 {: .-shortcuts}
27
28 ### Help
29
30 | `Alt H` | Help on word (man)                     |
31 | `Alt W` | Help on word (short descriptions)      |
32 | `Alt L` | List directory on cursor               |
33 {: .-shortcuts}
34
35 ## Variables
36
37 ### Defining and erasing
38
39 ```fish
40 set my_variable "Hello from Fish!"
41 ```
42
43 ```fish
44 set --erase my_variable
45 ```
46
47 ### Incrementing and decrementing
48
49 ```fish
50 set my_variable (math $my_variable + 1)
51 set my_variable (math $my_variable - 1)
52 ```
53
54 ### Slicing
55
56 ```fish
57 set my_variable $another_variable[1..10]
58 set my_variable $another_variable[2..]
59 set my_variable $another_variable[..-2]
60 ```
61
62 ## Arithmetic
63
64 ```fish
65 math 1 + 2
66 ```
67
68 | Operator            | Performs                    |
69 | ---                 | ---                         |
70 | `+`                 | Addition                    |
71 | `-`                 | Subtraction                 |
72 | `*`                 | Multiplication              |
73 | `/`                 | Division                    |
74 | `%`                 | Modulo                      |
75 | `^`                 | Exponentiation              |
76 {: .-shortcuts}
77
78 ## String manipulation
79
80 ```fish
81 string match --regex --entire 'Fish' 'Hello from Fish!'
82 ```
83
84 ```fish
85 string replace --regex 'Fish' 'fish' 'Hello from Fish!'
86 ```
87
88 | Pattern             | Matches                     |
89 | ---                 | ---                         |
90 | `x?`                | Zero or one `x` chars       |
91 | `x*`                | Any count `x` chars         |
92 | `x+`                | One or more  `x` chars      |
93 | `x{n}`              | n times `x` chars           |
94 | `x{n,m}`            | n to m times `x` chars      |
95 | `x{n,}`             | n or more times `x` chars   |
96 | `x{n,}`             | n or more times `x` chars   |
97 | `[xy] `             | `x` or y char               |
98 | `[^xy]`             | not `x` or y char           |
99 {: .-shortcuts}
100
101 | Class               | Description                 |
102 | ---                 | ---                         |
103 | `\w`                | Word character              |
104 | `\d`                | Digit character             |
105 | `\W`                | Not word character          |
106 | `\D`                | Not digit character         |
107 {: .-shortcuts}
108
109 ### Conditionals
110
111 ```fish
112 if test $my_variable -lt $another_variable
113   ···
114 else if test $my_variable -eq $another_variable
115   ···
116 else
117   ···
118 end
119 ```
120
121 | Operator            | Meaning                                   |
122 | ---                 | ---                                       |
123 | `-lt`               | [L]ess [t]han                             |
124 | `-eq`               | [Eq]ual                                   |
125 | `-gt`               | [G]reater [t]han                          |
126 | `-le`               | [L]ess than or [e]qual to                 |
127 | `-ge`               | [G]reater than or [e]qual to              |
128 | `-f`                | [F]ile exists                             |
129 | `-d`                | [D]irectory exists                        |
130 | `-r`                | File or directory exists and [r]eadable   |
131 | `-w`                | File or directory exists and [w]ritable   |
132 | `-x`                | File or directory exists and e[x]ecutable |
133 {: .-shortcuts}
134
135 ## Loops
136
137 ```fish
138 for i in (seq 1 10)
139   ...
140 end
141 ```
142
143 ## Command substitution
144
145 ```fish
146 set my_variable (math $my_variable + 1)
147 ```
148
149 ## Functions
150
151 ### Defining and erasing
152
153 ```fish
154 function my_function --description "My description"
155   ···
156 end
157 ```
158
159 ```fish
160 functions --erase my_function
161 ```
162
163 ### Event handling
164
165 ```fish
166 function my_hook --on-event my_event
167   ···
168 end
169 ```
170
171 ## Events
172
173 ### Emitting
174
175 ```fish
176 emit my_event
177 ```
178
179
180 ## Abbreviations
181
182 ### Defining and erasing
183
184 ```fish
185 abbr --add my_abbreviation echo "Hello from Fish!"
186 ```
187
188 ```fish
189 abbr --erase my_abbreviation
190 ```
191
192 ## Completions
193
194 ### Defining and erasing
195
196 ```fish
197 complete --command mycommand --arguments 'install uninstall'
198 complete --command mycommand --short-option 'h' --long-option 'help' --description 'Display help'
199 ```
200
201 ```fish
202 complete --command mycommand --erase
203 ```
204
205 | Option              | Description                               |
206 | ---                 | ---                                       |
207 | `--arguments`       | Arguments to command itself or option     |
208 | `--short-option`    | Short option                              |
209 | `--long-option`     | Long option                               |
210 | `--no-files`        | Don't suggest files                       |
211 | `--force-files`     | Suggest files                             |
212 | `--condition`       | Display hint only when condition is true  |
213 | `--description`     | Description                               |
214 {: .-shortcuts}
215
216 ## Useful built-in functions
217
218 | Condition | Description
219 | --- | ---
220 | `-n __fish_complete_directories STRING DESCRIPTION` | performs path completion on STRING, allowing only directories, and giving them the description DESCRIPTION.
221 | `-n __fish_complete_path STRING DESCRIPTION` | performs path completion on STRING, giving them the description DESCRIPTION.
222 | `-n __fish_complete_groups` | prints a list of all user groups with the groups members as description.
223 | `-n __fish_complete_pids` | prints a list of all processes IDs with the command name as description.
224 | `-n __fish_complete_suffix SUFFIX` | performs file completion allowing only files ending in SUFFIX. The mimetype database is used to find a suitable description.
225 | `-n __fish_complete_users` | prints a list of all users with their full name as description.
226 | `-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.
227 | `-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.
228 | `-n __fish_print_interfaces` | prints a list of all known network interfaces.
229 | `-n __fish_print_packages` | prints a list of all installed packages. This function currently handles Debian, rpm and Gentoo packages.
230 | `-n __fish_use_subcommand` |
231 | `-n __fish_seen_subcommand_from init` |