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.

  • Did you ever find a good way to do this? I agree that the answer below isn't so useful. – ezuk Apr 3 '16 at 6:02
  • No, I didn't yet :-/ – Daniel Rotter Apr 5 '16 at 7:37

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

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

if not set -q abbrs_initialized
  set -U abbrs_initialized
  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 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 gpl 'git pull'
  abbr gr 'git remote'
  abbr gs 'git status'
  abbr gst 'git stash'

  echo 'Done'
end
  • 1
    Thanks for this, reading the docs (fishshell.com/docs/current/commands.html) below the first snippet of code, the paragraph is really vague about it and only says it will "slow down the shell" but does not offer this incredibly simple alternative alongside it, cheers again! – SidOfc Feb 7 at 20:34

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.

Abbreviations are stored as universal variables in fish, so, in my opinion, they are more suitable to be used interactively as a quick & dirty™ way to persist your favorite expansions.

If you would like this configuration to be available in other boxes and sync them up with your dotfiles, I recommend creating a function as gco.fish, etc., for each abbreviation instead.

  • gco.fish

    function gco
        command git checkout $argv
    end
    

Functions are lazily loaded in fish and will always perform better than running abbr (which is also not a fish builtin, but a regular function with a definition type abbr) for each abbreviation in your config.fish.

  • But this way the aliases won't auto expand as if they were abbreviations, or do they? That's like the coolest thing about the abbreviations ;-) – Daniel Rotter Mar 8 '16 at 6:56
  • Yes, you are correct. It's a small tradeoff. I personally don't use abbr as they are implemented using universal variables and those, while super handy, it's something I will avoid if I can. – Jorge Bucaran Mar 8 '16 at 10:23
  • 1
    Another disadvantage is that auto complete doesn't work for me anymore... Typing gco <tab> doesn't show the branches anymore... – Daniel Rotter Mar 8 '16 at 21:03
  • @DanielRotter It would be too good if the function could inherit completions automatically. But the good news is, it would not be hard at all to use complete to copy the existing git completions into each of the functions. You will need a directory for your "git aliases", a functions directory for each function and a completions directory to add your completions, ideally one for each function too, but not necessarily so. – Jorge Bucaran Mar 9 '16 at 2:15
up vote 0 down vote accepted

I have been trying fish again, and found a solution which is working for me now. It is also described that way in the fish documentation.

They simply suggest to reset fish_user_abbreviations and call every abbr again and again. (I didn't even use the if in my case, also works well)

if status --is-interactive
    set -g fish_user_abbreviations
    abbr --add first 'echo my first abbreviation'
    abbr --add second 'echo my second abbreviation'
    # etcetera
end

It's actually similar to what MJV suggested, but without the downside that it is cached. The tradeoff is probably that startup is a little bit slower, but I'll keep an eye on that. I am going with that variant, since it is also mentioned in the documentation.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.