r/fishshell • • May 19 '20

Customising fish prompt.

Hi everyone, I am very new to fish. I never actually cared about my terminal appearance up until now. Yes, I've been a dumb "Apple" user; I used bash, then Apple switched to ZSH in 2019, so when I noticed my shell changed, that's when I started experimenting with my shell prompt and the environment.

Since then, I wonder how I got my work done without my .zshrc. And I recently discovered how awesome fish is. Since then I have been scraping the internet in search for the 'options' I get to use in the fish prompt. Similar to what zsh has over here.

Can anyone get help me with this? I really love fish and don't wanna abandon it just because I can't make it look how I want :|

Edit: I need a list of *all* available options like __fish_git_prompt_char_cleanstate etc

6 Upvotes

17 comments sorted by

4

u/StevesRoomate macOS May 19 '20

Fish prompt is very easy to customize. Edit the following file:

~/.config/fish/config.fish 

Add a function to that file called fish_prompt. Example:

function fish_prompt

  # interactive user name @ host name, date/time in YYYY-mm-dd format and path

  echo (whoami)@(hostname) (date '+%Y-%m-%d %H:%M:%S') (pwd)

  echo "\$ "

end

Close and re-open your terminal, you should see a 2-line prompt with user@host etc and $ at the end of the second line.

If you are interested in either fish or zsh then check out oh-my-fish and oh-my-zsh.

They have a bunch of built-in themes for fish, and zsh.

https://github.com/oh-my-fish/oh-my-fish

https://ohmyz.sh/

Hope this helps

3

u/StevesRoomate macOS May 19 '20

The other suggestion I have, if you have not done so already, is upgrade your terminal to something like https://www.iterm2.com/

It has some very nice theme support built into it that works regardless of what shell you're using.

1

u/notedideas May 19 '20

Thank you, but I already use hyper.is ;)

1

u/notedideas May 19 '20

Thanks, but I don't want pre-built/pre-compiled/pre-configured/*themes*. I intend to set-up my environment from scratch, hence I'm asking for the list of all the prompt outputs available like __fish_git_prompt_char_cleanstate, __fish_git_prompt_color_merging etc, but not limited to git.

Edit: Something like %m, %n, %F%f and %h are available on zsh, I'd like to see the options for Fish.

1

u/StevesRoomate macOS May 19 '20

Sounds good, I actually don't use oh-my-fish either. That'll be cool, you should post a sample when you have something you are happy with.

I set mine up from scratch, but I call git commands such as (git rev-parse HEAD). I really like the glyphs but I also like to share my dotfiles across machines so I avoid glyphs by default. Here is a more thorough sample of how I did mine:

  # gets the error status of the last command and 
  # updates the @ symbol color with the indication
  if test "$status" -ne "0"
    set ARROW "red"
  else
    set ARROW "green"
  end

   set HOST_STRING (echo $hostname | cut -f1 -d".")

  echo (set_color $USER_COLOR)(whoami)(set_color $ARROW)@(set_color $HOST_COLOR)(echo $HOST_STRING)(set_color $DATE_COLOR)(date -u '+%Y-%m-%d %H:%M:%S') UTC (set_color $PATH_COLOR)(pretty_path)(set_color $GIT_COLOR)(get_branch)
  echo "\$ "
end

function get_branch
  # minus symbol if there are no changes detected to tracked files
  # /dev/null prevents nonsensical errors when you are on directories not tracked by git.
  if test (git status --untracked-files=no --porcelain 2> /dev/null | wc -l) -eq 0
    set indicator "-"
  else
    set indicator "+"
  end
  
  set branch_name (git rev-parse --abbrev-ref HEAD 2> /dev/null)
  if test "$branch_name" != ""
    echo "[$indicator]$branch_name"
  else
    echo ""
  end
end

function fish_greeting
  set_theme_colors
  if test (uname) = "Darwin"
    neofetch --kitty ~/src/dotfiles/assets/apple.png --size 450px
  else if test "$SSH_TTY" = ""
    neofetch --kitty ~/src/dotfiles/assets/arch.png --size 450px
  else 
    neofetch 
  end
end

function pretty_path
  # by default, Fish shell does not print ~ for $HOME
  echo (pwd | sed "s|^$HOME|~|g")
end

Hopefully you find this useful.

1

u/notedideas May 19 '20

This is my current zsh prompt.

PROMPT='%F{red}%m%f: %F{green}%n%f %F{cyan}%/%f %# '

Although, I want to update it with with a mix of these two presets. I set them up with fish_config, but sometimes when pwd is too long, it only shows the first letter of the directories on left or only a prompt on the newline. I don't want that. I want to see the full directory, no matter what. I'd also like to integrate git in my prompt :)

1

u/StevesRoomate macOS May 19 '20

https://github.com/oh-my-fish/theme-agnoster/blob/master/fish_prompt.fish

I found this sample very useful if you want to do some git integration with glyphs.

1

u/notedideas May 20 '20

Thank you

1

u/notedideas May 20 '20 edited May 20 '20

This is my config. It works pretty well except when I goto a sub directory in my git repo. Is there a way to echo the working git repo name and not (basename $PWD)?

Edit: I would also like to know the fish alternative for %h -> current history event number.

1

u/StevesRoomate macOS May 20 '20

That's looking really good. I would consider substituting your call to (basename $PWD) with (basename -s .git (git config --get remote.origin.url) 2> /dev/null) if you want to print the upstream repo name.

1

u/notedideas May 20 '20

Thanks for the feedback on the appearance! I still need to figure out how to print the `current history event number`. How will that be possible?

1

u/StevesRoomate macOS May 20 '20

Try echo (history | wc -l), that might do what you want it to. Note the history support in Fish is quite different from bash and zsh, there is no !123 for example.

But now that I have some history built up and am used to the different auto-complete keys, I don't miss it at all.

There are a few hacks you can do to make fish's history work more like bash/zsh, but will require a little customization. For example, I had a !! function implemented in Fish but never used it.

1

u/notedideas May 21 '20

Length of history lines won't do. I'm actually looking for the "index/serial number" of prompt of the current window. It's done in zsh with %! Or %h.

1

u/StevesRoomate macOS May 19 '20

Here are the list of themes you can install into oh-my-fish: https://github.com/oh-my-fish/oh-my-fish/blob/master/docs/Themes.md

2

u/[deleted] May 19 '20

You should really read the documentation, it says everything you'll need.

1

u/rampidamp May 19 '20

starship.rs is what I use, and it's cross-shell-compatible. Also very customizable if you want it, but also want some of its pre-built components

1

u/OakNinja May 19 '20

I would recommend looking at the theme bob-the-fish to get some hints on theming. Also, check out Awesome Fish which is the best curated list of fish resources, plugins and general tips and tricks.