Add abbreviations in fish config

I would like to use the abbreviations feature of the fish shell, especially for the known bash variable !! or aliases to common git commands. I know that abbreviations can be added like in the following snippet:

abbr -a gco git checkout

I also know that this setting is persisted in a file located at .config/fish/fishd.host, but this file is also saying that it should not be edited, since changes might be lost (because the file is auto generated).

So my question is how to add this abbreviations in a config file, because I want to put my dotfiles in a git repository. And if I use these dotfiles on a separate machine I don't want to execute all these abbr command again.


Solution 1:

Sorry, I'm not sure if I understand what you're asking. But if your question is how to set your abbr definitions on startup (and in a file that you can store in a repository and share between machines), you should use the file ~/.config/fish/config.fish which is Fish's equivalent to .bashrc in Bash.

Since abbreviations are stored in global/universal variables, they don't need to be reset every time you open a terminal window, so you can place a safeguard in that config file to prevent resetting them every time (speeding things up a bit), as explained here: https://github.com/fish-shell/fish-shell/issues/1976#issuecomment-168698602

Edit (April 2019):

After Fish changed the way universal variables are stored in version 3.0.0 the safeguard functionality seems to prevent changes made in the abbr list to be included also after opening new terminal windows or rebooting one's machine. So, I've removed those lines from the example.

E.g. my config.fish looks like this (for Git commands):

  echo -n Setting abbreviations... 

  abbr g 'git'
  abbr ga 'git add'
  abbr gb 'git branch'
  abbr gbl 'git blame'
  abbr gc 'git commit -m'
  abbr gca 'git commit --amend -m'
  abbr gco 'git checkout'
  abbr gcp 'git cherry-pick'
  abbr gd 'git diff'
  abbr gf 'git fetch'
  abbr gl 'git log'
  abbr gm 'git merge'
  abbr gp 'git push'
  abbr gpf 'git push --force-with-lease'
  abbr gpl 'git pull'
  abbr gr 'git remote'
  abbr grb 'git rebase'
  abbr gs 'git status'
  abbr gst 'git stash'

  echo 'Done'

Solution 2:

Fixed this! What I ended up doing is just making the output of abbr -s into a Fish script. So in brief:

abbr -s > abbr.fish
(edit abbr.fish, add the following line to the top:) #! /usr/bin/fish
chmod +x abbr.fish

And you're done!

All you have to do now is just run abbr.fish on any Fish machine you want your abbreviations on, and presto.