The Computer Oracle

Add abbreviations in fish config

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Unforgiving Himalayas Looping

--

Chapters
00:00 Add Abbreviations In Fish Config
00:37 Answer 1 Score 14
01:32 Answer 2 Score 3
02:43 Answer 3 Score 2
03:07 Answer 4 Score 0
03:47 Accepted Answer Score 0
04:29 Thank you

--

Full question
https://superuser.com/questions/1049368/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#shell #fish

#avk47



ANSWER 1

Score 14


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'



ANSWER 2

Score 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.




ANSWER 3

Score 0


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.




ACCEPTED ANSWER

Score 0


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.