BACK TO THE HOMEPAGE

Aug 25, 2021 3 min read

Fish on Cloudshell

Fish is described as a smart and user-friendly command shell. As you would expect it works on Linux and is super useful to get that optimized experience. The blog post covers installation on GCP Cloud Shell I would expect the installation process to be similar on any Linux based machine.

If you want to use oh-my-bash, read a previous post on using Cloud Shell with oh-my-bash

You are good to go!

Installation

  1. Open Cloud Shell in the Google Cloud console
  2. Install the Fish package by typing:
NODE_TYPE // text
sudo apt install fish
  1. Once installed, at the command line type:
NODE_TYPE // text
fish

Nice you are now using the fish application in Cloud Shell

Useful command prompt

Fish configuration is performed using a web portal. Running the configuration app uses Port 8000.
Unfortunately this doesnt appear to work in Cloud Shell. However just need the generated files.

The following function provides a basic prompt for git

  1. Make a new subdirectory
NODE_TYPE // text
mkdir -p ~.config/fish/functions
  1. Add the above function to ~/.config/fish/functions/fish_prompt.fish
NODE_TYPE // text
function fish_prompt
        set -l __last_command_exit_status $status

    if not set -q -g __fish_robbyrussell_functions_defined
        set -g __fish_robbyrussell_functions_defined
        function _git_branch_name
            set -l branch (git symbolic-ref --quiet HEAD 2>/dev/null)
            if set -q branch[1]
                echo (string replace -r '^refs/heads/' '' $branch)
            else
                echo (git rev-parse --short HEAD 2>/dev/null)
            end
        end

        function _is_git_dirty
            echo (git status -s --ignore-submodules=dirty 2>/dev/null)
        end

        function _is_git_repo
            type -q git
            or return 1
            git rev-parse --git-dir >/dev/null 2>&1
        end

        function _hg_branch_name
            echo (hg branch 2>/dev/null)
        end

        function _is_hg_dirty
            echo (hg status -mard 2>/dev/null)
        end

        function _is_hg_repo
            fish_print_hg_root >/dev/null
        end

        function _repo_branch_name
            _$argv[1]_branch_name
        end

        function _is_repo_dirty
            _is_$argv[1]_dirty
        end

        function _repo_type
            if _is_hg_repo
                echo 'hg'
                return 0
            else if _is_git_repo
                echo 'git'
                return 0
            end
            return 1
        end
    end

    set -l cyan (set_color -o cyan)
    set -l yellow (set_color -o yellow)
    set -l red (set_color -o red)
    set -l green (set_color -o green)
    set -l blue (set_color -o blue)
    set -l normal (set_color normal)

    set -l arrow_color "$green"
    if test $__last_command_exit_status != 0
        set arrow_color "$red"
    end

    set -l arrow "$arrow_color➜ "
    if test "$USER" = 'root'
        set arrow "$arrow_color# "
    end

    set -l cwd $cyan(basename (prompt_pwd))

    if set -l repo_type (_repo_type)
        set -l repo_branch $red(_repo_branch_name $repo_type)
        set repo_info "$blue $repo_type:($repo_branch$blue)"

        if [ (_is_repo_dirty $repo_type) ]
            set -l dirty "$yellow ✗"
            set repo_info "$repo_info$dirty"
        end
    end

    echo -n -s $arrow ' '$cwd $repo_info $normal ' '
end

Other settings

If you want to generate other functions/settings, try running fish on your local machine to grab the config.

  1. The command to use is:
NODE_TYPE // text
fish_config
  1. Open the browser e.g. http://localhost:8000
  2. Select the configuration options required
  3. Add the generated file(s) across to Cloud Shell.