What is Fish Shell abbr?

In Fish shell, abbr (short for abbreviation) is a feature that allows you to create dynamic, auto-expanding text shortcuts. Unlike aliases, which require explicit execution, abbreviations expand immediately as you type them and press Space or Enter, similar to text snippets in a text editor. This feature is designed to improve productivity and reduce typing effort.

Advantages

  • Improved productivity – Save time by reducing the need to type long commands repeatedly. Abbreviations automatically expand into the full command as soon as you press space, Enter, or other delimiters.
  • Contextual usage – Works in interactive shells but does not affect scripting.
  • Easier to edit – Can be modified or removed dynamically without editing configuration files.
  • Supports arguments – Expands correctly even if additional arguments are appended.

Disadvantages

  • Only works in interactive mode – Unlike aliases, abbreviations don’t work in scripts.
  • Might not suit all workflows – If you prefer seeing the short form before execution, an alias might be better.
  • Not as flexible as functions – For complex command modifications, functions are more powerful.

Usage

The abbr command is used to create, list, and manage abbreviations in Fish. Here’s the basic syntax:

abbr --add <abbreviation> "<full command>"
  • --add: Adds a new abbreviation.
  • <abbreviation>: The shortcut you want to use.
  • <full command>: The full command that the abbreviation expands to.

1. Creating an abbreviation

Create an abbreviation gco for git checkout with option --add or -a

abbr -a gco "git checkout"

Now, when you type gco and press Space or Enter, it will expand to git checkout.

2. Listing all abbreviations

Use the abbr command without arguments to list all abbreviations.

abbr

3. Delete an abbreviation

Use the --erase or -e option to remove an abbreviation.

abbr -e gco

4. Save abbreviations permanently

Fish remembers abbreviations across session, but they are not automatically saved in Fish’s configuration file (~/.config/fish/config.fish). If you want them permanently, you must manually add abbr commands to your config file.


Examples: From Basic to Advanced

1. Basic: Simple Git Abbreviations

abbr -a gst "git status"
abbr -a gaa "git add --all"
abbr -a gcm "git commit -m"

Usage:

  • Typing gst and pressing Space expands to git status.

2. Intermediate: System Commands

abbr -a c "clear"
abbr -a ll "ls -lh"
abbr -a .. "cd .."
abbr -a ... "cd ../.."

Usage:

  • Typing .. expands to cd ..
  • Typing … expands to cd ../..

3. Advanced Examples

Abbreviation for a Complex Command

Create an abbreviation adb-ls-packages to list all available packages in the device via adb

abbr -a -g adb-ls-packages "adb shell pm list packages | awk -F \":\" '{print \$2}'"

Abbreviation with Pipes

  • Copying your SSH public key to clipboard
abbr -a pubkey "cat ~/.ssh/id_rsa.pub | pbcopy"
  • Searching for a “pattern” string inside a working directory by grep and fzf
abbr --add grepf 'grep -r "pattern" . | fzf'

Abbreviation with the Cursor

Back to the above example for grepf, when use it you need to use arrow keys to move the cursor to inside the quotes then delete the “pattern” string, then type the new one. From the version 3.6.0 (#9313), Fish shell supports this feature by adding the new option --set-cursor. So if you want to create an abbreviation for grep -r "" . | fzf and have the cursor placed inside the quotes, you can do:

abbr --add grepf --set-cursor 'grep -r "%" . | fzf'

Noted that we will use % to marks the cursor position.

Conclusion

The abbr feature in Fish is a powerful tool for improving productivity and reducing repetitive typing. It’s especially useful for developers, system administrators, and anyone who frequently uses the command line. By creating meaningful abbreviations, you can streamline your workflow and make your command-line experience more efficient.