Skip to main content
  1. Writings/

Making a Custom Fish Greeting

·4 mins

fish is the modern shell for power users and one of the great things about it is its practically endless customisation possibilities. If you’re not already using fish as your preferred shell, visit my guide on Switching to Fish Shell to get set up, then come back here and follow along. Let’s get started!

To make a custom greeting you first need to create a fish_greeting.fish file in ~/.config/fish/functions/. Files that are created in this folder are auto-loaded each time a new fish session is started and fish_greeting is a special function that is called at the start of each session. I’ll use nano to create and edit the file, but you can use any text editor of your choosing:

nano ~/.config/fish/functions/fish_greeting.fish

We’ll start with something basic, so paste the following into your file:

function fish_greeting
  printf "Welcome! Let's fish!\n"
end

This is the basic structure of a fish function and it will simple output the string, “Welcome! Let’s fish!”. Press Control + X and then Y to save. Press Return when asked to confirm the filename. You can now test your new greeting by simply calling the function:

Terminal screenshot showing the output of fish_greeting

Pretty neat, right? Now that we have our basic function up and running, let’s take it a step further. I spend a lot of time at the command line and to make it a little more fun, I want to have a custom random greeting every time I open a new Terminal window. Let’s change the code to make our function pick a string from a random set of possible greetings. Reopen fish_greeting.fish in your text editor and change the code to the following:

function fish_greeting

	# Greeting messages
	set powered_msgs \
		"candy!" \
		"rubber bands" \
		"a black hole" \
		"logic" \
		"electromagnetic cheese"

	# Randomly pick a message
	set chosen_msg (random)"%"(count $powered_msgs)
	set chosen_msg $powered_msgs[(math $chosen_msg"+1")]

	# Output it to the console
	printf (set_color F90)"Welcome! This terminal session is powered by %s\n" $chosen_msg

end

Before you close your editor, let’s break down how the new function works.

  • powered_msgs is an array of possible greetings. You can edit this to add as many greetings as you like (I have over a hundred!). Just be sure each entry is on its own line, surrounded by double-quotes, and for every item except the last, you’ll need to have a space followed by a backslash.
  • The (random) function is used to pick one string from the list of greetings and then that item is stored in chosen_msg for use in the next step.
  • Next, we use printf to compose and output our full message. We prefix the message with a short “Welcome!” string, which you can also customise however you like. The %s is a special placeholder that tells printf where we want to place our randomly chosen string and the \n outputs a new line character so that there’s a break between our custom greeting and the fish command prompt. Finally, to make the message stand out, we set its colour to orange by prefixing the string with (set_color F90), **** but you can use any hexadecimal colour you like. F90 is just shorthand for FF9900, however you can use either three- or six-digit hex values and fish will use the closest supported colour.

Once you’ve saved the file and exited your text editor, try running fish_greeting a couple of times and see what you get. You should see a new random greeting message each time.

Terminal screenshot showing the output of fish_greeting

The possibilities are endless and you can use any of fish’s functions in your custom greeting to be as creative as you like. You’ll find a full list of available commands in the fish documentation. For instance, you could output the date with date or the current working directory with pwd. Keep experimenting until you’re a happy fishy! 🐠

That’s all there is to it! If you ever want to remove your custom greeting, you can simply delete the ~/.config/fish/functions/fish_greeting.fish file.