Fish config

Table of Contents

1 Presentation

The file present in ~/.config/fish/config.fish is the configuration file for the fish shell. It contains custom functions, environment variables and abbreviations.

Just in case, we might need sometimes to declare the fish function fish_title as true, so let’s do so.

function fish_title
    true
end

2 Fish from within Emacs

I sometimes call fish from within emacs, with M-x ansi-term. In this case, the variable TERM needs to have the value eterm-color.

if test -n "$EMACS"
    set -x TERM eterm-color
end

3 Tramp remote access

When accessing from a remote machine our computer from Emacs, tramp needs a precise shell appearance: a simple $ followed by a space after which to put the commands it needs to execute, and nothing else. Due to this, let’s deactivate and redefine some of the functions defining the appearance of fish.

if test "$TERM" = "dumb"
    function fish_prompt
        echo "\$ "
    end
    function fish_right_prompt; end
    function fish_greeting; end
    function fish_title; end
end

4 Regular fish shell appearance

Now, there is only one function I modify when it comes to the appearance of fish when I’m the one using it: the fish_greeting function. I use it to give me an overview of my computer’s status, including its hostname, uptime, disks usage, ram usage, swap usage, and networking.

set RED '\033[0;31m'
set GREEN '\033[0;32m'
set NC '\033[0m'

function display_slider # used total
    set -l slider_length 38
    set -l used $argv[1]
    set -l total $argv[2]
    set -l used_slider (math -s0 "($used * $slider_length) / $total")
    set -l unused_slider (math -s0 "$slider_length - $used_slider")
    echo -en "["
    echo -en $RED
    echo -en (string repeat -n $used_slider '=')
    echo -en $GREEN
    echo -en (string repeat -n $unused_slider '=')
    echo -en $NC
    echo -en "]"
end

function fish_greeting
    set -l ruler_length 79
    set -l ruler (string repeat -n $ruler_length "=")

    set -l osname (cat /etc/os-release | grep -i pretty_name | sed 's/.*"\(.*\)".*/\1/')
    set -l uptime (uptime -p | sed 's/up //')

    set -l root (df -Ph | grep -E "/\$")
    set -l root_p (echo $root | awk '{print $5}' | tr -d '%')
    set -l root_used (echo $root | awk '{print $3}')
    set -l root_total (echo $root | awk '{print $2}')

    set -l ram (free -tm | grep Mem)
    set -l ram_total (echo $ram | awk '{print $2}')
    set -l ram_used (echo $ram | awk '{print $3}')
    set -l ram_p (math -s0 "$ram_used / $ram_total * 100")

    set -l swap (free -tm | grep Swap)
    set -l swap_total (echo $swap | awk '{print $2}')
    set -l swap_used (echo $swap | awk '{print $3}')
    set -l swap_p (math -s0 "$swap_used / $swap_total * 100")

    set -l connections (nmcli c s | grep -E "wifi|ethernet" | grep -v '\-\-')
    set -l wifi (echo $connections | grep "wifi" | awk '{print $1}')
    set -l ethernet (test "$connections" = "*ethernet*" && echo -e $GREEN"UP"$NC || echo -e $RED"DOWN"$NC)
    set -l wifi (test -n wifi && echo -e $GREEN$wifi$NC || echo - $RED"DOWN"$NC)

    echo $ruler
    printf "OS......: %-30sKernel: %s %s\n" $osname (uname -s) (uname -r)
    printf "Hostname: %-30sUptime: %s\n" (hostname) $uptime
    printf "Ethernet: %-41sWifi..: %s\n" $ethernet $wifi
    printf "Disks...: %-6s %s %6s / %6s (%2d%%)\n" "/" (display_slider $root_p 100) $root_used $root_total $root_p

    # loop other mountpoints
    for mp in (df -Ph 2> /dev/null | egrep "sd|tank|nvme" | egrep -v "boot|/\$")
        set -l mp_p (echo $mp | awk '{print $5}' | tr -d '%')
        set -l mp_used (echo $mp | awk '{print $3}')
        set -l mp_total (echo $mp | awk '{print $2}')
        set -l mp_name (echo $mp | awk '{print $6}')
        printf "          %-6s %s %6s / %6s (%2d%%)\n" $mp_name (display_slider $mp_p 100) $mp_used $mp_total $mp_p
    end

    printf "Ram.....:        %s %5dM / %5dM (%2d%%)\n" (display_slider $ram_used $ram_total) $ram_used $ram_total $ram_p
    printf "Swap....:        %s %5dM / %5dM (%2d%%)\n" (display_slider $swap_used $swap_total) $swap_used $swap_total $swap_p
    echo $ruler
end

The theme I use for fish is bobthefish, which by default puts a really long timestamp to the right of the prompt. I want something shorter, so here is the variable to set, using the format specified in date(1).

set -g theme_date_format "+%g-%m-%d %H:%M:%S"

I also wish to have a kinda different newline prompt, so let’s set it:

set -g theme_newline_prompt 'λ '

Finally, let’s set our prompt’s theme to the Nord theme.

set -g theme_color_scheme nord

5 Global variables

In order to keep some other code clean, I set the $BROWSER variable so I don’t have to call my web browser directly but rather with this variable.

set -gx BROWSER firefox

Sometimes, software will rely on SUDO_ASKPASS to get a GUI from which it can get the sudo password. So, let’s declare it.

set -gx SUDO_ASKPASS ~/.local/bin/askpass

5.1 Development

Now, let’s declare our editor of choice, EmacsClient; not Emacs itself since it will most often be just quick edits, nothing too heavy, if it is called from the EDITOR variable (from Git, for example), or from the VISUAL variable.

set -gx EDITOR emacsclient -c
set -gx VISUAL emacsclient -c

We also need to set the path to the Dart SDK.

set -gx DART_SDK /opt/dart-sdk/bin

And we also need to specify where the Android SDK it located.

set -gx ANDROID_HOME $HOME/Android/Sdk

Still related to Dart and Flutter development,

set -gx CHROME_EXECUTABLE /usr/bin/chromium

Next, we have two variables from Deno, the Node.js destroyer. Its base directory will be set in my XDG config directory, and its binaries will be located in my local binaries directory (see below).

set -gx DENO_DIR $HOME/.config/deno
set -gx DENO_INSTALL_ROOT $HOME/.local/bin/deno

Finally, some development packages require the PKG_CONFIG_PATH to be set, so let’s do so.

set -gx PKG_CONFIG_PATH /usr/local/lib/pkgconfig/ $PKG_CONFIG_PATH

5.2 $PATH

A variable available with the fish shell is fish_user_paths which lists custom paths to binaries specified by the user. Using this variable ensures they are included in the $PATH variable only once without the need to set it directly. For instance, my PATH variable needs Rust’s Cargo’s binaries, Go’s binaries, my own executables, and some more.

additional path what it leads to
$HOME/.pub-cache/bin Dart binaries and executables
$HOME/.local/bin Custom executables, see bin.html
$HOME/go/bin Go binaries and executables
$HOME/.cargo/bin Rust binaries and executables
$HOME/.gem/ruby/2.6.0/bin Ruby binaries and executables
$HOME/.cabal/bin Haskel binaries

So, let’s set our user paths:

set -g fish_user_paths \
$HOME/.pub-cache/bin \
$HOME/.local/bin \
$HOME/go/bin \
$HOME/.cargo/bin \
$HOME/.gem/ruby/2.6.0/bin \
$HOME/.cabal/bin

6 Abbreviations

Abbreviations are a great way to keep correctly track of which commands are run in the shell without polluting the history of the shell with obscure commands. When typing an abbreviation, fish will replace it with replace it with its expanded equivalent. Below are some of the abbreviations I use. Be aware some of them that invoke GUI programs can be invoked through devour which will make the terminal disappear while the program runs, and once the GUI program exists the terminal window will come back.

6.1 System monitoring

Here I have some abbreviations which are quite useful when performing some system monitoring. With df, we can get an overview of our filesystem usage, while with diskspace we get some more precise information. meminfo is a call to free with sane defaults, and similar to meminfo, we also have gpumeminfo so we can get a quick look at the memory-related logs of our X session. I also declared cpuinfo an alias of lscpu in order to keep consistent with meminfo. pscpu gives us information on what the CPU is running right now, and pscpu10 limits that to the top 10 threads. Similarly, psmem gives us information on the memory usage of the current threads, and psmem10 only the ten most important threads in terms of memory usage.

abbreviation command
df df -H
diskspace sudo df -h | grep -E "sd|lv|Size"
du du -ch
meminfo free -m -l -t
gpumeminfo grep -i –color memory /var/log/Xorg.0.log
cpuinfo lscpu
pscpu ps auxf | sort -nr -k 3
pscpu10 ps auxf | sort -nr -k 3 | head -10
psmem ps auxf | sort -nr -k 4
psmem10 ps auxf | sort -nr -k 4 | head -10
abbr df 'df -H'
abbr diskspace 'sudo df -h | grep -E "sd|lv|Size"'
abbr du 'du -ch'
abbr meminfo 'free -m -l -t'
abbr gpumeminfo 'grep -i --color memory /var/log/Xorg.0.log'
abbr cpuinfo 'lscpu'
abbr pscpu 'ps auxf | sort -nr -k 3'
abbr pscpu10 'ps auxf | sort -nr -k 3 | head -10'
abbr psmem 'ps auxf | sort -nr -k 4'
abbr psmem10 'ps auxf | sort -nr -k 4 | head -10'

6.2 System management (packages and services)

I added some of these abbreviations due to how often I have to write the whole thing.

6.2.1 Package management

The first command is remove which removes a package from my system, as well as its dependencies no longer needed. p. pacman's or paru's. This is why I simply type purge. And if I want to simply seach among the pacman repos, I can type search. Otherwise, if I want to include AUR results, I’ll use paru.

abbreviation command
remove sudo pacman -Rscnd
purge paru -Sc
abbr remove 'sudo pacman -Rscnd'
abbr purge 'paru -Sc'

6.2.2 Service management

I don’t have the muscle memory of systemctl. So instead, I simply type c when I want to do something user service related. And if I want to manipulate system services, I can instead type a simple capital S.

abbreviation command
s systemctl
suser systemctl –user
abbr s 'systemctl'
abbr suser 'systemctl --user'

6.3 Development

A good amount of these commands are development related, especially when it comes to compilation or Docker.

6.3.1 CMake

I have the following abbreviations so I can quickly run CMake and create a configuration for debug or release profiles.

abbreviation command
cdebug cmake -DCMAKE_BUILD_TYPE=Debug
crelease cmake -DCMAKE_BUILD_TYPE=Release

Here is the corresponding fish configuration:

abbr cdebug 'cmake -DCMAKE_BUILD_TYPE=Debug'
abbr crelease 'cmake -DCMAKE_BUILD_TYPE=Release'

6.3.2 Docker

And of course, when it comes to Docker Compose, I don't have time to write the full command, so I use these instead.

abbreviation command
dc docker-compose
dcb docker-compose build
dcd docker-compose down
dcl docker-compose logs
dcp docker-compose pull
dcr docker-compose run –rm
dcu docker-compose up
dcub docker-compose up –build
dcud docker-compose up -d
dcudb docker-compose up -d –build

Here is the corresponding fish configuration:

abbr dc 'docker-compose'
abbr dcb 'docker-compose build'
abbr dcd 'docker-compose down'
abbr dcl 'docker-compose logs'
abbr dcp 'docker-compose pull'
abbr dcr 'docker-compose run --rm'
abbr dcu 'docker-compose up'
abbr dcub 'docker-compose up --build'
abbr dcud 'docker-compose up -d'
abbr dcudb 'docker-compose up -d --build'

6.3.3 Text editors

I greatly prefer to use Emacsclient as my main text editor; Emacs has basically all I need. So, it's only normal I have an abbreviation to launch a new instance of it. If launched in the terminal, I’ll usually want Emacs to be displayed in CLI mode and not in GUI mode, otherwise I would invoke it with my WM’s shortcut. I also wish to completely stop using other text editors, such as vi, vim, nano and ed, so let's all add their command as an abbreviation for Emacs. In case we want to launch Emacs in GUI mode anyways, egui is available too.

abbreviation command
e emacsclient -c -nw
vi emacsclient -c -nw
vim emacsclient -c -nw
nano emacsclient -c -nw
ed emacsclient -c -nw
egui devour emacsclient -c

Here is the corresponding fish configuration:

abbr e 'emacsclient -c -nw'
abbr vi 'emacsclient -c -nw'
abbr vim 'emacsclient -c -nw'
abbr nano 'emacsclient -c -nw'
abbr ed 'emacsclient -c -nw'
abbr egui 'devour emacsclient -c'

6.3.4 Compilation

By default, I set clang, clang++, gcc and g++ to the latest standard and with the -Wall flag activated.

abbreviation command
clang clang -Wall
clang++ clang++ -Wall
g++ g++ -Wall -std=c++20
gcc gcc -Wall -std=c18

Here is the corresponding fish configuration:

abbr clang 'clang -Wall'
abbr clang++ 'clang++ -Wall'
abbr g++ 'g++ -Wall -std=c++20'
abbr gcc 'gcc -Wall -std=c18'

6.3.5 Git

And let's face it: we all at one point just wanted to commit our code without thinking about the message, to just get over with it. Don't worry, I got you covered.

abbreviation command
randcommit git commit -m (curl -s whatthecommit.com/index.txt)

Here is the corresponding fish configuration:

abbr randcommit 'git commit -m (curl -s whatthecommit.com/index.txt)'

6.4 LaTeX

Yes, although I use org-mode, I still have some use for LaTeX, especially when it comes to PDF exports of my org files. Hence why I use the LaTeX package manager. It is recommended to use tllocalmgr instead of tlmgr, but I can never remember the command, and the latter is faster to type, so time for an abbreviation. Same goes for texhash which must be run as sudo.

abbreviation command
tlmgr tllocalmgr
texhash sudo texhash

Here is the corresponding fish configuration:

abbr tlmgr 'tllocalmgr'
abbr texhash 'sudo texhash'

6.5 Some security measures

Some commands can be quite dangerous when not used properly, which is why I added default flags and options so I can get warnings before things get ugly. The -i and -I add prompts in case we might not want to do what we asked the shell to do. Notice lns which creates symlinks, rmd which removes directories, rmf which forces deletion, and rmdf which forces the delition of a directory. Notice also the --preserve-root which will prevent me from accidentally removing the root folder. I added the same option to chgrp, chmod, and chown.

abbreviation command
cp cp -i
ln ln -i
lns ln -si
mv mv -i
rm rm -Iv
rmd rm –preserve-root -Irv
rmdf rm –preserve-root -Irfv
rmf rm –preserve-root -Ifv
chgrp chgrp –preserve-root -v
chmod chmod –preserve-root -v
chown chown –preserve-root -v

Here is the corresponding fish configuration:

abbr cp 'cp -i'
abbr ln 'ln -i'
abbr lns 'ln -si'
abbr mv 'mv -i'
abbr rm 'rm -Iv'
abbr rmd 'rm --preserve-root -Irv'
abbr rmdf 'rm --preserve-root -Irfv'
abbr rmf 'rm --preserve-root -Ifv'
abbr chgrp 'chgrp --preserve-root -v'
abbr chmod 'chmod --preserve-root -v'
abbr chown 'chown --preserve-root -v'

6.6 Typos

Let's admit it, we all make typos from time to time in the shell, and some are recurrent enough we make abbreviations or aliases of the correct command. Well, I have some of my abbreviations which were make exactly because of this. Sometimes for some reasons, my brain makes me write clean instead of clear. So, let's just replace the former by the latter. I'm also very bad at typing exit. And sometimes I suck at typing htop.

abbreviation command
clean clear
exi exit
exti exit
hotp htop

Here is the corresponding fish configuration:

abbr clean 'clear'
abbr exi 'exit'
abbr exti 'exit'
abbr hotp 'htop'

6.7 Misc

Finally, some miscellaneous abbreviations that don't really fit into any of the above categories.

6.7.1 Media

Here you will find various commands related to media in general. the first one is a command to play some chillhop from the Chillhop YouTube channel's livestream.

abbr chill 'mpv --force-window=no --no-video "https://www.youtube.com/user/Chillhopdotcom/live" &'

When it comes to mpv, I do not want to force it to open a graphical window if for example I want to listen to an audio file. I also do not want any border on that window. So, I declared this abbreviation.

abbr mpv 'devour mpv --no-border --force-window=no'

When I want to download a song from YouTube, I'll just use the command flac videoIdentifier to get it through youtube-dl.

abbr flac 'youtube-dl -x --audio-format flac --audio-quality 0 -o "~/Music/%(uploader)s/%(title)s.%(ext)s"'

Some sane default options for sxiv, a simple X image Viewer. This includes playing GIFs and not displaying the filename below. Sxiv will also open in fullscreen and will fit the displayed image to the frame.

abbr sxiv 'sxiv -abfs f'

Finally, let's declare the following abbreviation that will launch an mpv instance displaying my webcam:

abbr webcam 'devour mpv --demuxer-lavf-format=video4linux2 --demuxer-lavf-o-set=input_format=mjpeg av://v4l2:/dev/video0'

6.7.2 Sudo

First, I make it so that sudo comes with the -A switch in order to call my custom graphical script for getting my password (see askpass). I also made it so please is an equivalent to sudo -A as a joke.

abbr please 'sudo -A'

6.7.3 History

I find it more intuitive and faster to just write hist instead of history, so let's declare that.

abbr hist history

6.7.4 Compression

It seems it's just like many other people, but I cannot for the life of me remember the syntax of tar. So, I made the following abbreviations, and one day hopefully, after seeing the abbreviations' expansion over and over I'll remember the command like I did for the abbreviation of remove (see Package management).

abbreviation command
compress tar -czf
untar tar -xvzf
abbr compress 'tar -czf'
abbr untar 'tar -xvzf'

6.7.5 exa

abbreviation command
exa exa -halg@ –group-directories-first –git
lsl exa -halg@ –group-directories-first –git
abbr exa 'exa -halg@ --group-directories-first --git'
abbr lsl 'exa -halg@ --group-directories-first --git'

6.7.6 Network Management

First, we have just nmcli with sane default options, that is a pretty output with colors.

abbr nmcli 'nmcli -p -c auto'

6.7.7 NordVPN

Next, we have some NordVPN-related shortcuts. The first one is a simple abbreviation to nordvpn. The second one is a shortcut to connect to a server, and to disconnect from the current server. I also have a couple of shortcuts to quickly connect to some preselected countries, mainly France, Germany, Japan and the US.

abbreviation command
n nordvpn
nc nordvpn c
nd nordvpn d
ncf nordvpn c France
ncg nordvpn c Germany
ncj nordvpn c Japan
ncu nordvpn c United_States
abbr n 'nordvpn'
abbr nc 'nordvpn c'
abbr nd 'nordvpn d'
abbr ncf 'nordvpn c France'
abbr ncg 'nordvpn c Germany'
abbr ncj 'nordvpn c Japan'
abbr ncu 'nordvpn c United_States'

6.7.8 Wget

By default, continue a download that was interupted.

abbr wget 'wget -c'

Author: Lucien Cartier-Tilet

Email: lucien@phundrak.com

Created: 2021-02-04 jeu. 14:46