fish: Refactor fish shell configuration

- Drop starship. This also helps us to get rid of ttf-font-nerd install
  which is a dependency of starship.
- Source fnm env only once on login. This makes fish shell/terminal load
  much much faster. Add an alias to invoke fnm use easily.
- Set path or environment variables only when required path or binary
  exists.
- Introduce a function to profile fish easily.
- Borrow the fish prompt logic from Alexis King's configuration.
- Factor out key binding related settings.
- Make su launch fish.

Prompt and other ideas for clean up taken from
https://github.com/lexi-lambda/dotfiles/tree/master/fish
This commit is contained in:
Sanchayan Maity 2021-12-27 11:57:49 +05:30
parent c6a7ba29bd
commit e541b2d745
11 changed files with 162 additions and 34 deletions

View file

@ -1 +0,0 @@
fnm env --use-on-cd --log-level error | source

View file

@ -0,0 +1,52 @@
function __fish_setup_on_tty_login --description "Set up environment on tty login"
for extra_path in ~/.{local,nix-profile,cargo,cabal,ghcup}/bin
if test -d "$extra_path"
set -gxp PATH "$extra_path"
end
end
if type -qf nvim
set -gx EDITOR nvim
set -gx VISUAL nvim
set -gx MANPAGER 'nvim +Man!'
else
if type -qf vim
set -gx EDITOR vim
set -gx VISUAL vim
end
end
if test -d {$HOME}/.nix-defexpr/channels
set -gx NIX_PATH {$HOME}/.nix-defexpr/channels
set -gx NIX_SSL_CERT_FILE '/etc/ssl/certs/ca-certificates.crt'
end
if test -d /var/lib/flatpak
# https://github.com/fish-shell/fish-shell/issues/7485#issuecomment-728984689
# https://wiki.archlinux.org/title/Flatpak#Adding_Flatpak_.desktop_files_to_your_menu
set -l xdg_data_home $XDG_DATA_HOME ~/.local/share
set -gx --path XDG_DATA_DIRS $xdg_data_home[1]/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
for flatpakdir in ~/.local/share/flatpak/exports/bin /var/lib/flatpak/exports/bin
if test -d $flatpakdir
contains $flatpakdir $PATH; or set -a PATH $flatpakdir
end
end
end
if type -qf kitty
set -gx TERM xterm-kitty
end
if type -qf fnm
fnm env | source
end
set -gx CLICOLOR 1
set -gx LSCOLORS 'gxfxcxdxbxegedabaggxgx'
end
# Instead of tty check, we would have liked to do this using status is-login
# check but for some reason that does not work?
set --local TTY1 (tty)
[ "$TTY1" = "/dev/tty1" ] && __fish_setup_on_tty_login

View file

@ -1 +0,0 @@
starship init fish | source

View file

@ -1,8 +1,3 @@
# If running from tty1 start sway # If running from tty1 start sway
set TTY1 (tty) set --local TTY1 (tty)
if test -z "$DISPLAY"; and test $TTY1 = "/dev/tty1" [ "$TTY1" = "/dev/tty1" ] && exec sway
exec sway
end
set -e BROWSER
set -Ux BROWSER firefox-wayland

View file

@ -1,25 +1,9 @@
set PATH {$HOME}/.nix-profile/bin {$HOME}/.cargo/bin {$HOME}/.cabal/bin {$HOME}/.local/bin $PATH set -g fish_term24bit 1
set -gx TERM xterm-kitty set -g fish_color_command cyan
set -gx NIX_PATH {$HOME}/.nix-defexpr/channels set -g fish_color_end red
set -g fish_color_error red --bold
# https://github.com/fish-shell/fish-shell/issues/5593 set -g fish_color_escape yellow
bind \cd true set -g fish_color_operator blue
set -g fish_color_param normal
export MANPAGER='nvim +Man!' set -g fish_color_quote green
export NIX_SSL_CERT_FILE='/etc/ssl/certs/ca-certificates.crt' set -g fish_color_redirection red
export VISUAL="nvim"
export EDITOR="nvim"
fzf_key_bindings
# https://github.com/fish-shell/fish-shell/issues/7485#issuecomment-728984689
# https://wiki.archlinux.org/title/Flatpak#Adding_Flatpak_.desktop_files_to_your_menu
set -l xdg_data_home $XDG_DATA_HOME ~/.local/share
set -gx --path XDG_DATA_DIRS $xdg_data_home[1]/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
for flatpakdir in ~/.local/share/flatpak/exports/bin /var/lib/flatpak/exports/bin
if test -d $flatpakdir
contains $flatpakdir $PATH; or set -a PATH $flatpakdir
end
end

View file

@ -0,0 +1,2 @@
function fish_greeting
end

View file

@ -0,0 +1,7 @@
function fish_profile
if test -e /tmp/fish.profile
rm /tmp/fish.profile
end
fish --profile-startup /tmp/fish.profile -i -c exit
sort -nk2 /tmp/fish.profile
end

View file

@ -0,0 +1,78 @@
set --path -a NESTED_FISH_LABELS
function fish_prompt
set -l command_status $status
set -l question_mark_in_circle '?'\u20dd
set_color red; echo -n '# '
# Print nesting
if test (count $NESTED_FISH_LABELS) -gt 0
echo -ns '[' (set_color yellow) \
(string join (set_color red)/(set_color yellow) $NESTED_FISH_LABELS) \
(set_color red) '] '
end
# Print current location
if [ "$PWD" = "$HOME" ]
set_color green; echo -n '~'
else
set -l parent_dir (dirname "$PWD")
set_color blue
if [ "$parent_dir" = "$HOME" ]
echo -n '~'
else
echo -n (basename "$parent_dir")
end
echo -n ' → '
set_color green; echo -n (basename "$PWD")
end
echo -n ' '
# Show some pretty git information if we're in a git repo
set -l is_git_worktree (git rev-parse --is-inside-work-tree 2>/dev/null)
if test "$is_git_worktree" = true
if git rev-parse '@{u}' >/dev/null 2>&1
# If an upstream is set, check how far ahead/behind the branch is
set -l git_commits_ahead (git rev-list '@{u}..HEAD' | wc -l | awk '{print $1}')
set -l git_commits_behind (git rev-list 'HEAD..@{u}' | wc -l | awk '{print $1}')
if [ \( "$git_commits_ahead" -eq 0 \) -a \( "$git_commits_behind" -eq 0 \) ]
set_color blue; echo -n '⦿'
else
if [ "$git_commits_behind" -gt 0 ]
set_color red; echo -n "$git_commits_behind"
end
if [ "$git_commits_ahead" -gt 0 ]
set_color brred; echo -n "$git_commits_ahead"
end
end
else
# Otherwise, indicate that an upstream is unknown
set_color brred; echo -n "$question_mark_in_circle"
end
set -l git_branch (git branch 2>/dev/null | sed -n '/\* /s///p')
if test -n "$git_branch"
set_color blue; echo -n ' ['
# Color the branch name differently if the working tree is dirty
if [ (count (git status --porcelain)) -gt 0 ]
set_color brred
else
set_color yellow
end
echo -n "$git_branch"
set_color blue; echo -n '] '
end
end
if [ "$command_status" -eq 0 ]
set_color brcyan; echo -n 'λ: '
else
set_color red; echo -n 'λ! '
end
set_color normal
# Color the hostname in the prompt dynamically whenever connected through SSH
if set -q SSH_TTY
set -g fish_color_host brred
end
end

View file

@ -0,0 +1,5 @@
function fish_user_key_bindings
# https://github.com/fish-shell/fish-shell/issues/5593
bind \cd true
fzf_key_bindings
end

View file

@ -0,0 +1,3 @@
function fu
fnm use
end

View file

@ -0,0 +1,4 @@
# https://wiki.archlinux.org/title/fish#Make_su_launch_fish
function su
command su --shell=/usr/bin/fish $argv
end