Hi everyone! This is one trick I learned earlier to list the files in a directory, when changing into another directory. I think this is actually a pretty useful thing, since I most of the time type ls after I typed cd some_folder on the terminal. So here's how to do it.
Firstly, we'll talk about making a function in fish. A function in fish starts with a line with function with the function name, and ends with a line with end. It looks something like this:

function my_function
# do something
echo "hello world"
end
Then you just need to put it in your config.fish and most people have it in ~/.config/fish/config.fish (at least that's where mine is :p). After you save the file and source the file by executing this command on the terminal source ~/.config/fish/config.fish, your function name have now become a command, and if you type my_function on your terminal, it should print out "hello world".
Now, back to our plan to list the files inside the directory after we just got in. When we use cd we provide the folder name as an argument to it right? So that means we will need to get the argument into our function, and by default it will be $argv. This is similar to bash's $@. If we want to modify our previous function to print out anything provided to it instead of "hello world", we can do this:

function my_function
echo $argv
end
However, we've now got 1 problem. If the user did not specify any arguments, the function will just print an empty line. But in the case of cd-ing, we would want to go to home directory right? Then, we can treat that special case by checking if $argv is empty by using count. count $argv will print out the number of things inside $argv. Let's put some if statements to handle empty argument to our function:

function my_function
if count $argv > /dev/null
echo $argv
else
echo "no args given"
end
end
Note that I am putting the output to /dev/null just so it doesn't get printed out on the terminal screen. There are just a few more things that we need to know. Like how this && that in bash is written as this; and that in fish, and also if we do this "$argv", it will be treated as 1 thing.

Moving on to cd

Alright, now we have learned a few basics of fish shell, but just a few more things before putting them together. Since we want to call our function cd, we will need some ways so that our function does not recurse infinitely. To do that, we use the built-in cd command, by putting the word builtin before cd. Let's now put them together:

function cd
if count $argv > /dev/null
builtin cd "$argv"; and ls
else
builtin cd ~; and ls
end
end

Thanks for reading my blog and I hope you have learned something new and useful! :)