Carlos Santos

iOS Dev

From zsh to fish - a tale of shells 🐚

2018-11-03 3 mintech

unsplash.com unsplash-logoleonides ruvalcabar

Recently I had huge performance problem with a zsh framework so I decided that it was time to finnaly debug and cut those painful 10s that my shell used to take to boot (you read that: 10 seconds).

Through the last 5 years I’ve tried several zsh frameworks, from oh-my-zsh to prezto, all had the same issue on my machine…

Solution

Guess what, it wasn’t zsh’s fault, of course 🤦‍♂️, neither a framework problem… it was rbenv that was taking a lot to load, so problem fixed, carry on.

Winds of change

Meanwhile, as I was along the way, I decided to try fish shell. I heard about that a few years ago, but zsh was working perfectly fine for me, apart from the rbenv issue, so I never took that final step.

So first impressions… 🤔

I was kind of scared that the old/ugly bash syntax that I’ve learned through the years didn’t work anymore here… CHAOS, PANIC, and now? No problem at all!

With a quick read on fish huge doc’s, I was able to start converting all my aliases from my previous .aliases file that I was loading on zsh boot.

Turns out, there are no aliases on fish… just functions 🤯.

Yes, we’ve been taugh since ever that 🍰 THE CAKE IS A LIE ALIAS 🍰


Define your first function

So, how to define a simple ls -lh alias? It’s actually really easy!

Open the shell, and start writing your new function:

$$ function ll
      ls -lh $argv
   end
  • Where ll is the function/alias name
  • $argv is the argument passed to the function
  • and all of this is coded on the shell, you can press ENTER to go to a new line
    • Just when you write the end keyword and hit ENTER the shell will assume your function

Finally, if we want to persist this new function to use it on every session, just type:

$$ funcsave ll

And your method will be save to the ~/.config/fish/functions folder, that you manipulate how you wish with your .dotfiles.

Keybindings

If you find yourself stuck in key bindings… like me… thinking what the hell is going on because skip words is not working (alt + L | alt + R)… you probably would like to check if you accidentaly enabled vi mode.

Wait, what? vi?

That’s correct! fish allows us to use vi keybindings to manipulate text.

Enabling vi mode

How did this happen? You may have set the variable fish_key_bindings with fish_vi_key_bindings, or some theme enabled that for you.

Troubleshooting that should be fairly simple: just search for fish_key_bindings on your config folder.

If you prefer regular key bindings, make sure you set your fish_key_bindings as fish_default_key_bindings.

You can achieve this by simply adding the following line to your config.fish file:

set -U fish_key_bindings fish_default_key_bindings

Cool functions

Here are listed a few functions that I use everyday that I find really useful.

function pubkey --description 'get your pub key on the clipboard'
    cat ~/.ssh/id_rsa.pub | pbcopy | echo '=> Public key copied to pasteboard.'
end

function wanip --description 'Print your IP addr'
    dig +short myip.opendns.com @resolver1.opendns.com
end

function cleancache --description 'Cleaning brew, Gems, yarn and npm caches'
    brew cleanup; gem cleanup; yarn cache clean; npm cache clean -f
end

Happy inputs! 💥