There are times when you want to check if you are in a SSH session from your shell. For example, I like to change the shell prompt based on whether it is in a local machine or in a SSH session on another machine.
An easy way to do this is to check if any environment variable related to SSH has been set. For example, I check if the SSH_CLIENT variable is set in my shell. In Fish, this can be done by checking the result of set --query SSH_CLIENT for example.
If the environment variable with many values already exists and you want to prepend or append a value to it, the technique is the same. For example, to prepend to PATH:
set -x PATH /home/joe/bin/some_foo_dir $PATH
Three special environment variables: PATH, CDPATH and MANPATH are treated differently by Fish. These arrive from the environment to Fish as colon-separated, they are converted to array so we can set them easily as shown above using space-delimiter and then they are sent back to the environment as colon-separated. This logic can be seen in the Fish source code file src/env.cpphere. This is also explained in this issue.
The problem now is that there are many other environment variables which need to be colon-delimited, but Fish does not do that. For example, the dynamic linker ld.so that is used to load up DLLs when a binary executes requires LD_LIBRARY_PATH environment variable to be colon-delimited. I got errors when this variable was set using space-delimiters: cannot open shared object file: No such file or directory
If you run into any problems with an environment variable that takes a series of values, then check back to see how it is set in traditional shells like Bash. If it is colon-delimited there, then you might need to make it colon-delimited in Fish too. But, do remember to enclose it in double quotes for variable expansion to work correctly. For example:
set -x FOO_ENV_VAR "/home/joe/bin/some_foo_dir:$FOO_ENV_VAR"
Autojump can be installed and used with Fish shell as described here. However, if you source your Fish config file repeatedly, then the autojump path appears repeated in your PATH environment variable:
When you open a man page you are confronted with a wall of white-on-black text. You may have noticed that man pages have some formatting information. This can be used to render the formatted elements in various colors. This can make reading and searching for information in a man page a pleasant affair.
To render man pages with colors in the Fish shell, add these lines to ~/.config/fish/config.fish:
set -x LESS_TERMCAP_mb (printf "33[01;31m")
set -x LESS_TERMCAP_md (printf "33[01;31m")
set -x LESS_TERMCAP_me (printf "33[0m")
set -x LESS_TERMCAP_se (printf "33[0m")
set -x LESS_TERMCAP_so (printf "33[01;44;33m")
set -x LESS_TERMCAP_ue (printf "33[0m")
set -x LESS_TERMCAP_us (printf "33[01;32m")
The difference in output is because these two time programs are different. The Bash time is a command internal to that shell. Fish does not have in inbuilt time command, so it runs the binary /usr/bin/time whose output is of the above format.
If you like the simple format of the Bash time command and want it under Fish, use the -p option of the time binary:
$ time -p ./do_something
If you would like this to be the default output format of time in Fish, then add the below lines to your ~/.config/fish/config.fish file:
function time --description="Time just like in Bash"
command time --portability $argv
Tried with: Time 1.7-24, Fish 2.1.1, Bash 4.3.11 and Ubuntu 14.04
The Fish shell makes it very easy to jump around in your directory history. This is the list of directories that you had changed to using the cd command.
Fish supports jumping back to the previous directory, just like in Bash: cd -
To view the directory history: dirh You can see your current directory highlighted in color in this list.
To move to the next and previous directory in your history, use the nextd and prevd commands.
Typing these commands is too much? Not to worry, at an empty Fish prompt, you can cycle to your previous and next directories by just pressing the Alt + Left and Alt + Right keys.
Note: The Alt + Left and Alt + Right shortcuts might have been grabbed by your terminal program, like GNOME Terminal. They need to be freed for this to work. I use Terminator, where I disabled these keybindings by right-clicking in the terminal -> Preferences -> Keybindings.
Tried with: Fish 2.2b1, Terminator 0.97-2 and Ubuntu 14.04