From 976538a385af6df533b0a71b5aea6d4962dc8eb1 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Thu, 13 Oct 2016 14:39:04 +0530 Subject: [PATCH] fish: Init --- .config/fish/completions/fusermount.fish | 8 +++ .config/fish/config.fish | 57 ++++++++++++++++ .config/fish/functions/cd.fish | 36 ++++++++++ .config/fish/functions/describe.fish | 12 ++++ .config/fish/functions/fish_prompt.fish | 27 ++++++++ .../functions/fish_user_key_bindings.fish | 10 +++ .config/fish/functions/man.fish | 35 ++++++++++ .config/fish/functions/ranger.fish | 17 +++++ .config/fish/fzf.fish | 68 +++++++++++++++++++ .config/fish/pacman.fish | 63 +++++++++++++++++ 10 files changed, 333 insertions(+) create mode 100644 .config/fish/completions/fusermount.fish create mode 100644 .config/fish/config.fish create mode 100644 .config/fish/functions/cd.fish create mode 100644 .config/fish/functions/describe.fish create mode 100644 .config/fish/functions/fish_prompt.fish create mode 100644 .config/fish/functions/fish_user_key_bindings.fish create mode 100644 .config/fish/functions/man.fish create mode 100644 .config/fish/functions/ranger.fish create mode 100644 .config/fish/fzf.fish create mode 100644 .config/fish/pacman.fish diff --git a/.config/fish/completions/fusermount.fish b/.config/fish/completions/fusermount.fish new file mode 100644 index 00000000..a8ecac75 --- /dev/null +++ b/.config/fish/completions/fusermount.fish @@ -0,0 +1,8 @@ +## TODO: This fixes upstream bug, report! +complete -c fusermount --description "Mount point" -x -a '(__fish_print_mounted)' +complete -c fusermount -s h --description "Display help and exit" +complete -c fusermount -s v --description "Display version and exit" +complete -c fusermount -s o -x --description "Mount options" +complete -c fusermount -s u --description "Unmount" +complete -c fusermount -s q --description "Quiet" +complete -c fusermount -s z --description "Lazy unmount" diff --git a/.config/fish/config.fish b/.config/fish/config.fish new file mode 100644 index 00000000..ac10125d --- /dev/null +++ b/.config/fish/config.fish @@ -0,0 +1,57 @@ +## fish working paths +set -q XDG_DATA_HOME; or set -l XDG_DATA_HOME $HOME/.local/share +set -q XDG_CONFIG_HOME; or set -l XDG_CONFIG_HOME $HOME/.config +set -q fish_data_path; or set -g fish_data_path $XDG_DATA_HOME/fish +set -q fish_config_path; or set -g fish_config_path $XDG_CONFIG_HOME/fish + +## cdhist options +set -g fish_cdhist_path $fish_data_path/fish_cdhist +set -g fish_cdhist_max 100 + +## Go back to last cdhist folder. Run this before the SHELL_FILEBROWSER hook. +if grep -q . $fish_cdhist_path ^ /dev/null + set dirprev (cat $fish_cdhist_path) + set -q dirprev[$fish_cdhist_max]; and set dirprev $dirprev[-$fish_cdhist_max..-1] + cd $dirprev[(count $dirprev)] +end + +## Run the following +## sh -c 'export SHELL_FILEBROWSER=true; exec $TERMCMD' +## and this will autostart the file browser. +[ -n "$SHELL_FILEBROWSER" ]; and set -u SHELL_FILEBROWSER; and ranger + +## Start at a specific location. Useful when switching to a shell from a browser +## for instance. +[ -n "$SHELL_CD" ]; and cd $SHELL_CD; and set -u SHELL_CD + +## Misc +set fish_greeting + +## Colors +## For custom prompt. +set fish_color_user brcyan +set fish_color_hostname brcyan + +## Aliases +function ls --description 'List contents of directory' + set -l param --color=auto + if isatty 1 + set param $param --indicator-style=classify + end + if [ (uname -o) = "GNU/Linux" ] + set param $param --group-directories-first + end + command ls $param $argv +end +alias l 'ls -1' +alias la 'll -lAh' + +alias mkdir 'mkdir -p' +function mkcd -a folder -d 'Make directory and change into it.' + mkdir -p $folder + cd $folder +end + +if type -pq pacman + source $fish_config_path/pacman.fish +end diff --git a/.config/fish/functions/cd.fish b/.config/fish/functions/cd.fish new file mode 100644 index 00000000..0d000796 --- /dev/null +++ b/.config/fish/functions/cd.fish @@ -0,0 +1,36 @@ +# Wrap the builtin cd command to maintain a permanent directory history. +function cd --description "Change directory" + if test (count $argv) -gt 1 + printf "%s\n" (_ "Too many args for cd command") + return 1 + end + + # Skip history in subshells. + if status --is-command-substitution + builtin cd $argv + return $status + end + + if test "$argv" = "-" + set argv $dirprev[(math (count $dirprev) - 1)] + end + + builtin cd $argv + set -l cd_status $status + + if test $cd_status -eq 0 + if set -q dirprev + ## Remove duplicates. + set -g dirprev (string match -v $PWD $dirprev) $PWD + set -q dirprev[$fish_cdhist_max]; and set dirprev $dirprev[-$fish_cdhist_max..-1] + ## Save history. + string join \n $dirprev > $fish_cdhist_path + else + set -g dirprev $PWD + end + set -e dirnext + set -g __fish_cd_direction prev + end + + return $cd_status +end diff --git a/.config/fish/functions/describe.fish b/.config/fish/functions/describe.fish new file mode 100644 index 00000000..78c1839a --- /dev/null +++ b/.config/fish/functions/describe.fish @@ -0,0 +1,12 @@ +function describe -d 'Extend on "type" for executables' + for i in $argv + switch (type -t $i) + case file + set -l path (type -p $i) + ls -l $path + file $path | cut -d':' -f2 | cut -b 2- + case '*' + type $i + end + end +end diff --git a/.config/fish/functions/fish_prompt.fish b/.config/fish/functions/fish_prompt.fish new file mode 100644 index 00000000..e250bc92 --- /dev/null +++ b/.config/fish/functions/fish_prompt.fish @@ -0,0 +1,27 @@ +function fish_prompt + set HOST (hostname) + printf "%s%s%s%s%s" (set_color -o)'(' \ + (set_color normal) \ + (set_color $fish_color_user)$USER \ + (set_color normal) \ + (set_color -o)@ \ + (set_color normal) \ + (set_color $fish_color_hostname)$HOST \ + (set_color normal) \ + (set_color -o)'}' + + ## Path + set -l cwd_color $fish_color_cwd + if test (id -u) -eq 0 + set -l cwd_color $fish_color_cwd_root + end + set PROMPT_PWD (prompt_pwd) + printf "%s%s%s%s" \ + (set_color -o)'[' \ + (set_color -o $cwd_color)$PROMPT_PWD \ + (set_color normal) \ + (set_color -o)']' + + echo + echo '> ' +end diff --git a/.config/fish/functions/fish_user_key_bindings.fish b/.config/fish/functions/fish_user_key_bindings.fish new file mode 100644 index 00000000..56210a22 --- /dev/null +++ b/.config/fish/functions/fish_user_key_bindings.fish @@ -0,0 +1,10 @@ +function fish_user_key_bindings + ## fzf + if type -pq fzf + source $fish_config_path/fzf.fish + end + + ## Ranger + type -q ranger; and bind \e\cb ranger +end + diff --git a/.config/fish/functions/man.fish b/.config/fish/functions/man.fish new file mode 100644 index 00000000..46f433ef --- /dev/null +++ b/.config/fish/functions/man.fish @@ -0,0 +1,35 @@ +function man --description 'Color and justify the on-line manual pages' + ## Work around the "builtin" manpage that everything symlinks to, + ## by prepending our fish datadir to man. This also ensures that man gives fish's + ## man pages priority, without having to put fish's bin directories first in $PATH + + # Justify man pager to current window size. + set -q MANWIDTH; and [ $COLUMNS -lt $MANWIDTH ]; and set -lx MANWIDTH $COLUMNS + + ## Color when using LESS. + ## mb = ? + ## md = bold (titles, commands) + ## so = status bar + ## us = italic (arguments, files) + set -lx LESS_TERMCAP_mb (printf "\e[1;31m") + set -lx LESS_TERMCAP_md (printf "\e[0;36m") + set -lx LESS_TERMCAP_me (printf "\e[0m") + set -lx LESS_TERMCAP_se (printf "\e[0m") + set -lx LESS_TERMCAP_so (printf "\e[1;4;37m") + set -lx LESS_TERMCAP_ue (printf "\e[0m") + set -lx LESS_TERMCAP_us (printf "\e[0;33m") + + # Notice local but exported variable + set -lx MANPATH (string join : $MANPATH) + if test -z "$MANPATH" + type -q manpath + and set MANPATH (command manpath) + end + set -l fish_manpath (dirname $__fish_datadir)/fish/man + if test -d "$fish_manpath" -a -n "$MANPATH" + set MANPATH "$fish_manpath":$MANPATH + end + + # If fish's man pages could not be found, just invoke man normally + command man $argv +end diff --git a/.config/fish/functions/ranger.fish b/.config/fish/functions/ranger.fish new file mode 100644 index 00000000..4e69fd4a --- /dev/null +++ b/.config/fish/functions/ranger.fish @@ -0,0 +1,17 @@ +# Compatible with ranger >= 1.4.2. +# +# To undo the effect of this function, you can type "cd -" to return to the +# original directory. +function ranger -d 'Run ranger and sync folders with shell' + [ (count $argv) -eq 0 ]; and set argv . + set -l tempfile (mktemp) + command ranger --choosedir="$tempfile" $argv + if grep -q . "$tempfile" ^ /dev/null + set -l dest (cat -- "$tempfile") + ## TODO: Clear prompt so that shorter path does not print on top of longer + ## path. Could be a multi-line issue. Happens on every repaint, e.g. with + ## fzf widgets as well. + [ "$dest" != "$PWD" ]; and cd $dest; and commandline -f repaint + end + rm -f -- "$tempfile" +end diff --git a/.config/fish/fzf.fish b/.config/fish/fzf.fish new file mode 100644 index 00000000..bc6367c5 --- /dev/null +++ b/.config/fish/fzf.fish @@ -0,0 +1,68 @@ +fzf_key_bindings +bind \cT transpose-chars +bind \e\ct fzf-file-widget +bind \ec capitalize-word +bind \eC fzf-cd-widget + +function __fzf-selector -d 'fzf commandline and print selection back to commandline. Awesome!' + set -l cmd (commandline) + [ $cmd ]; or return + eval $cmd | eval (__fzfcmd) -m --tiebreak=index --toggle-sort=ctrl-r | string join ' ' | read -l result + [ "$result" ]; and commandline -- $result + commandline -f repaint +end +bind \e\cm __fzf-selector + +## DONE: Report missing (commandline) upstream. +## TODO: Report use of 'read'. +function fzf-history-widget + history | eval (__fzfcmd) +m --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS -q '(commandline)' | read -l result + and commandline -- $result + commandline -f repaint +end + +## Like original but uses last token as root for 'find'. +## If last token is a path, you can use it as $cwd in FZF_CTRL_T_COMMAND to +## restrict search to this path. $cwd will be suppressed from commandline to +## ensure clean output from the search results. +## TODO: Report upstream. Makes '**' obsolete for bash and zsh. +function fzf-file-widget + set -l cwd_esc (commandline -t) + ## The commandline token might be escaped, we need to unescape it. + set -l cwd (eval "echo $cwd_esc") + if [ ! -d $cwd ] + set cwd . + end + + set -q FZF_CTRL_T_COMMAND; or set -l FZF_CTRL_T_COMMAND " + command find -L \$cwd \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \ + -o -type f -print \ + -o -type d -print \ + -o -type l -print 2> /dev/null | sed 1d" + + if eval $FZF_CTRL_T_COMMAND | eval (__fzfcmd) -m $FZF_CTRL_T_OPTS > $TMPDIR/fzf.result + if [ $cwd != . ] + ## Remove path from commandline: it will be + set -l cmd (commandline) + set -l len (math (string length $cmd) - (string length $cwd_esc)) + commandline -- (string sub -l $len (commandline)) + end + for i in (seq 20); commandline -i (cat $TMPDIR/fzf.result | __fzf_escape) 2> /dev/null; and break; sleep 0.1; end + end + commandline -f repaint + rm -f $TMPDIR/fzf.result +end + +function fzf-bcd-widget -d 'cd backwards' + pwd | nawk -v RS=/ '/\n/ {exit} {p=p $0 "/"; print p}' | eval (__fzfcmd) +m --tac | read -l result + [ $result ]; and cd $result + commandline -f repaint +end +bind \e\cL fzf-bcd-widget + +function fzf-cdhist-widget -d 'cd to one of the previously visited location' + string join \n $dirprev | eval (__fzfcmd) +m | read -l result + [ $result ]; and cd $result + commandline -f repaint +end +bind \er fzf-cdhist-widget diff --git a/.config/fish/pacman.fish b/.config/fish/pacman.fish new file mode 100644 index 00000000..47dc27a1 --- /dev/null +++ b/.config/fish/pacman.fish @@ -0,0 +1,63 @@ +## TODO: Remove unnecessary aliases. + +set -l listinstalled "(pacman -Q | string replace ' ' \t)" +set -l listall "(__fish_print_packages)" + +function pacfiles -d 'List of files in pacman package packages sorted by size' + pacman -Qlq $argv | grep -v '/$' | xargs du -cbh | sort -h +end +complete -c pacfiles -a "$listinstalled" + +function pel + set -l result + pacman -Qlq $argv | grep -v '/$' | eval (__fzfcmd) -m --tiebreak=index --toggle-sort=ctrl-r | while read -l r; set result $result $r; end + [ "$result" ]; and eval $EDITOR $result +end +complete -c pel -a "$listinstalled" + +function pi + sudo pacman -S --needed $argv +end +complete -c pi -a "$listall" + +function pli -a size -d 'Pacman last installed packages' + [ $size ]; or set size 30 + expac -t '%F %T' '%-8l %n' | sort -rn | head -$size +end + +function pqi + pacman -Qi $argv +end +complete -c pqi -a "$listinstalled" + +function pql -d 'Get detailed file list (no folders) for the specified packages' + pacman -Ql $argv | grep -v '/$' +end +complete -c pql -a "$listinstalled" + +function pqo + pacman -Qo $argv +end +complete -c pqo -a '(__fish_complete_command)' + +function pqs + pacman -Qs $argv +end + +function pr + sudo pacman -Rs $argv +end +complete -c pr -a "$listinstalled" + +function psi + pacman -Si $argv +end +complete -c psi -a "$listall" + +function pss + pacman -Ss $argv +end + +function pu -d 'Run pacman system update' + yes | sudo pacman -Sc; and sudo pacman -Syu +end