Literate config for fish shell.

This commit is contained in:
Phil Bajsicki 2023-08-12 16:47:06 +02:00
parent a8345fba50
commit b9cd319de2

421
.config/fish/README.org Normal file
View file

@ -0,0 +1,421 @@
#+title: fish config
#+PROPERTY: header-args :tangle config.fish
* Intro
This is the fish config I use. It's largely a copy of [[https://gitlab.com/dwt1][Derek Taylor's]] [[https://gitlab.com/dwt1/dotfiles/-/blob/master/.config/fish/config.fish][fish config.]] I have some edits in, but it's nothing significant. The majority of the comments are Derek's.
* Path
First line removes the path; second line sets it. Without the first line, your path gets massive and fish becomes very slow.
#+begin_src fish
set -e fish_user_paths
set -U fish_user_paths $HOME/.local/bin $HOME/Applications $HOME/.cabal/bin $HOME/.ghcup/bin $fish_user_paths
#+end_src
* Exports
#+begin_src fish
set fish_greeting
set TERM "xterm-256color"
set EDITOR "emacsclient -t -a ''"
set VISUAL "emacsclient -c -a emacs"
set QT_QPA_PLATFORMTHEME "qt6ct"
#+end_src
* Manpager
There are several options:
- bat: ~set -x MANPAGER "sh -c 'col -bx | bat -l man -p'"~
- vim: ~set -x MANPAGER '/bin/bash -c "vim -MRn -c \"set buftype=nofile showtabline=0 ft=man ts=8 nomod nolist norelativenumber nonu noma\" -c \"normal L\" -c \"nmap q :qa<CR>\"</dev/tty <(col -b)"'~
- nvim: ~set -x MANPAGER "nvim -c 'set ft=man' -"~
#+begin_src fish
set -x MANPAGER "sh -c 'col -bx | bat -l man -p'"
#+end_src
* Text editing mode:
You can choose between:
- emacs mode: ~fish_default_key_bindings~
- vi mode: ~fish_vi_key_bindings~
#+begin_src fish
function fish_user_key_bindings
fish_default_key_bindings
end
#+end_src
* Autocomplete and highlight colors
#+begin_src fish
set fish_color_normal brcyan
set fish_color_autosuggestion '#7d7d7d'
set fish_color_command brcyan
set fish_color_error '#ff6c6b'
set fish_color_param brcyan
#+end_src
* Functions
** Spark
*** Definitions
#+begin_src fish
set -g spark_version 1.0.0
complete -xc spark -n __fish_use_subcommand -a --help -d "Show usage help"
complete -xc spark -n __fish_use_subcommand -a --version -d "$spark_version"
complete -xc spark -n __fish_use_subcommand -a --min -d "Minimum range value"
complete -xc spark -n __fish_use_subcommand -a --max -d "Maximum range value"
function spark -d "sparkline generator"
if isatty
switch "$argv"
case {,-}-v{ersion,}
echo "spark version $spark_version"
case {,-}-h{elp,}
echo "usage: spark [--min=<n> --max=<n>] <numbers...> Draw sparklines"
echo "examples:"
echo " spark 1 2 3 4"
echo " seq 100 | sort -R | spark"
echo " awk \\\$0=length spark.fish | spark"
case \*
echo $argv | spark $argv
end
return
end
command awk -v FS="[[:space:],]*" -v argv="$argv" '
BEGIN {
min = match(argv, /--min=[0-9]+/) ? substr(argv, RSTART + 6, RLENGTH - 6) + 0 : ""
max = match(argv, /--max=[0-9]+/) ? substr(argv, RSTART + 6, RLENGTH - 6) + 0 : ""
}
{
for (i = j = 1; i <= NF; i++) {
if ($i ~ /^--/) continue
if ($i !~ /^-?[0-9]/) data[count + j++] = ""
else {
v = data[count + j++] = int($i)
if (max == "" && min == "") max = min = v
if (max < v) max = v
if (min > v ) min = v
}
}
count += j - 1
}
END {
n = split(min == max && max ? "▅ ▅" : "▁ ▂ ▃ ▄ ▅ ▆ ▇ █", blocks, " ")
scale = (scale = int(256 * (max - min) / (n - 1))) ? scale : 1
for (i = 1; i <= count; i++)
out = out (data[i] == "" ? " " : blocks[idx = int(256 * (data[i] - min) / scale) + 1])
print out
}
'
end
#+end_src
*** Spark functions
#+begin_src fish
function letters
cat $argv | awk -vFS='' '{for(i=1;i<=NF;i++){ if($i~/[a-zA-Z]/) { w[tolower($i)]++} } }END{for(i in w) print i,w[i]}' | sort | cut -c 3- | spark | lolcat
printf '%s\n' 'abcdefghijklmnopqrstuvwxyz' ' ' | lolcat
end
function commits
git log --author="$argv" --format=format:%ad --date=short | uniq -c | awk '{print $1}' | spark | lolcat
end
#+end_src
** !! and !$
*** Functions for !! and !$
#+begin_src fish
function __history_previous_command
switch (commandline -t)
case "!"
commandline -t $history[1]; commandline -f repaint
case "*"
commandline -i !
end
end
function __history_previous_command_arguments
switch (commandline -t)
case "!"
commandline -t ""
commandline -f history-token-search-backward
case "*"
commandline -i '$'
end
end
#+end_src
*** Bindings for !! and !$
#+begin_src fish
if [ $fish_key_bindings = "fish_vi_key_bindings" ];
bind -Minsert ! __history_previous_command
bind -Minsert '$' __history_previous_command_arguments
else
bind ! __history_previous_command
bind '$' __history_previous_command_arguments
end
#+end_src
** Small functions
*** backup
Creates a $filename.bak copy of the argument file.
#+begin_src fish
function backup --argument filename
cp $filename $filename.bak
end
#+end_src
*** copy
Copy things, recursively. Like ~cp -r~.
#+begin_src fish
function copy
set count (count $argv | tr -d \n)
if test "$count" = 2; and test -d "$argv[1]"
set from (echo $argv[1] | trim-right /)
set to (echo $argv[2])
command cp -r $from $to
else
command cp $argv
end
end
#+end_src
*** coln
Print only one column of the output.
#+begin_src fish
function coln
while read -l input
echo $input | awk '{print $'$argv[1]'}'
end
end
#+end_src
*** rown
Print only one row of the output.
#+begin_src fish
function rown --argument index
sed -n "$index p"
end
#+end_src
*** skip
Skips ~n~ lines of the output.
#+begin_src fish
function skip --argument n
tail +(math 1 + $n)
end
#+end_src
*** take
Prints only the first ~n~ lines of the output.
#+begin_src fish
function take --argument number
head -$number
end
#+end_src
*** org-search
Perform a search through your ~org-agenda-files~ for a ~string~.
#+begin_src fish
function org-search -d "send a search string to org-mode"
set -l output (/usr/bin/emacsclient -a "" -e "(message \"%s\" (mapconcat #'substring-no-properties \
(mapcar #'org-link-display-format \
(org-ql-query \
:select #'org-get-heading \
:from (org-agenda-files) \
:where (org-ql--query-string-to-sexp \"$argv\"))) \
\"
\"))")
printf $output
end
#+end_src
* Aliases
** clear
Clears the terminal and prints some decorations.
#+begin_src fish
alias clear='/bin/clear; echo; echo; seq 1 (tput cols) | sort -R | spark | lolcat; echo; echo'
#+end_src
** doas
Sets root privileges for the command.
#+begin_src fish
alias doas="doas --"
#+end_src
** cd shorts
Helps navigate directory trees without typing lots of dots.
#+begin_src fish
alias ..='cd ..'
alias ...='cd ../..'
alias .3='cd ../../..'
alias .4='cd ../../../..'
alias .5='cd ../../../../..'
#+end_src
** vim
#+begin_src fish
alias vim='nvim'
#+end_src
** emacs
Aliases for doom emacs.
*** em
Opens emacs in the terminal.
#+begin_src fish
alias em='/usr/bin/emacs -nw'
#+end_src
*** emacs
Calls ~emacsclient -c -a 'emacs'~.
#+begin_src fish
alias emacs="emacsclient -c -a 'emacs'"
#+end_src
*** doom
These aliases call the ~doom~ script from ~~/.config/emacs/bin~.
#+begin_src fish
alias doomsync="~/.config/emacs/bin/doom sync"
alias doomdoctor="~/.config/emacs/bin/doom doctor"
alias doomupgrade="~/.config/emacs/bin/doom upgrade"
alias doompurge="~/.config/emacs/bin/doom purge"
#+end_src
** exa
These bindings change the way ~ls~ behaves by calling ~exa~ instead.
#+begin_src fish
alias ls='exa -al --color=always --group-directories-first'
alias la='exa -a --color=always --group-directories-first'
alias ll='exa -l --color=always --group-directories-first'
alias lt='exa -aT --color=always --group-directories-first'
alias l.='exa -a | egrep "^\."'
#+end_src
** pacman
Aliases for common ~pacman~ commands.
*** pacsyu
Updates only ~pacman~ (core Arch repo) packages.
#+begin_src fish
alias pacsyu='sudo pacman -Syyu'
#+end_src
*** parsua
Updates only AUR packages with ~paru~.
#+begin_src fish
alias parsua='paru -Sua --noconfirm'
#+end_src
*** parsyu
Updates everything with ~paru~. Note the ~--sudoloop~ argument - it allows for unattended upgrades. Won't halt the process if you go and get coffee.
#+begin_src fish
alias parsyu='paru -Syu --noconfirm --sudoloop'
#+end_src
*** unlock
Removes ~pacman~'s lockfile.
#+begin_src fish
alias unlock='sudo rm /var/lib/pacman/db.lck'
#+end_src
*** cleanup
Removes orphaned packages.
#+begin_src fish
alias cleanup='sudo pacman -Rns (pacman -Qtdq)'
#+end_src
*** mirror
Aliases for ~reflector~, finding the fastest mirrors to get packages from.
#+begin_src fish
alias mirror="sudo reflector -f 30 -l 30 --number 10 --verbose --save /etc/pacman.d/mirrorlist"
alias mirrord="sudo reflector --latest 50 --number 20 --sort delay --save /etc/pacman.d/mirrorlist"
alias mirrors="sudo reflector --latest 50 --number 20 --sort score --save /etc/pacman.d/mirrorlist"
alias mirrora="sudo reflector --latest 50 --number 20 --sort age --save /etc/pacman.d/mirrorlist"
#+end_src
** grep
Add colors to ~grep~ output.
#+begin_src fish
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
#+end_src
** cp, mv, rm
Ask for confirmation before overwriting files.
#+begin_src fish
alias cp="cp -i"
alias mv='mv -i'
alias rm='rm -i'
#+end_src
** df, free
Make the output human-readable by default.
#+begin_src fish
alias df='df -h'
alias free='free -m'
#+end_src
** ps
Some handy shortcuts for ~ps~.
#+begin_src fish
alias psa="ps auxf"
alias psgrep="ps aux | grep -v grep | grep -i -e VSZ -e"
alias psmem='ps auxf | sort -nr -k 4'
alias pscpu='ps auxf | sort -nr -k 3'
#+end_src
** merge
Merges ~~/.Xresources~.
#+begin_src fish
alias merge='xrdb -merge ~/.Xresources'
#+end_src
** git
#+begin_src fish
alias addup='git add -u'
alias addall='git add .'
alias branch='git branch'
alias checkout='git checkout'
alias clone='git clone'
alias commit='git commit -m'
alias fetch='git fetch'
alias pull='git pull origin'
alias push='git push origin'
alias tag='git tag'
alias newtag='git tag -a'
#+end_src
** jctl
Get error messages from ~journalctl~.
#+begin_src fish
alias jctl="journalctl -p 3 -xb"
#+end_src
** gpg
*** gpg-check
Verify signature.
#+begin_src fish
alias gpg-check="gpg2 --keyserver-options auto-key-retrieve --verify"
#+end_src
*** gpg-retrieve
Retrieve gpg keys.
#+begin_src fish
alias gpg-retrieve="gpg2 --keyserver-options auto-key-retrieve --receive-keys"
#+end_src
** yta
~youtube-dl~ shortcuts.
#+begin_src fish
alias yta-aac="youtube-dl --extract-audio --audio-format aac "
alias yta-best="youtube-dl --extract-audio --audio-format best "
alias yta-flac="youtube-dl --extract-audio --audio-format flac "
alias yta-m4a="youtube-dl --extract-audio --audio-format m4a "
alias yta-mp3="youtube-dl --extract-audio --audio-format mp3 "
alias yta-opus="youtube-dl --extract-audio --audio-format opus "
alias yta-vorbis="youtube-dl --extract-audio --audio-format vorbis "
alias yta-wav="youtube-dl --extract-audio --audio-format wav "
alias ytv-best="youtube-dl -f bestvideo+bestaudio "
#+end_src
** shell
Aliases to switch your login shell. Why would you do this... I can't fathom.
#+begin_src fish
alias tobash="sudo chsh $USER -s /bin/bash && echo 'Now log out.'"
alias tozsh="sudo chsh $USER -s /bin/zsh && echo 'Now log out.'"
alias tofish="sudo chsh $USER -s /bin/fish && echo 'Now log out.'"
#+end_src
** config
Bare git repo for dotfiles.
#+begin_src fish
alias config="/usr/bin/git --git-dir=$HOME/git/dot --work-tree=$HOME"
#+end_src
** tb
~termbin~ in your terminal.
#+begin_src fish
alias tb="nc termbin.com 9999"
#+end_src
** tips
I'm not sure what this does. Derek's comment is ~Unlock LBRY tips~, which honestly can mean anything. Leaving this in my config just in case it's important.
#+begin_src fish
alias tips="lbrynet txo spend --type=support --is_not_my_input --blocking"
#+end_src
* DTOS
These are things that I tend to keep in after first installing DTOS on top of Arch. I don't need much afterwards, so a lot of it is missing.
For the full deal, head to Derek's repo, where you can see all the aliases and settings.
** Pretty!
*** colorscript
You can get this script from [[gitlab.com/dwt1/shell-color-scripts][DT's git,]] or install them from AUR: ~shell-color-scripts~.
#+begin_src fish
colorscript random
#+end_src
*** starship prompt
#+begin_src fish
starship init fish | source
#+end_src
** Environment variables
#+begin_src fish
set -gx PATH "$HOME/.cabal/bin:$HOME/.ghcup/bin:$PATH"
#+end_src