OSDN Git Service

Regular updates
[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 the line beginning/end        |
15 | `Alt ←`/`Alt →`     | Jump to the previous/next word        |
16 | `↑`/`↓`             | Switch to the previous/next command   |
17 | `Alt ↑`/`Alt ↓`     | Switch to the previous/next arguments |
18 | ---                 | ---                                   |
19 | `^U`                | Delete to the beginning               |
20 | `^C`                | Cancel the line                       |
21 | ---                 | ---                                   |
22 | `Alt H`             | Show the command man page description |
23 | `Alt W`             | Show the short command description    |
24 | ---                 | ---                                   |
25 | `Alt .`             | Repeat last argument                  |
26
27 ### Sample program
28
29 ```fish
30 #!/usr/bin/env fish
31
32 echo 'Hello from Fish!'
33 ```
34
35 ### Comments
36
37 ```fish
38 # my comment
39 ```
40
41 ### Printing text
42
43 ```fish
44 echo 'Hello from Fish!'
45 # or
46 printf '%s\n' 'Hello from Fish!'
47 ```
48
49 Print the string with a trailing `\n`.
50
51 ### Reading from stdin
52
53 ```fish
54 read my_variable
55 ```
56
57 Reads the string to a variable `my_variable`.
58
59 ### Loops
60
61 ```fish
62 for i in (seq 1 10)
63   ...
64 end
65 ```
66
67 ## Variables
68
69 ### Defining and erasing
70
71 ```fish
72 # Declare the global/local variable:
73 set my_variable 'Hello from Fish!'
74
75 i# Remove the variable:
76 set --erase my_variable
77 ```
78
79 ### Slicing
80
81 ```fish
82 echo $my_variable[1..10]
83 echo $my_variable[2..]
84 echo $my_variable[..-2]
85 ```
86
87 ## Numbers
88
89 ### Incrementing and decrementing
90
91 ```fish
92 set my_variable (math $my_variable + 1)
93 set my_variable (math $my_variable - 1)
94 ```
95
96 ### Arithmetic
97
98 ```fish
99 echo (math 1 + 2)
100 ```
101
102 | Operator            | Performs       |
103 | ---                 | ---            |
104 | `+`                 | Addition       |
105 | `-`                 | Subtraction    |
106 | `*`                 | Multiplication |
107 | `/`                 | Division       |
108 | `%`                 | Modulo         |
109 | `^`                 | Exponentiation |
110
111 ## Strings
112
113 ### Matching
114
115 Match the string against a regular expresion:
116
117 ```fish
118 string match --regex --entire 'Fish' 'Hello from Fish!'
119 ```
120
121 | Pattern             | Matches                   |
122 | ---                 | ---                       |
123 | `x?`                | Zero or one `x` chars     |
124 | `x*`                | Any count `x` chars       |
125 | `x+`                | One or more  `x` chars    |
126 | `x{n}`              | n times `x` chars         |
127 | `x{n,m}`            | n to m times `x` chars    |
128 | `x{n,}`             | n or more times `x` chars |
129 | `[xy]`              | `x` or y char             |
130 | `[^xy]`             | not `x` or y char         |
131 | ---                 | ---                 |
132 | `\w`                | Word character      |
133 | `\d`                | Digit character     |
134 | `\W`                | Not word character  |
135 | `\D`                | Not digit character |
136
137 Perl compatible regular expressions are described here.
138
139 ### Replacing
140
141 ```fish
142 # Replaces the first match
143 string replace --regex 'Fish' 'fish' 'Hello from Fish!'
144
145 # Replaces all matches
146 string replace --regex --all 'Fish' 'fish' 'Hello from Fish!'
147 ```
148
149 ## Conditionals
150
151 ### If/else
152
153 ```fish
154 if test $my_variable -lt $another_variable
155   ···
156 else if test $my_variable -eq $another_variable
157   ···
158 else
159   ···
160 end
161 ```
162
163 ### Comparisons
164
165 #### Numbers
166
167 | Number operator     | Meaning                                   |
168 | ---                 | ---                                       |
169 | `-lt`               | [L]ess [t]han                             |
170 | `-eq`               | [Eq]ual                                   |
171 | `-gt`               | [G]reater [t]han                          |
172 | `-le`               | [L]ess than or [e]qual to                 |
173 | `-ge`               | [G]reater than or [e]qual to              |
174 | `-ne`               | [N]ot [E]qual                             |
175
176 #### Strings
177
178 | String operator     | Meaning                                   |
179 | ---                 | ---                                       |
180 | `==`                | [Eq]ual                                   |
181 | `!=`                | [N]ot [E]qual                             |
182
183 #### Files
184
185 | File operator       | Meaning                                   |
186 | ---                 | ---                                       |
187 | `-f`                | [F]ile exists                             |
188 | `-d`                | [D]irectory exists                        |
189 | `-r`                | File or directory exists and [r]eadable   |
190 | `-w`                | File or directory exists and [w]ritable   |
191 | `-x`                | File or directory exists and e[x]ecutable |
192
193
194 ## Process communication
195
196 ### Writing to files
197
198 ```fish
199 # Overwrite file
200 echo 'Hello from Fish!' > my_file
201
202 # Append to file
203 echo 'Hello from Fish!' >> my_file
204 ```
205
206 ### Piping
207
208 ```fish
209 my_command | another_command
210 ```
211
212 Passes the first command stdout output as an input to a second command.
213
214 ### Command substitution
215
216 ```fish
217 echo (math $my_variable + 1)
218 ```
219
220 The `(...)` expression is substituted with the output of the command inside it.
221
222
223 ### Process substitution
224
225 ```fish
226 echo (math $my_variable + 1 | psub)
227 ```
228
229 The `(... | psub)` expression is substituted with a temporary file with the command's output.
230
231 ## Functions
232
233 ### Defining and erasing
234
235 ```fish
236 # Declare the function
237 function my_function --description 'My description'
238   ···
239 end
240
241 # Remove the function
242 functions --erase my_function
243 ```
244
245 ## Events
246
247 ### Emitting
248
249 ```fish
250 emit my_event
251 ```
252
253 Emits an event that can be picked up by other functions.
254
255 ### Event handling
256
257 ```fish
258 function my_hook --on-event my_event
259   ···
260 end
261 ```
262
263 Reacts to the `my_event` event.
264
265 ## Abbreviations
266
267 ### Defining and erasing
268
269 ```fish
270 # Declare the abbreviation
271 abbr --add grh "git reset --hard HEAD"
272 ```
273
274
275 ```fish
276 # Remove the abbreviation
277 abbr --erase grh
278 ```
279
280 ## Completions
281
282 ### Defining completions
283
284 ```fish
285 complete --command mycommand --arguments 'install uninstall'
286 complete --command mycommand --short-option 'h' --long-option 'help' --description 'Display help'
287 ```
288
289 | Option              | Description                                          |
290 | ---                 | ---                                                  |
291 | `--arguments`       | Arguments to the command itself or option            |
292 | `--short-option`    | Short option                                         |
293 | `--long-option`     | Long option                                          |
294 | `--no-files`        | Don't suggest files                                  |
295 | `--force-files`     | Suggest files                                        |
296 | `--condition`       | Display the hint only when a given condition is true |
297 | `--description`     | Description                                          |
298
299 Declares the completion for a command.
300
301 ### Removing completions
302
303 ```fish
304 complete --command mycommand --erase
305 ```
306
307 ## Useful built-in functions
308
309 | Function                              | Description                                                   |
310 | ---                                   | ---                                                           |
311 | `__fish_seen_argument`                | Check whether the specified argument is used                  |
312 | `__fish_seen_subcommand_from`         | Check whether the specified subcommand is used                |
313 | `__fish_use_subcommand`               | Check whether any subcommand is used                          |
314 | ---                                   | ---                                                           |
315 | `__fish_complete_directories`         | Complete directories with the specified letters in their name |
316 | `__fish_complete_suffix`              | Complete files with the specified suffix                      |
317 | ---                                   | ---                                                           |
318 | `__fish_complete_users`               | List all users                                                |
319 | `__fish_complete_groups`              | List all user groups                                          |
320 | `__fish_print_hostnames`              | List all host names                                           |
321 | `__fish_complete_pids`                | List all PIDs                                                 |
322 | `__fish_print_filesystems`            | List all known filesystems                                    |
323 | `__fish_print_interfaces`             | List all network interfaces                                   |