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 | `Alt ←` _/_ `Alt →` | Move word                   |
15 | `^U`                | Delete to beginning         |
16 | `^W`                | Delete to previous `/`      |
17 | `^D`                | Delete next character       |
18 | `Alt D`             | Delete next word            |
19 | `^C`                | Cancel line                 |
20 | `Alt P`             | Page output                 |
21 | ---                 | ---                         |
22 | `Alt ↑` _/_ `Alt ↓` | Previous _/_ next arguments |
23 | `Alt E` _/_ `Alt V` | Open in external editor     |
24 | `^L`                | Repaint screen              |
25 {: .-shortcuts}
26
27 ### Help
28
29 | `Alt H` | Help on word (man)                     |
30 | `Alt W` | Help on word (short descriptions)      |
31 | `Alt L` | List directory on cursor               |
32 {: .-shortcuts}
33
34 ## Function
35
36 ### Writing functions
37
38 ```fish
39 function my_function --description "My description"
40   ···
41 end
42 ```
43
44 ### Conditional
45
46 ```fish
47 if test -f foo.txt
48   ···
49 else if test -f bar.txt
50   ···
51 else
52   ···
53 end
54 ```
55
56 ### Combining tests
57
58 ```fish
59 if test -f foo.txt && test -f bar.txt
60 ```
61
62 ```fish
63 if test -f foo.txt -a -f bar.txt
64 ```
65
66 ```fish
67 if test \( -f foo.txt \) -a -f \( bar.txt \)
68 ```
69
70 ### Events
71
72 #### Emitting
73
74 ```fish
75 emit my_event
76 ```
77
78 #### Listening
79
80 ```fish
81 function myhook --on-event my_event
82   ···
83 end
84 ```
85
86 This lets you hook onto events, such as `fish_prompt`.
87
88 ## Completions
89
90 ### Creating completions
91
92 #### ~/.fish/completions/mycommand.fish
93
94 ```fish
95 complete -c mycommand ...
96 complete -c mycommand ...
97 complete -c mycommand ...
98 ```
99
100 ### Options
101
102 ```fish
103 complete \
104   -c                         # command
105   -s                         # short option
106   -l                         # long option
107   -r, --require-parameter
108   -f, --no-files
109   -x                         # exclusive (-r -f)
110   -n '__fish_use_subcommand' # condition
111   --description ".."
112 ```
113
114 #### Example
115
116 ```fish
117   complete -c $cmd \
118 -n '__fish_use_subcommand' \
119 -x -a hello \
120 --description 'lol'
121 ```
122
123 ### Conditions
124
125 | Condition | Description
126 | --- | ---
127 | `-n __fish_complete_directories STRING DESCRIPTION` | performs path completion on STRING, allowing only directories, and giving them the description DESCRIPTION.
128 | `-n __fish_complete_path STRING DESCRIPTION` | performs path completion on STRING, giving them the description DESCRIPTION.
129 | `-n __fish_complete_groups` | prints a list of all user groups with the groups members as description.
130 | `-n __fish_complete_pids` | prints a list of all processes IDs with the command name as description.
131 | `-n __fish_complete_suffix SUFFIX` | performs file completion allowing only files ending in SUFFIX. The mimetype database is used to find a suitable description.
132 | `-n __fish_complete_users` | prints a list of all users with their full name as description.
133 | `-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.
134 | `-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.
135 | `-n __fish_print_interfaces` | prints a list of all known network interfaces.
136 | `-n __fish_print_packages` | prints a list of all installed packages. This function currently handles Debian, rpm and Gentoo packages.
137 | `-n __fish_use_subcommand` |
138 | `-n __fish_seen_subcommand_from init` |
139
140 #### Example
141
142 ```fish
143 complete -c ruby -s X -x -a '(__fish_complete_directories (commandline -ct))' --description 'Directory'
144 ```
145
146 ### Examples
147
148 Start each example with `complete -c cmdname`
149
150 ```fish
151 -x
152   # no filename completion
153 ```
154
155 ```fish
156 -s d -x -a "read skip"
157   # -d {read|skip}
158 ```
159
160 ```fish
161 -s d -x
162   # -d <something>
163 ```
164
165 ```fish
166 -s f -r
167   # -f FILE
168 ```
169
170 ```fish
171 -s f -l force
172   # -f, --force
173 ```
174
175 ```fish
176 -a "(cat /etc/passwd | cut -d : -f 1)"
177   # first argument as filename
178 ```