Mac os fish as default shell

Introduction¶

This is the documentation for fish, the friendly interactive shell.

A shell is a program that helps you operate your computer by starting other programs. fish offers a command-line interface focused on usability and interactive use.

Unlike other shells, fish does not follow the POSIX standard, but still uses roughly the same model.

Some of the special features of fish are:

Extensive UI: Syntax highlighting , Autosuggestions , tab completion and selection lists that can be navigated and filtered.

No configuration needed: fish is designed to be ready to use immediately, without requiring extensive configuration.

Easy scripting: New functions can be added on the fly. The syntax is easy to learn and use.

This page explains how to install and set up fish and where to get more information.

Further Reading¶

If this is your first time using fish, see the tutorial .

If you are already familiar with other shells like bash and want to see the scripting differences, see Fish For Bash Users .

For a comprehensive overview of fish’s scripting language, see The Fish Language .

For information on using fish interactively, see Interactive use .

Installation and Start¶

This section describes how to install, uninstall, start, and exit the fish shell. It also explains how to make fish the default shell.

Installation¶

Up-to-date instructions for installing the latest version of fish are on the fish homepage.

To install the development version of fish, see the instructions on the project’s GitHub page.

Starting and Exiting¶

Once fish has been installed, open a terminal. If fish is not the default shell:

Type fish to start a fish shell:

Type exit to exit a fish shell:

Executing Bash¶

If fish is your default shell and you want to copy commands from the internet that are written in bash (the default shell on most systems), you can proceed in one of the following two ways:

Use the bash command with the -c switch to read from a string:

Use bash without a switch to open a bash shell you can use and exit afterward:

Default Shell¶

To make fish your default shell:

Add the line /usr/local/bin/fish to /etc/shells .

Change your default shell with chsh -s /usr/local/bin/fish .

For detailed instructions see Switching to fish .

Uninstalling¶

Shebang Line¶

Because shell scripts are written in many different languages, they need to carry information about which interpreter should be used to execute them. For this, they are expected to have a first line, the shebang line, which names the interpreter executable.

A script written in bash would need a first line like this:

When the shell tells the kernel to execute the file, it will use the interpreter /bin/bash .

For a script written in another language, just replace /bin/bash with the interpreter for that language (for example: /usr/bin/python for a python script, or /usr/local/bin/fish for a fish script).

This line is only needed when scripts are executed without specifying the interpreter. For functions inside fish or when executing a script with fish /path/to/script , a shebang is not required (but it doesn’t hurt!).

Configuration files¶

When fish is started, it reads and runs its configuration files. Where these are depends on build configuration and environment variables.

The main file is

/.config/fish/config.fish (or more precisely $XDG_CONFIG_HOME/fish/config.fish ).

Configuration files are evaluated in the following order:

Configuration snippets in files ending in .fish , in the directories:

$__fish_config_dir/conf.d (by default,

$__fish_sysconf_dir/conf.d (by default, /etc/fish/conf.d/ )

Directories for third-party software vendors to ship their own configuration snippets for their software. Fish searches the directories in the XDG_DATA_DIRS environment variable for a fish/vendor_conf.d directory; if that variable is not defined, the default is /usr/share/fish/vendor_conf.d and /usr/local/share/fish/vendor_conf.d , unless your distribution customized this.

If there are multiple files with the same name in these directories, only the first will be executed. They are executed in order of their filename, sorted (like globs) in a natural order (i.e. «01» sorts before «2»).

System-wide configuration files, where administrators can include initialization that should be run for all users on the system — similar to /etc/profile for POSIX-style shells — in $__fish_sysconf_dir (usually /etc/fish/config.fish ).

User initialization, usually in

/.config/fish/config.fish (controlled by the XDG_CONFIG_HOME environment variable, and accessible as $__fish_config_dir ).

/.config/fish/config.fish is sourced after the snippets. This is so users can copy snippets and override some of their behavior.

These files are all executed on the startup of every shell. If you want to run a command only on starting an interactive shell, use the exit status of the command status —is-interactive to determine if the shell is interactive. If you want to run a command only when using a login shell, use status —is-login instead. This will speed up the starting of non-interactive or non-login shells.

If you are developing another program, you may wish to install configuration which is run for all users of the fish shell on a system. This is discouraged; if not carefully written, they may have side-effects or slow the startup of the shell. Additionally, users of other shells will not benefit from the Fish-specific configuration. However, if they are absolutely required, you may install them to the «vendor» configuration directory. As this path may vary from system to system, the pkgconfig framework should be used to discover this path with the output of pkg-config —variable confdir fish .

If you want to add the directory

/linux/bin to your PATH variable when using a login shell, add this to your

(alternatively use fish_add_path like fish_add_path

/linux/bin , which only adds the path if it isn’t included yet)

If you want to run a set of commands when fish exits, use an event handler that is triggered by the exit of the shell:

Further help and development¶

If you have a question not answered by this documentation, there are several avenues for help:

If you have an improvement for fish, you can submit it via the GitHub page.

Читайте также:  Звук для заставки windows

Источник

Frequently asked questions

Why does cd, $PWD and and various fish commands always resolve symlinked directories to their canonical path?

/images is a symlink to

/Documents/Images, if I write ‘cd images’, my prompt will say

Because it is impossible to consistently keep symlinked directories unresolved. It is indeed possible to do this partially, and many other shells do so. But it was felt there are enough serious corner cases that this is a bad idea. Most such issues have to do with how ‘..’ is handled, and are varitations of the following example:

Writing cd images; ls .. given the above directory structure would list the contents of

/Documents, not of

, even though using cd .. changes the current directory to

, and the prompt, the pwd builtin and many other directory information sources suggest that the current directory is

/images and its parent is

. This issue is not possible to fix without either making every single command into a builtin, breaking Unix semantics or implementing kludges in every single command.

This issue can also be seen when doing IO redirection.

Another related issue is that many programs that operate on recursive directory trees, like the find command, silently ignore symlinked directories. For example, find $PWD -name ‘*.txt’ silently fails in shells that don’t resolve symlinked paths.

I accidentally entered a directory path and fish changed directory. What happened?

‘, fish will test if a directory of that name exists. If it does, it is implicitly assumed that you want to change working directory. For example, the fastest way to switch to your home directory is to simply press

The open command doesn’t work.

How do I make fish my default shell?

echo /usr/local/bin/fish >>/etc/shells

If you installed a prepackaged version of fish, the package manager should have already done this for you.

In order to change your default shell, type:

chsh -s /usr/local/bin/fish

You may need to adjust the above path to e.g. /usr/bin/fish . Use the command which fish if you are unsure of where fish is installed.

Unfortunately, there is no way to make the changes take effect at once. You will need to log out and back in again.

I’m seeing weird output before each prompt when using screen. What’s wrong?

Run the following command in fish:

The long answer:

Fish is trying to set the titlebar message of your terminal. While screen itself supports this feature, your terminal does not. Unfortunately, when the underlying terminal doesn’t support setting the titlebar, screen simply passes through the escape codes and text to the underlying terminal instead of ignoring them. It is impossible detect and resolve this problem from inside fish since fish has no way of knowing what the underlying terminal type is. For now, the only way to fix this is to unset the titlebar message, as suggested above.

Note that fish has a default titlebar message, which will be used if the fish_title function is undefined. So simply unsetting the fish_title function will not work.

How do I change the greeting message?

Why doesn’t history substitution («!$» etc.) work?

Fish history recall is very simple yet effective:

  • As in any modern shell, the Up arrow recalls whole lines, starting from the last line executed. A single press replaces «!!», later presses replace «!-3» and the like.
    • If the line you want is far back in the history, type any part of the line and then press Up one or more times. This will constrain the recall to lines that include this text, and you will get to the line you want much faster. This replaces «!vi», «!?bar.c» and the like.
  • Alt+Up recalls individual arguments, starting from the last argument in the last line executed. A single press replaces «!$», later presses replace «. 4» and the like.
    • If the argument you want is far back in history (e.g. 2 lines back — that’s a lot of words!), type any part of it and then press Alt+Up. This will show only arguments containing that part and you will get what you want much faster. Try it out, this is very convenient!
    • If you want to reuse several arguments from the same line («. 3*» and the like), consider recalling the whole line and removing what you don’t need (Alt+D and Alt+Backspace are your friends).

See documentation for more details about line editing in fish.

Uninstalling fish

Next, do the following (assuming fish was installed to /usr/local):

Источник

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:

This prompt that you see above is the fish default prompt: it shows your username, hostname, and working directory. — to change this prompt see how to change your prompt — to switch to fish permanently see switch your default shell to fish .

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 Fish For Bash Users .

Running Commands¶

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

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 escape with a backslash, or quote it using single or double quotes:

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.

Syntax Highlighting¶

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

Читайте также:  Команды для ком строки windows

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:

Valid file paths are underlined as you type them:

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 color variables directly.

Wildcards¶

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

You can include multiple wildcards:

Especially powerful is the recursive wildcard ** which searches directories recursively:

If that directory traversal is taking a long time, you can Control + C out of it.

Pipes and Redirections¶

You can pipe between commands with the usual vertical bar:

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

To redirect stdout and stderr into one file, you need to first redirect stdout, and then stderr into stdout:

Autosuggestions¶

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

It knows about paths and options:

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

To accept the autosuggestion, hit → (right arrow) or Control + F . To accept a single word of the autosuggestion, Alt + → (right arrow). If the autosuggestion is not what you want, just ignore it.

Tab Completions¶

A rich set of tab completions work «out of the box».

Press Tab and fish will attempt to complete the command, argument, or path:

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

Hit tab again to cycle through the possibilities.

fish can also complete many commands, like git branches:

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:

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

Unlike other shells, fish has no dedicated VARIABLE=VALUE syntax for setting variables. Instead it has an ordinary command: set , which takes a variable name, and then its value.

(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:

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. Other shells use the term «arrays», rather than lists.

You can erase (or «delete») a variable with -e or —erase

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». Unlike other shells, fish does not have an export command. Instead, a variable is exported via an option to set , either —export or just -x .

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 scope ! Usually you’d make them global via set -gx MyVariable SomeValue .

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:

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 :

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:

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

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

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

Lists adjacent to other lists or strings are expanded as cartesian products unless quoted (see Variable expansion ):

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:

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

Command substitutions are not expanded within quotes. Instead, you can temporarily close the quotes, add the command substitution, and reopen them, all in the same argument:

Unlike other shells, fish does not split command substitutions on any whitespace (like spaces or tabs), only newlines. This 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 .

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

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:

Читайте также:  Самый красивый интерфейс linux

Exit Status¶

When a command exits, it returns a status code as a non-negative integer.

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

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 1 of processes in a pipe.

or «stati» if you prefer, or «statūs» if you’ve time-travelled from ancient Rome or work as a latin teacher

Combiners (And, Or, Not)¶

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

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 and , or , and not . The first two are job modifiers and have lower precedence. Example usage:

As mentioned in the section on the semicolon , this can also be written in multiple lines, like so:

Conditionals (If, Else, Switch)¶

Use if and else to conditionally execute code, based on the exit status of a command.

To compare strings or numbers or check file properties (whether a file exists or is writeable and such), use test , like

Combiners can also be used to make more complex conditions, like

For even more complex conditions, use begin and end to group parts of them.

There is also a switch command:

As you see, case does not fall through, and can accept multiple arguments or (quoted) wildcards.

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 function builtin:

Unlike other shells, fish does not have aliases or special prompt syntax. Functions take their place. 2

You can list the names of all functions with the functions builtin (note the plural!). fish starts out with a number of functions:

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

There is a function called alias , but it’s just a shortcut to make functions.

Loops¶

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

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

Prompt¶

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

You can define your own prompt from the command line:

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 set_color , passing it named ANSI colors, or hex RGB values:

This prompt would look like:

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

$PATH is an environment variable containing the directories that fish searches for commands. Unlike other shells, $PATH is a list , 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 Exports .

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

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

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

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 this example .

A faster way is to use the 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 universal variable , which is automatically prepended to $PATH . For example, to permanently add /usr/local/bin to your $PATH , you could write:

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 config.fish , 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.

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:

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 Configuration Files 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

For example, if you wanted to have a function ll , you would add a text file ll.fish to

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

See the documentation for funced and funcsave for ways to create these files automatically, and $fish_function_path to control their location.

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 :

Now in another shell:

Switching to fish?¶

If you wish to use fish (or any other shell) as your default shell, you need to enter your new shell’s executable in two places.

Add the shell to /etc/shells with:

Change your default shell with:

This assumes you installed fish to /usr/local/bin, which is the default location when you’ve compiled it yourself. If you installed it with a package manager, the usual location is /usr/bin/fish, but package managers typically already add it to /etc/shells. Just substitute the correct location.

(To change it back to another shell, just substitute /usr/local/bin/fish with /bin/bash , /bin/tcsh or /bin/zsh as appropriate in the steps above.)

Источник

Оцените статью