File: tutorial.rst

package info (click to toggle)
fish 4.2.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,980 kB
  • sloc: python: 6,972; javascript: 1,407; sh: 1,009; xml: 411; ansic: 230; objc: 78; makefile: 20
file content (756 lines) | stat: -rw-r--r-- 27,326 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
.. highlight:: fish-docs-samples

Tutorial
========

Why fish?
---------

Fish is a fully-equipped command line shell (like bash or zsh) that is smart and user-friendly. Fish supports powerful features like syntax highlighting, autosuggestions, and tab completions that just work, with nothing to learn or configure.

If you want to make your command line more productive, more useful, and more fun, without learning a bunch of arcane syntax and configuration options, then fish might be just what you're looking for!


Getting started
---------------

Once installed, just type in ``fish`` into your current shell to try it out!

You will be greeted by the standard fish prompt,
which means you are all set up and can start using fish::

    > fish
    Welcome to fish, the friendly interactive shell
    Type help for instructions on how to use fish
    you@hostname ~>


This prompt that you see above is the fish default prompt: it shows your username, hostname, and working directory. You can customize it, see :ref:`how to change your prompt <prompt>`.

From now on, we'll pretend your prompt is just a ``>`` to save space.


Learning fish
-------------

This tutorial assumes a basic understanding of command line shells and Unix commands, and that you have a working copy of fish.

If you have a strong understanding of other shells, and want to know what fish does differently, search for the magic phrase *unlike other shells*, which is used to call out important differences.

Or, if you want a quick overview over the differences to other shells like Bash, see :doc:`Fish For Bash Users <fish_for_bash_users>`.

For the full, detailed description of how to use fish interactively, see :doc:`Interactive Use <interactive>`.

For a comprehensive description of fish's scripting language, see :doc:`The Fish Language <language>`.

Running Commands
----------------

Fish runs commands like other shells: you type a command, followed by its arguments. Spaces are separators::

    > echo hello world
    hello world


This runs the command ``echo`` with the arguments ``hello`` and ``world``. In this case that's the same as one argument ``hello world``, but in many cases it's not. If you need to pass an argument that includes a space, you can :ref:`escape <escapes>` with a backslash, or :ref:`quote <quotes>` it using single or double quotes::

    > mkdir My\ Files
    # Makes a directory called "My Files", with a space in the name
    > cp ~/Some\ File 'My Files'
    # Copies a file called "Some File" in the home directory to "My Files"
    > ls "My Files"
    Some File


Getting Help
------------

Run ``help`` to open fish's help in a web browser, and ``man`` with the page (like ``fish-language``) to open it in a man page. You can also ask for help with a specific command, for example, ``help set`` to open in a web browser, or ``man set`` to see it in the terminal.

::

    > man set
    set - handle shell variables
      Synopsis...

To open this section, use ``help getting-help``.

This only works for fish's own documentation for itself and its built-in commands (the "builtins"). For any other commands on your system, they should provide their own documentation, often in the man system. For example ``man ls`` should tell you about your computer's ``ls`` command.

Syntax Highlighting
-------------------

.. role:: red
.. role:: gray
.. role:: prompt
.. role:: command
.. role:: param
.. role:: param-valid-path

You'll quickly notice that fish performs syntax highlighting as you type. Invalid commands are colored red by default:

.. parsed-literal::
    :class: highlight

    :prompt:`>` :red:`/bin/mkd`

A command may be invalid because it does not exist, or refers to a file that you cannot execute. When the command becomes valid, it is shown in a different color::

    > /bin/mkdir


Valid file paths are underlined as you type them:

.. parsed-literal::
    :class: highlight

    :prompt:`>` :command:`cat` :param-valid-path:`~/somefi`


This tells you that there exists a file that starts with ``somefi``, which is useful feedback as you type.

These colors, and many more, can be changed by running ``fish_config``, or by modifying :ref:`color variables <variables-color>` directly.

For example, if you want to disable (almost) all coloring::

  fish_config theme choose None

This picks the "none" theme. To see all themes::

  fish_config theme show

Just running ``fish_config`` will open up a browser interface that allows you to pick from the available themes.

Autosuggestions
---------------

As you type fish will suggest commands to the right of the cursor, in gray. For example:

.. parsed-literal::
    :class: highlight

    :prompt:`>` :red:`/bin/h`:gray:`ostname`


It knows about paths and options:

.. parsed-literal::
    :class: highlight

    :prompt:`>` :command:`grep` :param:`--i`:gray:`gnore-case`


And history too. Type a command once, and you can re-summon it by just typing a few letters:

.. parsed-literal::
    :class: highlight

    :prompt:`>` :red:`r`:gray:`sync -avze ssh . myname@somelonghost.com:/some/long/path/doo/dee/doo/dee/doo`


To accept the autosuggestion, hit :kbd:`right` (``→``) or :kbd:`ctrl-f`. To accept a single word of the autosuggestion, :kbd:`alt-right` (``→``). If the autosuggestion is not what you want, just ignore it.

If you don't like autosuggestions, you can disable them by setting ``$fish_autosuggestion_enabled`` to 0::

  set -g fish_autosuggestion_enabled 0

Tab Completions
---------------

A rich set of tab completions work "out of the box".

Press :kbd:`tab` and fish will attempt to complete the command, argument, or path:

.. parsed-literal::
    :class: highlight

    :prompt:`>` :red:`/pri`:kbd:`tab` => :command:`/private/`


If there's more than one possibility, it will list them:

.. parsed-literal::
    :class: highlight

    :prompt:`>` :red:`~/stuff/s`:kbd:`tab`
    ~/stuff/script.sh  :gray:`(command)`  ~/stuff/sources/  :gray:`(directory)`


Hit tab again to cycle through the possibilities. The part in parentheses there (that "command" and "directory") is the completion description. It's just a short hint to explain what kind of argument it is.

fish can also complete many commands, like git branches:

.. parsed-literal::
    :class: highlight

    :prompt:`>` :command:`git` :param:`merge pr`:kbd:`tab` => :command:`git` :param:`merge prompt_designer`
    :prompt:`>` :command:`git` :param:`checkout b`:kbd:`tab`
    builtin_list_io_merge :gray:`(Branch)`  builtin_set_color :gray:`(Branch)` busted_events :gray:`(Tag)`

Try hitting tab and see what fish can do!

Variables
---------

Like other shells, a dollar sign followed by a variable name is replaced with the value of that variable::

    > echo My home directory is $HOME
    My home directory is /home/tutorial

This is known as variable substitution, and it also happens in double quotes, but not single quotes::

    > echo "My current directory is $PWD"
    My current directory is /home/tutorial
    > echo 'My current directory is $PWD'
    My current directory is $PWD

Unlike other shells, fish has an ordinary command to set variables: ``set``, which takes a variable name, and then its value.

::

    > set name 'Mister Noodle'
    > echo $name
    Mister Noodle


(Notice the quotes: without them, ``Mister`` and ``Noodle`` would have been separate arguments, and ``$name`` would have been made into a list of two elements.)

Unlike other shells, variables are not further split after substitution::

    > mkdir $name
    > ls
    Mister Noodle

In bash, this would have created two directories "Mister" and "Noodle". In fish, it created only one: the variable had the value "Mister Noodle", so that is the argument that was passed to ``mkdir``, spaces and all.

You can erase (or "delete") a variable with ``-e`` or ``--erase``

::

    > set -e MyVariable
    > env | grep MyVariable
    (no output)

For more, see :ref:`Variable expansion <expand-variable>`.

.. _tut-exports:

Exports (Shell Variables)
-------------------------

Sometimes you need to have a variable available to an external command, often as a setting. For example many programs like ``git`` or ``man`` read the ``$PAGER`` variable to figure out your preferred pager (the program that lets you scroll text). Other variables used like this include ``$BROWSER``, ``$LANG`` (to configure your language) and ``$PATH``. You'll note these are written in ALLCAPS, but that's just a convention.

To give a variable to an external command, it needs to be "exported". This is done with a flag to ``set``, either ``--export`` or just ``-x``.

::

    > set -x MyVariable SomeValue
    > env | grep MyVariable
    MyVariable=SomeValue

It can also be unexported with ``--unexport`` or ``-u``.

This works the other way around as well! If fish is started by something else, it inherits that parents exported variables. So if your terminal emulator starts fish, and it exports ``$LANG`` set to ``en_US.UTF-8``, fish will receive that setting. And whatever started your terminal emulator also gave *it* some variables that it will then pass on unless it specifically decides not to. This is how fish usually receives the values for things like ``$LANG``, ``$PATH`` and ``$TERM``, without you having to specify them again.

Exported variables can be local or global or universal - "exported" is not a :ref:`scope <variables-scope>`! Usually you'd make them global via ``set -gx MyVariable SomeValue``.

For more, see :ref:`Exporting variables <variables-export>`.

.. _tut-lists:

Lists
-----

The ``set`` command above used quotes to ensure that ``Mister Noodle`` was one argument. If it had been two arguments, then ``name`` would have been a list of length 2.  In fact, all variables in fish are really lists, that can contain any number of values, or none at all.

Some variables, like ``$PWD``, only have one value. By convention, we talk about that variable's value, but we really mean its first (and only) value.

Other variables, like ``$PATH``, really do have multiple values. During variable expansion, the variable expands to become multiple arguments::

    > echo $PATH
    /usr/bin /bin /usr/sbin /sbin /usr/local/bin

Variables whose name ends in "PATH" are automatically split on colons to become lists. They are joined using colons when exported to subcommands. This is for compatibility with other tools, which expect $PATH to use colons. You can also explicitly add this quirk to a variable with ``set --path``, or remove it with ``set --unpath``.

Lists cannot contain other lists: there is no recursion.  A variable is a list of strings, full stop.

Get the length of a list with ``count``::

    > count $PATH
    5

You can append (or prepend) to a list by setting the list to itself, with some additional arguments. Here we append /usr/local/bin to $PATH::

    > set PATH $PATH /usr/local/bin

You can access individual elements with square brackets. Indexing starts at 1 from the beginning, and -1 from the end::

    > echo $PATH
    /usr/bin /bin /usr/sbin /sbin /usr/local/bin
    > echo $PATH[1]
    /usr/bin
    > echo $PATH[-1]
    /usr/local/bin

You can also access ranges of elements, known as "slices":

::

    > echo $PATH[1..2]
    /usr/bin /bin
    > echo $PATH[-1..2]
    /usr/local/bin /sbin /usr/sbin /bin

You can iterate over a list (or a slice) with a for loop::

    for val in $PATH
      echo "entry: $val"
    end
    # Will print:
    # entry: /usr/bin/
    # entry: /bin
    # entry: /usr/sbin
    # entry: /sbin
    # entry: /usr/local/bin

One particular bit is that you can use lists like :ref:`Brace expansion <expand-brace>`. If you attach another string to a list, it'll combine every element of the list with the string::

    > set mydirs /usr/bin /bin
    > echo $mydirs/fish # this is just like {/usr/bin,/bin}/fish
    /usr/bin/fish /bin/fish

This also means that, if the list is empty, there will be no argument::

    > set empty # no argument
    > echo $empty/this_is_gone # prints an empty line
    
If you quote the list, it will be used as one string and so you'll get one argument even if it is empty.

For more, see :ref:`Lists <variables-lists>`.
For more on combining lists with strings (or even other lists), see :ref:`cartesian products <cartesian-product>` and :ref:`Variable expansion <expand-variable>`.

Wildcards
---------

Fish supports the familiar wildcard ``*``. To list all JPEG files::

    > ls *.jpg
    lena.jpg
    meena.jpg
    santa maria.jpg


You can include multiple wildcards::

    > ls l*.p*
    lena.png
    lesson.pdf


The recursive wildcard ``**`` searches directories recursively::

    > ls /var/**.log
    /var/log/system.log
    /var/run/sntp.log


If that directory traversal is taking a long time, you can :kbd:`ctrl-c` out of it.

For more, see :ref:`Wildcards <expand-wildcard>`.

Pipes and Redirections
----------------------

You can pipe between commands with the usual vertical bar::

    > echo hello world | wc
          1       2      12

stdin and stdout can be redirected via the familiar ``<`` and ``>``. stderr is redirected with a ``2>``.

::

    > grep fish < /etc/shells > ~/output.txt 2> ~/errors.txt

To redirect stdout and stderr into one file, you can use ``&>``::

    > make &> make_output.txt

For more, see :ref:`Input and output redirections <redirects>` and :ref:`Pipes <pipes>`.


Command Substitutions
---------------------

Command substitutions use the output of one command as an argument to another. Unlike other shells, fish does not use backticks `` for command substitutions. Instead, it uses parentheses with or without a dollar::

    > echo In (pwd), running $(uname)
    In /home/tutorial, running FreeBSD

A common idiom is to capture the output of a command in a variable::

    > set os (uname)
    > echo $os
    Linux

Command substitutions without a dollar are not expanded within quotes, so the version with a dollar is simpler::

    > touch "testing_$(date +%s).txt"
    > ls *.txt
    testing_1360099791.txt

Unlike other shells, fish does not split command substitutions on any whitespace (like spaces or tabs), only newlines. Usually this is a big help because unix commands operate on a line-by-line basis. Sometimes it can be an issue with commands like ``pkg-config`` that print what is meant to be multiple arguments on a single line. To split it on spaces too, use ``string split``.

::

    > printf '%s\n' (pkg-config --libs gio-2.0)
    -lgio-2.0 -lgobject-2.0 -lglib-2.0
    > printf '%s\n' (pkg-config --libs gio-2.0 | string split -n " ")
    -lgio-2.0
    -lgobject-2.0
    -lglib-2.0

If you need a command substitutions output as one argument, without any splits, use quoted command substitution::

    > echo "first line
    second line" > myfile
    > set myfile "$(cat myfile)"
    > printf '|%s|' $myfile
    |first line
    second line|

For more, see :ref:`Command substitution <expand-command-substitution>`.

.. _tut-semicolon:

Separating Commands (Semicolon)
-------------------------------

Like other shells, fish allows multiple commands either on separate lines or the same line.

To write them on the same line, use the semicolon (";"). That means the following two examples are equivalent::

    echo fish; echo chips
    
    # or
    echo fish
    echo chips

This is useful interactively to enter multiple commands. In a script it's easier to read if the commands are on separate lines.

Exit Status
-----------

When a command exits, it returns a status code as a non-negative integer (that's a whole number >= 0).

Unlike other shells, fish stores the exit status of the last command in ``$status`` instead of ``$?``.

::

    > false
    > echo $status
    1

This indicates how the command fared - 0 usually means success, while the others signify kinds of failure. For instance fish's ``set --query`` returns the number of variables it queried that weren't set - ``set --query PATH`` usually returns 0, ``set --query arglbargl boogagoogoo`` usually returns 2.

There is also a ``$pipestatus`` list variable for the exit statuses [#]_ of processes in a pipe.

For more, see :ref:`The status variable <variables-status>`.

.. [#] or "stati" if you prefer, or "statūs" if you've time-travelled from ancient Rome or work as a latin teacher

.. _tut-combiners:

Combiners (And, Or, Not)
------------------------

fish supports the familiar ``&&`` and ``||`` to combine commands, and ``!`` to negate them::

    > ./configure && make && sudo make install

Here, ``make`` is only executed if ``./configure`` succeeds (returns 0), and ``sudo make install`` is only executed if both ``./configure`` and ``make`` succeed.

fish also supports :doc:`and <cmds/and>`, :doc:`or <cmds/or>`, and :doc:`not <cmds/not>`. The first two are job modifiers and have lower precedence. Example usage::

    > cp file1 file1_bak && cp file2 file2_bak; and echo "Backup successful"; or echo "Backup failed"
    Backup failed


As mentioned in :ref:`the section on the semicolon <tut-semicolon>`, this can also be written in multiple lines, like so::

    cp file1 file1_bak && cp file2 file2_bak
    and echo "Backup successful"
    or echo "Backup failed"

Conditionals (If, Else, Switch)
-------------------------------

Use :doc:`if <cmds/if>` and :doc:`else <cmds/else>` to conditionally execute code, based on the exit status of a command.

::

    if grep fish /etc/shells
        echo Found fish
    else if grep bash /etc/shells
        echo Found bash
    else
        echo Got nothing
    end

To compare strings or numbers or check file properties (whether a file exists or is writeable and such), use :doc:`test <cmds/test>`, like

::

    if test "$fish" = "flounder"
        echo FLOUNDER
    end
    
    # or
    
    if test "$number" -gt 5
        echo $number is greater than five
    else
        echo $number is five or less
    end

    # or

    # This test is true if the path /etc/hosts exists
    # - it could be a file or directory or symlink (or possibly something else).
    if test -e /etc/hosts
        echo We most likely have a hosts file
    else
        echo We do not have a hosts file
    end

:ref:`Combiners <tut-combiners>` can also be used to make more complex conditions, like

::

    if command -sq fish; and grep fish /etc/shells
        echo fish is installed and configured
    end

For even more complex conditions, use :doc:`begin <cmds/begin>` and :doc:`end <cmds/end>` to group parts of them.

There is also a :doc:`switch <cmds/switch>` command::

    switch (uname)
    case Linux
        echo Hi Tux!
    case Darwin
        echo Hi Hexley!
    case FreeBSD NetBSD DragonFly
        echo Hi Beastie!
    case '*'
        echo Hi, stranger!
    end

As you see, :doc:`case <cmds/case>` does not fall through, and can accept multiple arguments or (quoted) wildcards.

For more, see :ref:`Conditions <syntax-conditional>`.

Functions
---------

A fish function is a list of commands, which may optionally take arguments. Unlike other shells, arguments are not passed in "numbered variables" like ``$1``, but instead in a single list ``$argv``. To create a function, use the :doc:`function <cmds/function>` builtin::

    function say_hello
        echo Hello $argv
    end
    say_hello
    # prints: Hello
    say_hello everybody!
    # prints: Hello everybody!

Unlike other shells, fish does not have aliases or special prompt syntax. Functions take their place. [#]_

You can list the names of all functions with the :doc:`functions <cmds/functions>` builtin (note the plural!). fish starts out with a number of functions::

    > functions
    N_, abbr, alias, bg, cd, cdh, contains_seq, dirh, dirs, disown, down-or-search, edit_command_buffer, export, fg, fish_add_path, fish_breakpoint_prompt, fish_clipboard_copy, fish_clipboard_paste, fish_config, fish_default_key_bindings, fish_default_mode_prompt, fish_git_prompt, fish_hg_prompt, fish_hybrid_key_bindings, fish_indent, fish_is_root_user, fish_job_summary, fish_key_reader, fish_md5, fish_mode_prompt, fish_npm_helper, fish_opt, fish_print_git_action, fish_print_hg_root, fish_prompt, fish_sigtrap_handler, fish_svn_prompt, fish_title, fish_update_completions, fish_vcs_prompt, fish_vi_cursor, fish_vi_key_bindings, funced, funcsave, grep, help, history, hostname, isatty, kill, la, ll, ls, man, nextd, open, popd, prevd, prompt_hostname, prompt_pwd, psub, pushd, realpath, seq, setenv, suspend, trap, type, umask, up-or-search, vared, wait

You can see the source for any function by passing its name to ``functions``::

    > functions ls
    function ls --description 'List contents of directory'
        command ls -G $argv
    end

For more, see :ref:`Functions <syntax-function>`.

.. [#] There is a function called :doc:`alias <cmds/alias>`, but it's just a shortcut to make functions. fish also provides :ref:`abbreviations <abbreviations>`, through the :doc:`abbr <cmds/abbr>` command.

Loops
-----

While loops::

    while true
        echo "Loop forever"
    end
    # Prints:
    # Loop forever
    # Loop forever
    # Loop forever
    # yes, this really will loop forever. Unless you abort it with ctrl-c.

For loops can be used to iterate over a list. For example, a list of files::

    for file in *.txt
        cp $file $file.bak
    end

Iterating over a list of numbers can be done with ``seq``::

    for x in (seq 5)
        touch file_$x.txt
    end

For more, see :ref:`Loops and blocks <syntax-loops-and-blocks>`.

Prompt
------

.. role:: purple

Unlike other shells, there is no prompt variable like ``PS1``. To display your prompt, fish executes the :doc:`fish_prompt <cmds/fish_prompt>` function and uses its output as the prompt. And if it exists, fish also executes the :doc:`fish_right_prompt <cmds/fish_right_prompt>` function and uses its output as the right prompt.

You can define your own prompt from the command line:

.. parsed-literal::
    :class: highlight

    > function fish_prompt; echo "New Prompt % "; end
    New Prompt % _


Then, if you are happy with it, you can save it to disk by typing ``funcsave fish_prompt``. This saves the prompt in ``~/.config/fish/functions/fish_prompt.fish``. (Or, if you want, you can create that file manually from the start.)

Multiple lines are OK. Colors can be set via :doc:`set_color <cmds/set_color>` by passing it named ANSI colors, or hex RGB values::

    function fish_prompt
        set_color purple
        date "+%m/%d/%y"
        set_color FF0000
        echo (pwd) '>' (set_color normal)
    end


This prompt would look like:

.. parsed-literal::
    :class: highlight

    :purple:`02/06/13`
    :red:`/home/tutorial >` _


See also :doc:`Writing your own prompt <prompt>`.

You can choose among sample prompts by running ``fish_config`` for a web UI or ``fish_config prompt`` for a simpler version inside your terminal.

$PATH
-----

``$PATH`` is an environment variable containing the directories that fish searches for commands. Unlike other shells, $PATH is a :ref:`list <tut-lists>`, not a colon-delimited string.

Fish takes care to set ``$PATH`` to a default, but typically it is just inherited from fish's parent process and is set to a value that makes sense for the system - see :ref:`Exports <tut-exports>`.

To prepend /usr/local/bin and /usr/sbin to ``$PATH``, you can write::

    > set PATH /usr/local/bin /usr/sbin $PATH


To remove /usr/local/bin from ``$PATH``, you can write::

    > set PATH (string match -v /usr/local/bin $PATH)

For compatibility with other shells and external commands, $PATH is a :ref:`path variable<variables-path>`, and so will be joined with colons (not spaces) when you quote it::

    > echo "$PATH"
    /usr/local/sbin:/usr/local/bin:/usr/bin

and it will be exported like that, and when fish starts it splits the $PATH it receives into a list on colon.

You can do so directly in ``config.fish``, like you might do in other shells with ``.profile``. See :ref:`this example <path_example>`.

A faster way is to use the :doc:`fish_add_path <cmds/fish_add_path>` function, which adds given directories to the path if they aren't already included. It does this by modifying the ``$fish_user_paths`` :ref:`universal variable <tut-universal>`, which is automatically prepended to ``$PATH``. For example, to permanently add ``/usr/local/bin`` to your ``$PATH``, you could write::

    > fish_add_path /usr/local/bin


The advantage is that you don't have to go mucking around in files: just run this once at the command line, and it will affect the current session and all future instances too. You can also add this line to :ref:`config.fish <tut-config>`, as it only adds the component if necessary.

Or you can modify $fish_user_paths yourself, but you should be careful *not* to append to it unconditionally in config.fish, or it will grow longer and longer.

.. _tut-config:

Startup (Where's .bashrc?)
--------------------------

Fish starts by executing commands in ``~/.config/fish/config.fish``. You can create it if it does not exist.

It is possible to directly create functions and variables in ``config.fish`` file, using the commands shown above. For example:

.. _path_example:

::

    > cat ~/.config/fish/config.fish
    
    set -x PATH $PATH /sbin/
    
    function ll
        ls -lh $argv
    end


However, it is more common and efficient to use  autoloading functions and universal variables.

If you want to organize your configuration, fish also reads commands in .fish files in ``~/.config/fish/conf.d/``. See :ref:`Configuration Files <configuration>` for the details.

Autoloading Functions
---------------------

When fish encounters a command, it attempts to autoload a function for that command, by looking for a file with the name of that command in ``~/.config/fish/functions/``.

For example, if you wanted to have a function ``ll``, you would add a text file ``ll.fish`` to ``~/.config/fish/functions``::

    > cat ~/.config/fish/functions/ll.fish
    function ll
        ls -lh $argv
    end


This is the preferred way to define your prompt as well::

    > cat ~/.config/fish/functions/fish_prompt.fish
    function fish_prompt
        echo (pwd) "> "
    end


See the documentation for :doc:`funced <cmds/funced>` and :doc:`funcsave <cmds/funcsave>` for ways to create these files automatically, and :ref:`$fish_function_path <syntax-function-autoloading>` to control their location.

.. _tut-universal:

Universal Variables
-------------------

A universal variable is a variable whose value is shared across all instances of fish, now and in the future – even after a reboot. You can make a variable universal with ``set -U``::

    > set -U EDITOR vim


Now in another shell::

    > echo $EDITOR
    vim

You only need to set universal variables once interactively. There is no need to add them to your :ref:`config files <configuration>`. For more details, see :ref:`Universal Variables <variables-universal>`.

Ready for more?
---------------

If you want to learn more about fish, there is :ref:`lots of detailed documentation <intro>`, the `official gitter channel <https://gitter.im/fish-shell/fish-shell>`__, an `official mailing list <https://lists.sourceforge.net/lists/listinfo/fish-users>`__, and the `github page <https://github.com/fish-shell/fish-shell/>`__.