zsh/zshrc

2573 lines
82 KiB
Bash
Raw Normal View History

2014-08-17 22:54:05 +02:00
# /etc/zsh/zshrc ou ~/.zshrc
# Fichier de configuration principal de zsh, lu au lancement des shells interactifs
# (et non des shells d'interprétation de fichier)
# Formation Debian GNU/Linux par Alexis de Lattre
# http://formation-debian.via.ecp.fr/
################
# 1. Les alias #
################
# Gestion du 'ls' : couleur & ne touche pas aux accents
alias ls='ls --classify --tabsize=0 --literal --color=auto --show-control-chars --human-readable'
# Copie en récursif et qui garde la date des fichiers copiés
#alias cp='cp -Rp'
2015-02-26 14:20:54 +01:00
alias cp='cp -R' # **cp** copy files and directories recursively # TEST
2014-08-17 22:54:05 +02:00
# Copie distante, ajout de la récursivité tout le temps:
alias scp='scp -r'
# Demande confirmation avant d'écraser un fichier
alias cP='cp --interactive'
alias mv='mv --interactive'
alias rm='rm --interactive'
2022-03-08 09:56:33 +01:00
# ls shortcut {{{
2023-09-16 07:33:24 +02:00
if [ ! $(command -v eza) ]; then
2022-03-29 18:00:16 +02:00
## with ls {{{
2017-08-04 16:37:24 +02:00
alias ll='ls -l'
2022-01-12 11:46:40 +01:00
## Show hidden files
2023-07-21 09:55:15 +02:00
alias la='ll --almost-all'
2022-07-11 12:16:14 +02:00
## Show hidden only
alias lla='la --directory .*'
2023-07-21 09:55:15 +02:00
alias l.='ls --directory .*'
alias llh='ll | head'
alias llp='ll | $PAGER'
alias llw='ll | wc --lines'
2022-01-12 11:46:40 +01:00
## Sort by date
2023-07-21 09:55:15 +02:00
alias lll='ls -l -t --human-readable --reverse'
alias llll='ls -l -t --human-readable --reverse'
alias lld='ls -l --directory */ --human-readable'
alias lr='ls --recursive | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\'''
2022-03-29 18:00:16 +02:00
## }}}
2023-09-16 07:33:24 +02:00
## with eza (forked from exa) {{{
2017-08-04 16:37:24 +02:00
else
2023-09-16 07:33:24 +02:00
alias exa=eza
alias ll='eza --color=always --long --group --git'
2022-01-12 11:46:40 +01:00
## Show hidden files
2022-07-11 12:16:14 +02:00
alias la='ll --all --sort .name'
## Show hidden only
alias lla='ll --list-dirs .*'
2023-07-21 09:55:15 +02:00
alias l.='ls --directory .*'
2022-01-12 11:46:40 +01:00
alias llh='ll | head'
2023-07-21 09:55:15 +02:00
alias llp='ll | $PAGER'
alias llw='ll | wc --lines'
2022-01-12 11:46:40 +01:00
## Sort by date
alias lll='ll --sort=modified'
alias llll='ll --sort=modified'
## Give a tree of current directory
alias llt='ll --tree'
alias lld='ll --group-directories-first'
2023-07-21 09:55:15 +02:00
alias lr='ls --recursive | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\'''
2022-03-29 18:00:16 +02:00
## }}}
2023-02-21 16:11:56 +01:00
fi
2022-03-08 09:56:33 +01:00
# }}}
2014-08-17 22:54:05 +02:00
2021-08-09 10:01:46 +02:00
# cat with number on output lines
2023-07-21 09:55:15 +02:00
alias ca='cat --number'
2021-08-09 10:01:46 +02:00
# docker related
alias d='docker'
2021-08-09 10:01:46 +02:00
# Correct ip command
alias ipa='ip a'
# Disable because of "There is no rawcontrolchars option" warning…
2021-08-09 10:01:46 +02:00
# less with raw character
#alias less='less --rawcontrolchars'
2021-08-09 10:01:46 +02:00
#alias less='less --quiet'
2023-07-17 08:32:05 +02:00
# Call last command
alias pp='fc -e -'
# Call last command in edit mode
alias ppe='fc'
# Call last command with sudo
alias pps='sudo $(fc -ln -1)'
2023-02-21 16:12:15 +01:00
# fdfind
## disregard vcs ignore files (.gitignore…) as homedir is a git repository…
alias fd='fd --no-ignore-vcs'
2018-04-19 18:59:24 +02:00
# sudo
## Please consider using "Defaults env_keep+=HOME" configuration in sudoers {{{
### This is Ubuntu default behaviour.
### This will allow to share user's homedir/dotfiles with root throught sudo commands
## }}}
## Ensure sudo can use aliases (end whitespace) {{{
### `man zshall` partie Aliasing: "If the text ends with a space, the next word in the shell input is treated as though it were in command position for
### purposes of alias expansion. "
alias s='sudo '
2018-04-19 18:59:24 +02:00
## }}}
## sudo aliases {{{
2014-08-17 22:54:05 +02:00
if [ ${USER} != "root" ]; then
2017-08-04 09:17:58 +02:00
alias sc='sudo systemctl '
2020-12-21 15:39:10 +01:00
alias scu='systemctl --user'
2017-08-04 09:11:30 +02:00
alias sd='sudo docker'
alias si='sudo iptables -L -vn '
2017-08-04 09:17:58 +02:00
alias sj='sudo journalctl '
2017-08-04 09:50:06 +02:00
alias sn='sudo nft list ruleset'
2017-12-20 08:39:22 +01:00
alias sv='sudo vi '
alias sz='sudo zsh'
2014-08-17 22:54:05 +02:00
else
2017-08-04 09:17:58 +02:00
alias sc='systemctl '
2021-08-09 10:01:46 +02:00
alias scu='systemctl --user'
2017-08-04 09:11:30 +02:00
alias sd='docker'
alias si='iptables -L -vn '
2017-08-04 09:17:58 +02:00
alias sj='journalctl '
2017-08-11 12:08:29 +02:00
alias sn='nft list ruleset'
alias vi='vi -S ~/.vim/vimrc '
alias sv='vi -S ~/.vim/vimrc '
2014-08-17 22:54:05 +02:00
fi
2018-04-19 18:59:24 +02:00
## }}}
2014-08-17 22:54:05 +02:00
# Envoyer une notification via libnotify-bin (fin d'une commande, ...)
# Nécessite le paquet libnotify-bin
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Nettoyage de l'écran
alias c='clear'
# Liste le contenu du dossier
alias dir='dir --color=always'
# Page de manuel en anglais
alias mane="LANG=C man "
# Afficher les lignes trop longues sur la ligne suivante plutot que de remplacer par un "$":
alias most='most -w'
2023-09-29 21:38:49 +02:00
## System
# APT|Aptitude|dpkg|… commands {{{
2014-08-17 22:54:05 +02:00
if [ -d /etc/apt ]; then
alias acle='sudo aptitude clean'
2022-02-18 15:36:12 +01:00
alias afil='apt-file search --'
alias aful='sudo aptitude full-upgrade'
alias ains='sudo aptitude install'
2021-05-27 07:08:31 +02:00
alias alis='apt list --upgradable'
2015-10-20 09:21:07 +02:00
alias apol='apt-cache policy'
2015-09-07 13:40:24 +02:00
alias apur="sudo aptitude purge '~c'"
2015-11-11 21:55:17 +01:00
alias aupd='sudo apt update'
alias aupg='sudo aptitude upgrade'
alias arem='sudo aptitude remove'
alias apts='apt search'
2022-02-18 15:36:12 +01:00
alias asea='aptitude search --'
alias asho='aptitude show'
alias aver='apt-show-versions'
2015-10-20 09:21:07 +02:00
alias awhy='aptitude why'
2023-07-21 09:55:15 +02:00
alias insexperimental='sudo aptitude --target-release experimental install'
alias anorepos="apt list --installed | sed --silent 's/\(.*\)\/.*,local.*/\1/p' | tr '\n' ' '"
alias gdpkg='dpkg --list | grep --extended-regexp -- '
2014-08-17 22:54:05 +02:00
fi
# }}}
2023-08-30 19:00:24 +02:00
# Pacman commands {{{
if [ -d /etc/pacman.d ]; then
alias pacle='echo "$(ls -1 /var/cache/pacman/pkg | wc --lines) packages to clean" && sudo pacman --sync --clean'
alias pafil='pacman --files --regex --'
alias paful='sudo pacman --sync --sysupgrade --'
alias pains='sudo pacman --sync --'
alias palis='pacman --query --upgrades --'
alias papol='pacman --sync --info --'
alias paupd='sudo pacman --sync --refresh --'
alias paupg='sudo pacman --sync --'
alias parem='sudo pacman --remove --'
alias paremdep='sudo pacman --remove --recursive --'
alias paorph='pacman --query --unrequired --deps --quiet --'
alias papurge='pacman --query --unrequired --deps --quiet -- | sudo pacman --remove --recursive --nosave -- -'
#alias apts='apt search'
alias pasea='pacman --sync --search --'
alias pasho='pacman --sync --info --'
#alias awhy='aptitude why'
#alias anorepos="apt list --installed | sed --silent 's/\(.*\)\/.*,local.*/\1/p' | tr '\n' ' '"
alias gpackage='pacman --query --search -- | grep --extended-regexp -- '
2023-08-30 19:00:24 +02:00
fi
# }}}
2023-09-01 19:04:14 +02:00
# Yay commands for AUR Archlinux User Repository {{{
if command -v yay > /dev/null; then
alias yains='yay --sync --'
alias yalis='yay --query --upgrades --'
alias yapol='yay --sync --info --'
#alias apts='apt search'
alias yasea='yay --sync --search --'
alias yasho='yay --sync --info --'
#alias awhy='aptitude why'
#alias anorepos="apt list --installed | sed --silent 's/\(.*\)\/.*,local.*/\1/p' | tr '\n' ' '"
fi
# }}}
2014-08-17 22:54:05 +02:00
2023-09-29 21:38:49 +02:00
# Filter pending mail by date
alias maildate='sudo mailq |grep "^[A-F0-9]" |sort -k5n -k6n'
# Grep aliases {{{
2023-07-21 09:55:15 +02:00
alias grep='grep --color=always --ignore-case '
alias gerp='grep --ignore-case'
2017-02-27 10:03:04 +01:00
alias Grep='\grep '
2023-07-21 09:55:15 +02:00
alias gmount='mount | grep --extended-regexp -- '
alias gdf='df | grep --extended-regexp -- '
# }}}
2016-06-06 15:52:00 +02:00
2014-08-17 22:54:05 +02:00
# Espace disque
2023-07-21 09:55:15 +02:00
alias df='df --block-size=1K --print-type --human-readable'
alias dus='du --total --human-readable | sort --human-numeric-sort'
alias dua='du --all --total --human-readable | sort --human-numeric-sort'
2021-08-31 14:41:36 +02:00
alias ncdu='gdu'
2014-08-17 22:54:05 +02:00
2022-03-08 09:56:33 +01:00
# Gestion des processus:
2014-08-17 22:54:05 +02:00
# Si htop n'est pas installé sur la machine:
if [ ! -f "`which htop`" ]; then
alias htop=top
fi
# Qu'est-ce qui consomme de la mémoire vive sur le système
2023-07-21 09:55:15 +02:00
alias wotgobblemem='ps -o time,ppid,pid,nice,pcpu,pmem,user,comm -A | sort --numeric-sort --key=6 | tail -15'
2014-08-17 22:54:05 +02:00
2022-11-28 08:27:17 +01:00
# ps aliases
2023-07-21 14:08:43 +02:00
alias px='ps faux|grep --invert-match -- grep|grep --extended-regexp --ignore-case --regexp=VSZ -e '
2023-05-09 15:59:09 +02:00
## ps with fzf
2022-11-28 08:27:17 +01:00
alias fpx="ps -ef | fzf --bind 'ctrl-r:reload(ps -ef)' --header 'Press CTRL-R to reload' --header-lines=1 --color fg:188,bg:233,hl:103,fg+:222,bg+:234,hl+:104"
2023-05-09 15:59:09 +02:00
alias pxf="ps -ef | fzf --bind 'ctrl-r:reload(ps -ef)' --header 'Press CTRL-R to reload' --header-lines=1 --color fg:188,bg:233,hl:103,fg+:222,bg+:234,hl+:104"
## ps last entries with watch|tail|grep
alias wps="watch \"ps faux | tail --lines=30 -- | grep --invert-match --extended-regexp '(tail|ps faux|grep)' --\""
alias psw="watch \"ps faux | tail --lines=30 -- | grep --invert-match --extended-regexp '(tail|ps faux|grep)' --\""
2014-08-17 22:54:05 +02:00
# Décompression
2023-07-21 09:55:15 +02:00
alias untargz='tar --gzip --extract --verbose -f'
alias untarbz2='tar --bzip2 --extract --verbose -f'
2014-08-17 22:54:05 +02:00
# Terminal multiplexer
alias ta='tmux a || tmux'
alias td='tmux detach'
2014-08-17 22:54:05 +02:00
# Send the content of a file to a privatebin
# Needs cpaste or any other Third party clients for PrivateBin
# https://github.com/PrivateBin/PrivateBin/wiki/Third-party-clients
alias pbin='~/repos/cpaste/cpaste --sourcecode --expire 1week --file '
## Git aliases Try to prefix with 'gg' {{{
2019-06-19 23:31:27 +02:00
### Get status of the repo
alias ggstatus='git status'
2022-11-07 12:05:26 +01:00
### Show differences between changes and index and ensure to display colors even with a pipe
alias ggdiff='git diff --color=always'
### Show differences between changes and index WITHOUT any colors
alias ggnocolordiff='git diff --color=never'
2019-06-19 23:31:27 +02:00
### Show only words/characters differences
alias ggwdiff='git diff --color-words=. --patience'
### Add changes to index
alias ggadd='git add'
2021-10-03 11:15:25 +02:00
### Restore changes to be committed
alias ggrestore='git restore --staged'
2019-06-19 23:31:27 +02:00
### Remove files from the index
alias ggrm='git rm'
### Move or rename file
alias ggmv='git mv'
### Cancel changes
alias ggcheckout='git checkout --'
### Print lines matching a pattern
2023-07-21 09:55:15 +02:00
alias ggrep='git grep --color --line-number --perl-regexp'
alias gggrep='git grep --color --line-number --perl-regexp'
2019-06-19 23:31:27 +02:00
### Show commit logs
alias gglog="git log --graph --full-history --all --color --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
### Show last 20 commits with graph
alias gglg="git --no-pager log --graph --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ar)%Creset' -20"
### Show all commits with graph
alias ggllg="git log --graph --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ar)%Creset'"
### Display all commits/changes of a file
2023-07-21 09:55:15 +02:00
alias ggchanges="git log --follow --perl-regexp --"
### Commands that might require a valid gpg-agent
#### Download any new commits
alias ggpull='load-gpg-agent && git pull'
#### Commit changes
alias ggco='load-gpg-agent && git commit'
alias ggcommit='load-gpg-agent && git commit'
#### Amend changes
alias ggamend='load-gpg-agent && git commit --amend'
#### Update remote repository
alias ggpush='load-gpg-agent && git push'
2019-06-19 23:31:27 +02:00
## }}}
2014-08-17 22:54:05 +02:00
# Lister les fichiers de configuration inutiles
2023-07-21 09:55:15 +02:00
alias purge='grep-status --no-field-names --show-field=Package --field=Status config-files'
2014-08-17 22:54:05 +02:00
## Piped alias
2014-08-17 22:54:05 +02:00
alias -g H='| head'
2023-07-21 09:55:15 +02:00
alias -g T='tail --follow'
alias -g G='| grep --invert-match grep -- | grep --extended-regexp'
2014-08-17 22:54:05 +02:00
alias -g L='| less'
alias -g M="| most"
2015-08-08 22:46:53 +02:00
alias -g S="| sort"
alias -g V="| vimmanpager"
2023-07-21 09:55:15 +02:00
alias -g W="| wc --lines"
alias -g X="| xclip -selection clipboard"
alias -g TM="| tmux load-buffer -- -"
2014-08-17 22:54:05 +02:00
alias -g B="&|"
alias -g HL="--help"
2023-07-21 09:55:15 +02:00
alias -g MM="2>&1 | most"
2014-08-17 22:54:05 +02:00
alias -g LL="2>&1 | less"
2023-07-21 09:55:15 +02:00
alias -g CA="2>&1 | cat --show-all"
2014-08-17 22:54:05 +02:00
alias -g NE="2> /dev/null"
alias -g NUL="> /dev/null 2>&1"
## Affichage de l'historique
if [ "$PAGER" = "most" ]; then
# En commencant par la fin avec most (bidouillage, on est pas sensé avoir):
2014-08-17 22:54:05 +02:00
alias hist="fc -El 0 | most +$HISTSIZE"
# Une fois dans un fichier avec most, la touche 'B' permet d'aller à la fin du fichier
else
# En commencant par la fin avec less:
alias hist="fc -El 0 | $PAGER +G"
2014-08-17 22:54:05 +02:00
#alias hist="less +G ~/.zsh/history"
fi
alias ghist='fc -El 0 | grep --'
2014-08-17 22:54:05 +02:00
## Gestion des répertoires
alias u='cd ..'
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
# Revenir dans le dossier dans lequel on se trouvait précédemment
alias cd-='cd -'
2022-10-30 10:47:02 +01:00
alias uu='cd -'
2023-07-26 09:52:56 +02:00
alias uuu='cd -2'
2014-08-17 22:54:05 +02:00
# Afficher la pile des dossiers:
alias dirs='dirs -v'
# Créer les répertoires précédents si ils n'existent pas
2023-07-21 09:55:15 +02:00
alias mkdir='mkdir --parents'
2014-08-17 22:54:05 +02:00
# Affiche l'arborescence du répertoire courant
#alias tree="find . | sed 's/[^/]*\//| /g;s/| *\([^| ]\)/+--- \1/'"
#La commande tree "basique" fait ça très bien ...
# Affiche la variable $PATH ligne par ligne
2015-10-20 09:21:07 +02:00
alias path='printf %s $PATH | tr ":" "\n"'
2014-08-17 22:54:05 +02:00
# Edite le dernier fichier d'un dossier
## Fonctionement:
## (. correspond au fichier
## om les classent par date de modification
## [1] choisit le premier
##  désactive les options GLOB_DOTS
alias vil='vi *(.om[1]^D)'
# Lancer vi pour qu'il ne conserve aucune trace du fichier
## Faire précéder la commande d'un espace empêche l'enregistrement dans l'historique du shell
alias vnb='vi -n "+set noundofile" "+set nobackup"'
2022-03-08 09:56:33 +01:00
# Différence entre deux fichiers
2014-08-17 22:54:05 +02:00
alias diff='colordiff -u'
alias diffs='\diff --side-by-side'
2014-08-17 22:54:05 +02:00
# Recherche toutes les occurences de l'arguments passé en paramètre dans l'historique des commandes
alias param='fc -l 0 | grep --'
2014-08-17 22:54:05 +02:00
# Multimédia
##Extraire la piste audio d'un .avi
alias avi2wav='mplayer -vc dummy -vo null -ao pcm:file=video.wav'
##Modifie la bar de progression du gestionnaire de téléchargement Axel
alias axel='axel -a --num-connection=20'
2022-12-01 10:31:14 +01:00
## List video file's subtitles
alias ff.list.subs='ffprobe -loglevel error -select_streams s -show_entries stream=index:stream_tags=language -of csv=p=0'
## List video file's audio stream
alias ff.list.audio='ffprobe -loglevel error -select_streams a -show_entries stream=index:stream_tags=language -of csv=p=0'
2014-08-17 22:54:05 +02:00
# Calculatrice en ligne de commande
alias caltos='bc'
#alias df='df --human-readable'
#alias du='du --human-readable'
2024-03-26 11:15:00 +01:00
alias mutt='neomutt'
alias m='neomutt'
2014-08-17 22:54:05 +02:00
alias md='mkdir'
alias rd='rmdir'
#Internet
2023-01-27 10:49:23 +01:00
## Minimal webserver from current directory and available at http://IP.AD.RE.SS:8002
## See : https://docs.python.org/3/library/http.server.html
2023-08-30 17:39:46 +02:00
alias httpserv="python3 -m http.server -b $(ip route get 1 | awk '{print $(NF-2);exit}') 8002"
## Minimal webserver with upload option and storage in dedicated directory
2023-08-30 17:39:46 +02:00
alias httpup="mkdir --parent -- /tmp/http.upload.directory && cd -- /tmp/http.upload.directory && python3 ${HOME}/bin/upload-http-server.py --listen $(ip route get 1 | awk '{print $(NF-2);exit}') --port 8004"
2014-08-17 22:54:05 +02:00
2022-03-08 09:56:33 +01:00
## Limite l'envoi à 3 requêtes pour ping
2014-08-17 22:54:05 +02:00
alias ping="ping -c 3"
2015-06-08 14:26:45 +02:00
alias ping6="ping6 -c 3"
2014-08-17 22:54:05 +02:00
## JOSM
alias josm="java -jar -Xmx2048M /opt/josm-tested.jar"
2022-03-08 09:56:33 +01:00
# Khard
alias kemail='khard email'
alias kmail='khard email'
alias kphone='khard phone'
alias ktel='khard phone'
# SSH {{{
#########
# Load ssh-agent with a fix socket path {{{
## This function can be used :
## 1. in zlogin (for a new shell)
## 2. with recossh alias to load a new ssh-agent
function load-ssh-agent() {
2021-09-27 16:50:53 +02:00
# If a ssh-key is available
2023-07-21 09:55:15 +02:00
if find "${HOME}/.ssh" -maxdepth 1 -type f -iname "id_*" | grep --quiet -- .; then
2021-09-27 16:50:53 +02:00
## If ssh-agent is not already launched
2023-07-21 09:55:15 +02:00
if ! ps -x | grep --invert-match -- grep | grep --fixed-strings --quiet -- "ssh-agent -a ${SSH_AGENT_SOCK}"; then
2021-09-27 16:50:53 +02:00
### Remove any previous socket and environment files
rm --force -- "${SSH_AGENT_SOCK}" "${SSH_AGENT_ENV}"
### Start ssh-agent with a specified socket path
### AND store informations in a file
ssh-agent -a "${SSH_AGENT_SOCK}" > "${SSH_AGENT_ENV}"
fi
## Load content of ssh-agent environment file
source "${SSH_AGENT_ENV}"
fi
}
# }}}
# Add ed25519 ssh-key to ssh-agent {{{
function load-ssh-ed25519() {
# If a ED25519 ssh-key is available
# AND not already loaded in ssh-agent
if [ -f "${SSH_ED25519_KEY}" ] &&
2023-07-21 09:55:15 +02:00
! ssh-add -l | grep --quiet --ignore-case -- "(ed25519)"; then
ssh-add "${SSH_ED25519_KEY}"
fi
}
# }}}
# Add rsa ssh-key to ssh-agent {{{
function load-ssh-rsa() {
# If a RSA ssh-key is available
# AND not already loaded in ssh-agent
if [ -f "${SSH_RSA_KEY}" ] &&
2023-07-21 09:55:15 +02:00
! ssh-add -l | grep --quiet --ignore-case -- "(rsa)"; then
ssh-add "${SSH_RSA_KEY}"
fi
}
# }}}
# Clear old entries in known_hosts {{{
function clearsshkey() {
sed -i "${1}d" ~/.ssh/known_hosts
}
# }}}
# recossh will:
# (re)load ssh-agent informations
# run the previous ssh command
alias recossh='load-ssh-agent &&
$(fc -lr 4900 | \grep --max-count=1 --extended-regexp "[0-9]{1,4} ssh " | cut --delimiter=" " --fields=4-)'
# For development and tests
alias sshdev='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=accept-new'
2023-08-31 20:34:02 +02:00
alias sshdev-copy-id='ssh-copy-id -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=accept-new'
alias scpdev='scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=accept-new'
# }}}
2021-09-19 23:21:52 +02:00
# GPG {{{
#########
# (re)Load gpg-agent {{{
2021-09-19 23:21:52 +02:00
## This function can be used :
## 1. with aliases (git commit,…)
function load-gpg-agent() {
## If a gpg key is available
if [ -f "${GPG_PRIV_KEY}" ]; then
## Remove any previous test file
rm --force -- "${GPG_TEST_FILE}"
## Run a gpg command on the test file
gpg --quiet --for-your-eyes-only --decrypt "${GPG_TEST_FILE}.gpg" > /dev/null
fi
}
2021-09-19 23:21:52 +02:00
# }}}
2021-09-19 23:21:52 +02:00
# }}}
2017-06-08 15:59:32 +02:00
# Taskwarrior {{{
2018-04-13 22:35:24 +02:00
## Aliases
alias t="task"
### Most recent tasks first
alias tarecent="task simple limit:page"
2022-02-24 11:44:28 +01:00
alias taold="task oldest limit:page"
### All pending tasks
alias tapending="task rc._forcecolor:on +PENDING all | less"
2021-10-08 13:56:28 +02:00
### Filter by tag
alias tawork="task rc._forcecolor:on +work | head"
alias taperso="task rc._forcecolor:on +perso | head"
alias tarm=tadel
2021-10-08 13:56:28 +02:00
## Functions
2022-03-02 09:18:45 +01:00
## search in pending tasks by task's name (case INsensitive) {{{
2018-03-07 16:21:39 +01:00
function tase() {
2021-10-07 17:52:20 +02:00
local search=""
for i; do; search="$search /$i/"; done;
2022-03-02 09:18:45 +01:00
task rc.search.case.sensitive:no +PENDING $search all || return 0
}
## }}}
2022-03-02 09:18:45 +01:00
## search in pending tasks by task's name (case SENSITIVE) {{{
function tasec() {
2021-10-07 17:52:20 +02:00
local search=""
2018-03-07 16:21:39 +01:00
for i; do; search="$search /$i/"; done;
2022-03-02 09:18:45 +01:00
task $search +PENDING all || return 0
}
## }}}
## search in all tasks by task's name (case INsensitive) {{{
function tall() {
local search=""
for i; do; search="$search /$i/"; done;
task rc.search.case.sensitive:no $search all || return 0
}
## }}}
2021-10-11 09:16:08 +02:00
## Delete a task {{{
function tadel() {
2023-02-21 14:46:51 +01:00
local tadel_task_id
local tadel_task_desc
local tadel_confirmation
2021-10-11 09:16:08 +02:00
# Verify argument
if [ "${#}" -eq 1 ]; then
## Get the first arg as task ID
tadel_task_id="${1}"
else
## If no argument or more than one
## Ask the user to choose an ID in the 20 more recents tasks {{{
task newest limit:20 || return 0
printf '%b' "Enter the ${MAGENTAB}ID${RESET} of the task to remove : "
read -r tadel_task_id
## }}}
fi
# If no task with this ID exists, exit function {{{
if ! task "${tadel_task_id}" | grep --quiet -- "^ID *${tadel_task_id}"; then
printf '%b' "No available task with ${REDB}${tadel_task_id}${RESET} ID."
return 1
fi
# }}}
# Get task's description
2023-07-21 09:55:15 +02:00
tadel_task_desc=$(task "${tadel_task_id}" | sed --quiet "s/^Description *\(.*\)/\1/p")
2021-10-11 09:16:08 +02:00
printf '%b' "Delete the task \"${MAGENTAB}${tadel_task_id} ${tadel_task_desc}${RESET}\" [Y/n] ? "
read -r tadel_confirmation
# Check confirmation
if printf -- '%s' "${tadel_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
task rc.confirmation:no "${tadel_task_id}" delete
printf '%b' "\"${MAGENTAB}${tadel_task_id} ${tadel_task_desc}${RESET}\" task was removed.\n"
2021-10-11 09:16:08 +02:00
fi
# Also offer to purge this task
tapurge "${tadel_task_desc}"
2021-10-11 09:16:08 +02:00
}
## }}}
## Mark a task as done {{{
2021-11-26 15:57:01 +01:00
function tadone() {
2023-02-21 14:46:51 +01:00
local tadone_task_regexp
local tadone_task_id
local tadone_task_desc
local tadone_confirmation
2021-11-26 15:57:01 +01:00
# Verify argument
if [ "${#}" -eq 0 ]; then
## If no argument
## Ask the user to choose a task description from the last week Timewarrior activities {{{
timew summary :week :ids || return 0
printf '%b' "Enter the ${MAGENTAB}a pattern${RESET} matching the expected task to mark as complete : "
read -r tadone_task_regexp
else
## Merge all args into one var
tadone_task_regexp="${*}"
## }}}
fi
#printf '%b' "Pattern to search a task is ${REDB}${tadone_task_regexp}${RESET}.\n"
2023-02-21 14:46:51 +01:00
tadone_task_id=$(task "${tadone_task_regexp}" simpleid | grep --after-context=2 -- ID | tail --lines=1 || return 0)
2021-11-26 15:57:01 +01:00
# If no task with this ID exists, exit function {{{
if [ -z "${tadone_task_id}" ]; then
printf '%b' "No available task with ${REDB}${tadone_task_regexp}${RESET} pattern."
return 1
fi
# }}}
# Get task's description
2023-07-21 09:55:15 +02:00
tadone_task_desc=$(task "${tadone_task_id}" | sed --quiet "s/^Description *\(.*\)/\1/p")
2021-11-26 15:57:01 +01:00
printf '%b' "Mark the task \"${MAGENTAB}${tadone_task_id} ${tadone_task_desc}${RESET}\" as done [Y/n] ? "
read -r tadone_confirmation
# Check confirmation
if printf -- '%s' "${tadone_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
task "${tadone_task_id}" done
printf '%b' "\"${MAGENTAB}${tadone_task_id} ${tadone_task_desc}${RESET}\" task is now complete."
fi
}
## }}}
## Purge an old task {{{
function tapurge() {
2023-02-21 14:46:51 +01:00
local tapurge_task_regexp
local tapurge_task_desc
local tapurge_confirmation
# Verify argument
if [ "${#}" -eq 0 ]; then
## If no argument
## Ask the user to choose a task description from the deleted tasks list {{{
task all status:deleted || return 0
printf '%b' "Enter few ${MAGENTAB}words matching the description${RESET} of the expected task to purge : "
read -r tapurge_task_regexp
else
## Merge all args into one var
tapurge_task_regexp="${*}"
## }}}
fi
2023-02-21 14:46:51 +01:00
tapurge_task_desc=$(task "${tapurge_task_regexp}" simpledeleted | grep --after-context=2 -- "^Description" | tail --lines=1 || return 0)
# If no task with this Description exists, exit function {{{
if [ -z "${tapurge_task_desc}" ]; then
printf '%b' "No available task with ${REDB}${tapurge_task_regexp}${RESET} pattern."
return 1
fi
# }}}
printf '%b' "Completly purge the task \"${MAGENTAB}${tapurge_task_desc}${RESET}\" [Y/n] ? "
read -r tapurge_confirmation
# Check confirmation
if printf -- '%s' "${tapurge_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
task rc.confirmation:no "${tapurge_task_desc}" purge
printf '%b' "\"${MAGENTAB}${tapurge_task_desc}${RESET}\" was purged."
fi
}
## }}}
2021-11-26 15:57:01 +01:00
## Take note for a task {{{
function tanote() {
2023-02-21 14:46:51 +01:00
local tanote_task_regexp
local tanote_task_id
local tanote_task_desc
local tanote_confirmation
# Verify argument
if [ "${#}" -eq 0 ]; then
## If no argument
2021-11-26 15:57:01 +01:00
## Ask the user to choose a task description from the recent tasks {{{
task simple limit:page || return 0
2021-11-26 15:57:01 +01:00
printf '%b' "Enter ${MAGENTAB}a pattern${RESET} matching the expected task that need notes : "
read -r tanote_task_regexp
else
## Merge all args into one var
2021-11-26 15:57:01 +01:00
tanote_task_regexp="${*}"
## }}}
fi
2023-02-21 14:46:51 +01:00
tanote_task_id=$(task "${tanote_task_regexp}" simpleid | grep --after-context=2 -- ID | tail --lines=1 || return 0)
# If no task with this ID exists, exit function {{{
2021-11-26 15:57:01 +01:00
if [ -z "${tanote_task_id}" ]; then
printf '%b' "No available task with ${REDB}${tanote_task_regexp}${RESET} pattern."
return 1
fi
# }}}
# Get task's description
2023-07-21 09:55:15 +02:00
tanote_task_desc=$(task "${tanote_task_id}" | sed --quiet "s/^Description *\(.*\)/\1/p")
2021-11-26 15:57:01 +01:00
printf '%b' "Add notes to \"${MAGENTAB}${tanote_task_id} ${tanote_task_desc}${RESET}\" task [Y/n] ? "
read -r tanote_confirmation
# Check confirmation
2021-11-26 15:57:01 +01:00
if printf -- '%s' "${tanote_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
task note "${tanote_task_id}"
fi
}
## }}}
2021-10-08 13:56:28 +02:00
## Completion {{{
zstyle ':completion:*:*:task:*' verbose yes
zstyle ':completion:*:*:task:*:descriptions' format '%U%B%d%b%u'
zstyle ':completion:*:*:task:*' group-name ''
2021-10-08 13:56:28 +02:00
## }}}
# }}}
# Timewarrior {{{
2021-10-08 13:56:28 +02:00
## Aliases
2021-10-11 09:16:08 +02:00
alias tid="tiday"
2021-10-18 08:44:46 +02:00
alias tiw="tiweek"
alias tim="timonth"
2021-10-08 13:56:28 +02:00
alias timv="timove"
2023-02-04 09:30:34 +01:00
alias tiresize="tiduration"
2021-10-08 13:56:28 +02:00
## Functions
## Display tasks of today {{{
function tiday() {
2019-10-16 15:18:24 +02:00
if [ "${#}" -eq 0 ]; then
timew summary :day :ids || return 0
2019-10-16 15:18:24 +02:00
else
timew summary :day :ids "${*}" || return 0
2019-10-16 15:18:24 +02:00
fi
}
## }}}
## Display tasks of last day {{{
function tilastday() {
if [ "${#}" -eq 0 ]; then
timew summary :yesterday :ids || return 0
else
timew summary :yesterday :ids "${*}" || return 0
fi
}
## }}}
## Display tasks of this week {{{
function tiweek() {
2019-10-16 15:18:24 +02:00
if [ "${#}" -eq 0 ]; then
timew summary :week :ids || return 0
2019-10-16 15:18:24 +02:00
else
timew summary :week :ids "${*}" || return 0
2019-10-16 15:18:24 +02:00
fi
}
## }}}
## Display tasks of last week {{{
function tilastweek() {
if [ "${#}" -eq 0 ]; then
timew summary :lastweek :ids || return 0
else
timew summary :lastweek :ids "${*}" || return 0
fi
}
## }}}
## Display tasks of this month {{{
function timonth() {
2019-10-16 15:18:24 +02:00
if [ "${#}" -eq 0 ]; then
timew summary :month :ids || return 0
2019-10-16 15:18:24 +02:00
else
timew summary :month :ids "${*}" || return 0
2019-10-16 15:18:24 +02:00
fi
}
## }}}
## Display tasks of last month {{{
function tilastmonth() {
if [ "${#}" -eq 0 ]; then
timew summary :lastmonth :ids || return 0
else
timew summary :lastmonth :ids "${*}" || return 0
fi
}
## }}}
## Display tasks of this year {{{
function tiyear() {
2019-10-16 15:18:24 +02:00
if [ "${#}" -eq 0 ]; then
timew summary :year :ids || return 0
2019-10-16 15:18:24 +02:00
else
timew summary :year :ids "${*}" || return 0
2019-10-16 15:18:24 +02:00
fi
}
## }}}
## Display tasks of last year {{{
function tilastyear() {
if [ "${#}" -eq 0 ]; then
timew summary :lastyear :ids || return 0
else
timew summary :lastyear :ids "${*}" || return 0
fi
}
## }}}
## Double spent time on a task {{{
## "double" cause by default my tasks runs for 25 minutes
function tidouble() {
# Default duration time to add to a task
2021-10-07 17:52:20 +02:00
local tidouble_extra_time="25mins"
2023-02-21 14:46:51 +01:00
local tidouble_task_id
local tidouble_confirmation
local tidouble_task_desc
# Verify argument
if [ "${#}" -eq 1 ]; then
## Get the first arg as task ID
tidouble_task_id="${1}"
else
## If no argument or more than one
## Ask the user to choose an ID in tasks from the yesterday {{{
timew summary from yesterday :ids || return 0
printf '%b' "Enter the ${MAGENTAB}ID${RESET} of the task to double spent time should be doubled : "
read -r tidouble_task_id
## }}}
fi
# If no task with this ID exists, exit function {{{
if ! timew summary :year :ids | grep --quiet -- " @${tidouble_task_id} "; then
printf '%b' "No available task in the last year with ${REDB}${tidouble_task_id}${RESET} ID."
return 1
fi
# }}}
# Get task's description from all task of this year
2023-07-21 09:55:15 +02:00
tidouble_task_desc=$(timew summary :year :ids | sed --quiet "s/.*@\(${tidouble_task_id} .*\)/\1/p" | sed 's/ */ /g')
printf '%b' "Add ${tidouble_extra_time} to \"${MAGENTAB}${tidouble_task_desc}${RESET}\" [Y/n] ? "
read -r tidouble_confirmation
# Check confirmation
if printf -- '%s' "${tidouble_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
timew lengthen @"${tidouble_task_id}" "${tidouble_extra_time}" || return 0
timew split @"${tidouble_task_id}" || return 0
printf '%b' "${tidouble_extra_time} were added to \"${MAGENTAB}${tidouble_task_desc}${RESET}\" task."
fi
}
## }}}
## Delete a time tracking {{{
function tirm() {
2023-02-21 14:46:51 +01:00
local tirm_task_id
local tirm_task_desc
local tirm_confirmation
# Verify argument
if [ "${#}" -eq 1 ]; then
## Get the first arg as task ID
tirm_task_id="${1}"
else
## If no argument or more than one
## Ask the user to choose an ID in tasks from the yesterday {{{
timew summary from yesterday :ids || return 0
printf '%b' "Enter the ${MAGENTAB}ID${RESET} of the task to delete : "
read -r tirm_task_id
## }}}
fi
# If no task with this ID exists, exit function {{{
if ! timew summary :year :ids | grep --quiet -- " @${tirm_task_id} "; then
printf '%b' "No available task in the last year with ${REDB}${tirm_task_id}${RESET} ID."
return 1
fi
# }}}
# Get task's description from all task of this year
2023-07-21 09:55:15 +02:00
tirm_task_desc=$(timew summary :year :ids | sed --quiet "s/.*@\(${tirm_task_id} .*\)/\1/p" | sed 's/ */ /g')
printf '%b' "Delete to \"${MAGENTAB}${tirm_task_desc}${RESET}\" [Y/n] ? "
read -r tirm_confirmation
# Check confirmation
if printf -- '%s' "${tirm_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
timew delete @"${tirm_task_id}" || return 0
printf '%b' "${tirm_task_desc} was deleted"
fi
}
## }}}
## Modify the start of a time tracking {{{
function tistart() {
# Define new_start empty by default
2023-02-21 14:46:51 +01:00
local tistart_time_new_start=""
local tistart_time_id
local tistart_time_desc
local tistart_time_start_day
local tistart_confirmation
# Verify argument
case "${#}" in
1 )
## Get the first arg as time ID
tistart_time_id="${1}"
;;
2 )
## Get the first arg as time ID
tistart_time_id="${1}"
## Get the second arg as new start time for this track
tistart_time_new_start="${2}"
;;
* )
## Ask the user to choose an ID in time tracking from the yesterday {{{
timew summary from yesterday :ids || return 0
printf '%b' "Enter the ${MAGENTAB}ID${RESET} of the time tracking to modify : "
read -r tistart_time_id
## }}}
;;
esac
# If no time tracking exists with this ID, exit function {{{
if ! timew summary :year :ids | grep --quiet -- " @${tistart_time_id} "; then
printf '%b' "No available time tracking in the last year with ${REDB}${tistart_time_id}${RESET} ID."
return 1
fi
# }}}
# Get time tracking's description from all time tracking of this year
2023-07-21 09:55:15 +02:00
tistart_time_desc=$(timew summary :year :ids | sed --quiet "s/.*@\(${tistart_time_id} .*\)/\1/p" | sed 's/ */ /g')
# Get time tracking's start day from all time tracking
2023-07-21 09:55:15 +02:00
tistart_time_start_day=$(timew export | sed --quiet --regexp-extended "s/^\{\"id\":${tistart_time_id},\"start\":\"([0-9]{4})([0-9]{2})([0-9]{2})T.*end.*/\1-\2-\3/p")
# Check or ask for new start time {{{
if [ -z "${tistart_time_new_start}" ]; then
printf '%b' "Enter the ${MAGENTAB}new start time${RESET} (or new date 'YYYY-MM-DD${REDB}T${RESET}HH:MM:SS'; default day is ${tistart_time_start_day}) for this time tracking : "
read -r tistart_time_new_start
fi
# }}}
printf '%b' "Change start time of \"${MAGENTAB}${tistart_time_desc}${RESET}\" to ${REDB}=> ${tistart_time_new_start} <=${RESET} [Y/n] ? "
read -r tistart_confirmation
# Check confirmation
if printf -- '%s' "${tistart_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
## If the new start time entered by user contains a "T" (user enter a dateTtime)
if printf -- '%s' "${tistart_time_new_start}" | grep --quiet -- "T"; then
printf '%b' "start time of ${tistart_time_desc} is now ${tistart_time_new_start}."
timew modify start @"${tistart_time_id}" "${tistart_time_new_start}" || return 0
else
printf '%b' "start time of ${tistart_time_desc} is now ${tistart_time_start_day}T${tistart_time_new_start}."
timew modify start @"${tistart_time_id}" "${tistart_time_start_day}T${tistart_time_new_start}" || return 0
fi
fi
}
## }}}
## Modify the end of a time tracking {{{
function tiend() {
# Define new_end empty by default
2023-02-21 14:46:51 +01:00
local tiend_time_new_end=""
local tiend_time_id
local tiend_time_new_end
local tiend_time_desc
local tiend_time_start_day
local tiend_confirmation
# Verify argument
case "${#}" in
1 )
## Get the first arg as time ID
tiend_time_id="${1}"
;;
2 )
## Get the first arg as time ID
tiend_time_id="${1}"
## Get the second arg as new end time for this track
tiend_time_new_end="${2}"
;;
* )
## Ask the user to choose an ID in time tracking from the yesterday {{{
timew summary from yesterday :ids || return 0
printf '%b' "Enter the ${MAGENTAB}ID${RESET} of the time tracking to modify : "
read -r tiend_time_id
## }}}
;;
esac
# If no time tracking exists with this ID, exit function {{{
if ! timew summary :year :ids | grep --quiet -- " @${tiend_time_id} "; then
printf '%b' "No available time tracking in the last year with ${REDB}${tiend_time_id}${RESET} ID."
return 1
fi
# }}}
# Get time tracking's description from all time tracking of this year
2023-07-21 09:55:15 +02:00
tiend_time_desc=$(timew summary :year :ids | sed --quiet "s/.*@\(${tiend_time_id} .*\)/\1/p" | sed 's/ */ /g')
# Get time tracking's start day from all time tracking
2023-07-21 09:55:15 +02:00
tiend_time_start_day=$(timew export | sed --quiet --regexp-extended "s/^\{\"id\":${tiend_time_id},\"start\":\"([0-9]{4})([0-9]{2})([0-9]{2})T.*end.*/\1-\2-\3/p")
# Check or ask for new end time {{{
if [ -z "${tiend_time_new_end}" ]; then
printf '%b' "Enter the ${MAGENTAB}new end time${RESET} (or new date 'YYYY-MM-DD${REDB}T${RESET}HH:MM:SS'; default day is ${tiend_time_start_day}) for this time tracking : "
read -r tiend_time_new_end
fi
# }}}
printf '%b' "Change end time of \"${MAGENTAB}${tiend_time_desc}${RESET}\" to ${REDB}=> ${tiend_time_new_end} <=${RESET} [Y/n] ? "
read -r tiend_confirmation
# Check confirmation
if printf -- '%s' "${tiend_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
## If the new end time entered by user contains a "T" (user enter a dateTtime)
if printf -- '%s' "${tiend_time_new_end}" | grep --quiet -- "T"; then
timew modify end @"${tiend_time_id}" "${tiend_time_new_end}" || return 0
printf '%b' "End time of ${tiend_time_desc} is now ${tiend_time_new_end}."
else
timew modify end @"${tiend_time_id}" "${tiend_time_start_day}T${tiend_time_new_end}" || return 0
printf '%b' "End time of ${tiend_time_desc} is now ${tiend_time_start_day}T${tiend_time_new_end}."
fi
fi
}
## }}}
2023-02-04 09:30:34 +01:00
## Modify the duration of a time tracking {{{
function tiduration() {
# Define new_duration empty by default
2023-02-21 14:46:51 +01:00
local tiduration_time_new_duration=""
local tiduration_time_id
local tiduration_time_new_duration
local tiduration_time_desc
local tiduration_confirmation
2023-02-04 09:30:34 +01:00
# Verify argument
case "${#}" in
1 )
## Get the first arg as time ID
tiduration_time_id="${1}"
;;
2 )
## Get the first arg as time ID
tiduration_time_id="${1}"
## Get the second arg as new end time for this track
tiduration_time_new_duration="${2}"
;;
* )
## Ask the user to choose an ID in time tracking from the yesterday {{{
timew summary from yesterday :ids || return 0
printf '%b' "Enter the ${MAGENTAB}ID${RESET} of the time tracking to modify : "
read -r tiduration_time_id
## }}}
;;
esac
# If no time tracking exists with this ID, exit function {{{
if ! timew summary :year :ids | grep --quiet -- " @${tiduration_time_id} "; then
printf '%b' "No available time tracking in the last year with ${REDB}${tiduration_time_id}${RESET} ID."
return 1
fi
# }}}
# Get time tracking's description from all time tracking of this year
2023-07-21 09:55:15 +02:00
tiduration_time_desc=$(timew summary :year :ids | sed --quiet "s/.*@\(${tiduration_time_id} .*\)/\1/p" | sed 's/ */ /g')
2023-02-04 09:30:34 +01:00
# Check or ask for new end time {{{
if [ -z "${tiduration_time_new_duration}" ]; then
printf '%b' "Enter the ${MAGENTAB}new duration${RESET} (eg. 25mins, 1hour…) for this time tracking : "
read -r tiduration_time_new_duration
fi
# }}}
printf '%b' "Change duration of \"${MAGENTAB}${tiduration_time_desc}${RESET}\" to ${REDB}=> ${tiduration_time_new_duration} <=${RESET} [Y/n] ? "
read -r tiduration_confirmation
# Check confirmation
if printf -- '%s' "${tiduration_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
timew resize @"${tiduration_time_id}" "${tiduration_time_new_duration}" || return 0
printf '%b' "Duration of ${tiduration_time_desc} is now ${tiduration_time_new_duration}."
fi
}
## }}}
2021-10-08 13:56:28 +02:00
## Move a time tracking {{{
function timove() {
# Define new_start empty by default
2023-02-21 14:46:51 +01:00
local timove_time_new_start=""
local timove_time_id
local timove_time_new_start
local timove_time_desc
local timove_time_start_day
local timove_confirmation
2021-10-08 13:56:28 +02:00
# Verify argument
case "${#}" in
1 )
## Get the first arg as task ID
timove_time_id="${1}"
2021-10-08 13:56:28 +02:00
;;
2 )
## Get the first arg as task ID
timove_time_id="${1}"
2021-10-08 13:56:28 +02:00
## Get the second arg as new start time for the task
timove_time_new_start="${2}"
2021-10-08 13:56:28 +02:00
;;
* )
## Ask the user to choose an ID in tasks from the yesterday {{{
timew summary from yesterday :ids || return 0
printf '%b' "Enter the ${MAGENTAB}ID${RESET} of the task to move : "
read -r timove_time_id
2021-10-08 13:56:28 +02:00
## }}}
;;
esac
# If no task with this ID exists in the last month, exit function {{{
if ! timew summary :month :ids | grep --quiet -- " @${timove_time_id} "; then
printf '%b' "No available task in the last month with ${REDB}${timove_time_id}${RESET} ID."
2021-10-08 13:56:28 +02:00
return 1
fi
# }}}
# Get task's description from all task of this month
2023-07-21 09:55:15 +02:00
timove_time_desc=$(timew summary :month :ids | sed --quiet "s/.*@\(${timove_time_id} .*\)/\1/p" | sed 's/ */ /g')
# Get time tracking's start day from all time tracking
2023-07-21 09:55:15 +02:00
timove_time_start_day=$(timew export | sed --quiet --regexp-extended "s/^\{\"id\":${timove_time_id},\"start\":\"([0-9]{4})([0-9]{2})([0-9]{2})T.*end.*/\1-\2-\3/p")
2021-10-08 13:56:28 +02:00
# Check or ask for new start time {{{
if [ -z "${timove_time_new_start}" ]; then
printf '%b' "Enter the ${MAGENTAB}new start time${RESET} (or new date 'YYYY-MM-DD${REDB}T${RESET}HH:MM:SS'; default day is ${timove_time_start_day}) for this task : "
read -r timove_time_new_start
2021-10-08 13:56:28 +02:00
fi
# }}}
printf '%b' "Move \"${MAGENTAB}${timove_time_desc}${RESET}\" to ${REDB}=> ${timove_time_new_start} <=${RESET} [Y/n] ? "
2021-10-08 13:56:28 +02:00
read -r timove_confirmation
# Check confirmation
if printf -- '%s' "${timove_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then
## If the new start time entered by user contains a "T" (user enter a dateTtime)
if printf -- '%s' "${timove_time_new_start}" | grep --quiet -- "T"; then
timew move @"${timove_time_id}" "${timove_time_new_start}" || return 0
printf '%b' "${timove_time_desc} was moved to ${timove_time_new_start}."
else
timew move @"${timove_time_id}" "${timove_time_start_day}T${timove_time_new_start}" || return 0
printf '%b' "start time of ${timove_time_desc} is now ${timove_time_start_day}T${timove_time_new_start}."
fi
2021-10-08 13:56:28 +02:00
fi
}
## }}}
# }}}
2018-03-07 16:32:10 +01:00
# DebOps {{{
2022-03-24 10:21:56 +01:00
## Source DebOps user environment file {{{
if [ -f "${HOME}/.config/debops/environment" ]; then
source "${HOME}/.config/debops/environment"
fi
## }}}
## Direct access to DebOps's directories
alias dero="cd ${DEBOPS_VENV_ROLES:-/dev/null}"
alias deplay="cd ${DEBOPS_VENV_PLAYBOOKS:-/dev/null}"
2021-08-09 10:01:46 +02:00
2022-03-24 10:21:56 +01:00
## Apply full config to a define host(s)/group(s) {{{
function debhost() {
if [ ${ANS_HOST} ]; then
2022-03-24 10:21:56 +01:00
debops run site --limit "${ANS_HOST}"
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
2022-03-24 10:21:56 +01:00
## }}}
## Apply a role to a define host(s)/group(s) {{{
function debrole() {
if [ ${ANS_HOST} ]; then
debops run site --limit "${ANS_HOST}" --tags "role::${1:-/dev/null}"
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
## }}}
## Apply the role from a service to a define host(s)/group(s) {{{
function debserv() {
if [ ${ANS_HOST} ]; then
debops run service/"${1:-/dev/null}" --limit "${ANS_HOST}" --tags "role::${1:-/dev/null}"
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
2018-03-07 16:32:10 +01:00
}
2022-03-24 10:21:56 +01:00
## }}}
## Apply a role from a service to a define host(s)/group(s) {{{
function debservice() {
if [ ${ANS_HOST} ]; then
debops run service/"${1:-/dev/null}" --limit "${ANS_HOST}"
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
## }}}
## Apply a playbook to a define host(s)/group(s) {{{
function debplay() {
if [ ${ANS_HOST} ]; then
if [ ! ${2} ]; then
debops run "${1:-/dev/null}" --limit "${ANS_HOST}"
else
debops run "${1:-/dev/null}" --limit "${ANS_HOST}" --tags "role::${2:-/dev/null}"
fi
2022-03-24 10:21:56 +01:00
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
## }}}
2018-03-07 16:32:10 +01:00
2022-03-24 10:21:56 +01:00
## Check full config to a define host(s)/group(s) {{{
function debchost() {
if [ ${ANS_HOST} ]; then
debops check site --limit "${ANS_HOST}"
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
## }}}
## Check a role to a define host(s)/group(s) {{{
function debcrole() {
if [ ${ANS_HOST} ]; then
debops check site --limit "${ANS_HOST}" --tags "role::${1:-/dev/null}"
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
## }}}
## Check the role from a service to a define host(s)/group(s) {{{
function debcserv() {
if [ ${ANS_HOST} ]; then
debops check service/"${1:-/dev/null}" --limit "${ANS_HOST}" --tags "role::${1:-/dev/null}"
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
## }}}
## Check a role from a service to a define host(s)/group(s) {{{
function debcservice() {
if [ ${ANS_HOST} ]; then
debops check service/"${1:-/dev/null}" --limit "${ANS_HOST}"
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
## }}}
## Check a playbook to a define host(s)/group(s) {{{
function debcplay() {
if [ ${ANS_HOST} ]; then
if [ ! ${2} ]; then
debops run "${1:-/dev/null}" --limit "${ANS_HOST}"
else
debops run "${1:-/dev/null}" --limit "${ANS_HOST}" --tags "role::${2:-/dev/null}"
fi
2022-03-24 10:21:56 +01:00
else
printf '%b' "${MAGENTA}ANS_HOST${RESET} vars is ${REDB}not${RESET} define!\n"
fi
}
## }}}
2018-03-07 16:32:10 +01:00
# }}}
# Web apps {{{
# Get weather
2022-07-20 17:26:41 +02:00
function meteo {
if command -v curl > /dev/null; then
curl http://fr.wttr.in/${*}
elif command -v wget > /dev/null; then
2023-07-21 09:55:15 +02:00
wget --quiet --output-document=- http://wttr.in/${*}
2022-07-20 17:26:41 +02:00
else
printf '%b' "Please ${REDB}install one of this tools :${RESET} curl or wget.\n"
fi
}
# }}}
2018-03-07 16:32:10 +01:00
2014-08-17 22:54:05 +02:00
#######################################
#Ouverture d'un programme en fonction
#de l'extension du fichier
#######################################
alias -s txt='$PAGER '
alias -s odt='libreoffice --writer '
alias -s rtf='libreoffice --writer '
alias -s doc='libreoffice --writer '
alias -s docx='libreoffice --writer '
2014-08-17 22:54:05 +02:00
alias -s ods='libreoffice --calc '
alias -s xls='libreoffice --calc '
alias -s html=$BROWSER
alias -s org=$BROWSER
alias -s php=$BROWSER
alias -s com=$BROWSER
alias -s net=$BROWSER
alias -s png='mirage '
alias -s jpg='mirage '
alias -s gif='mirage '
alias -s mp4='smplayer '
alias -s avi='smplayer '
alias -s flv='smplayer '
2023-07-21 09:55:15 +02:00
alias -s log='tail --follow'
2014-08-17 22:54:05 +02:00
alias -s conf='vim '
alias -s gz='gunzip '
2023-07-21 09:55:15 +02:00
alias -s bz2='bzip2'
2014-08-17 22:54:05 +02:00
#######################################
# Raccourcis pour les dossier
# Pour y accéder, faire ~NOM_DU_RACCOURCIS
#######################################
2015-11-11 23:49:53 +01:00
hash -d apt="/etc/apt"
2014-08-17 22:54:05 +02:00
hash -d zsh="/etc/zsh"
hash -d dl="/media/udata/download/"
hash -d sid="/media/udata/config_debian"
hash -d smb="/etc/samba/"
# Inutile mais fun :p
##Affiche le calendrier et le jour courant en couleur
#alias ccal='var=$(cal -m); echo "${var/$(date +%-d)/$(echo -e "\033[1;31m$(date +%-d)\033[0m")}"'
alias ccal='var=$(cal); echo "${var/$(date +%-d)/$(echo -e "\033[1;31m$(date +%-d)\033[0m")}"'
##Commande utilisant cowsay (ou PONYSAY!!) pour afficher un message
function bonjour() {
2023-02-21 16:11:56 +01:00
local MIN_TIME=$(date +%M)
local MODULO_MIN=$(($MIN_TIME % 2))
if [ $(command -v bash_quote) ] && [ $(command -v cowsay) ]; then
if [ $MODULO_MIN -eq 0 ]; then
#echo Bonjour $USER, nous sommes le `date +"%A %e %B %Y"`, et il est : `date +"%H"` h `date +"%M"` | cowsay -f $(/bin/ls /usr/share/cowsay/cows -1 | head -n $(expr $$$(date +%s) % $(ls /usr/share/cowsay/cows | wc -w) + 1) | tail -n 1)
2023-08-30 18:00:19 +02:00
# Random cow
command bash_quote | cowsay -f $(find /usr/share/cow* -type f -iname "*.cow" | sort --random-sort | head --lines=1)
else
#echo Bonjour $USER, nous sommes le `date +"%A %e %B %Y"`, et il est : `date +"%H"` h `date +"%M"` | ponythink
command bash_quote | ponythink
fi
2014-08-17 22:54:05 +02:00
else
printf '%b' "${WHITEB}bonjour function${RESET}: One of the prerequesites is unavailable.\nFor bash_quote please see: https://git.101010.fr/dotfiles-gardouille/scripts/blob/master/bash_quote\n"
2014-08-17 22:54:05 +02:00
fi
}
##Affiche quelques statistiques à propos de l'ordinateur
2023-07-21 09:55:15 +02:00
alias stat_sys="echo ' ' && uname --all && echo ' '&& uptime &&echo ' '&& df && echo ' '"
2014-08-17 22:54:05 +02:00
#####################################
#####FONCTIONS
######################################
## Verrouiller le shell avec vlock
#function TRAPALRM() { vlock }
##Cree le repertoire et va dedans
function mkcd() {
2023-02-06 16:06:37 +01:00
mkdir -- $1 && cd $_
2014-08-17 22:54:05 +02:00
}
#liste les alias et functions
function listalias(){
2023-07-21 09:55:15 +02:00
cat /etc/zsh/zshrc | egrep "alias|function" | grep --invert-match "^#" -- | $PAGER
2014-08-17 22:54:05 +02:00
}
# Get real address behind a shorten URL {{{
# Require a function because the given argument need to be passed to curl
function unshorten()
{
curl --silent https://unshorten.me/s/"${1-}"
}
# }}}
2014-08-17 22:54:05 +02:00
2023-01-27 10:49:23 +01:00
# Get public IP address {{{
2014-08-17 22:54:05 +02:00
function ippub()
{
#curl ifconfig.me
#wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d" " -f 6 | cut -d\< -f 1
dig +short myip.opendns.com @resolver1.opendns.com
2014-08-17 22:54:05 +02:00
}
2023-01-27 10:49:23 +01:00
# }}}
2014-08-17 22:54:05 +02:00
2023-01-27 10:49:23 +01:00
# Get all private IP addresses {{{
2014-08-17 22:54:05 +02:00
function ippriv()
{
for interface in $(find /sys/class/net/ ! -name lo -type l -printf "%f\n" | sort);
do
2023-02-21 16:11:56 +01:00
local IP_INTER=$(ip a s ${interface}|grep "inet "|awk '{print $2}')
printf '%b' "${WHITEB}${interface}${RESET}: ${IP_INTER}\n"
done
2014-08-17 22:54:05 +02:00
}
2023-01-27 10:49:23 +01:00
# }}}
# Get main private IP adress {{{
function ipmain()
{
2023-08-30 17:39:46 +02:00
ip route get 1 | awk '{print $(NF-2);exit}'
2023-01-27 10:49:23 +01:00
}
# }}}
2014-08-17 22:54:05 +02:00
# Test if a network connection is available
function is_network()
{
for path_interface in $(find /sys/class/net/ ! -name lo -type l);
do
2023-02-21 16:11:56 +01:00
local IS_UP=$(grep 1 ${path_interface}/carrier)
if [ ${IS_UP} ]; then
return 0
fi
done
printf '%b' "${REDB}Not Online${RESET}\n"
return 1
}
2014-08-17 22:54:05 +02:00
#Renomme les fichiers en minuscule
function lowercase()
{
for file in *; do
2023-02-21 16:11:56 +01:00
local filename=${file##*/}
2014-08-17 22:54:05 +02:00
case "$filename" in
2023-02-21 16:11:56 +01:00
*/*) local dirname==${file%/*} ;;
*) local dirname=.;;
2014-08-17 22:54:05 +02:00
esac
2023-02-21 16:11:56 +01:00
local nf=$(printf $filename | tr A-Z a-z)
local newname="${dirname}/${nf}"
2014-08-17 22:54:05 +02:00
if [ "$nf" != "$filename" ]; then
mv "$file" "$newname"
printf 'lowercase: %s --> %s\n' ${file} ${newname}
2014-08-17 22:54:05 +02:00
else
printf 'lowercase %s not changed\n' ${file}
2014-08-17 22:54:05 +02:00
fi
done
}
##Capture d'écran
function printscreen()
{
2023-07-21 09:55:15 +02:00
scrot --select --exec 'gimp $f ; rm --recursive --force -- $f'
2014-08-17 22:54:05 +02:00
}
## Latex:
##Redéfinition de pdflatex:
# La commande pdflatex est exécutée deux fois pour permettre la construction de l'index
function pdflatex()
{
# On récupère le nom du fichier sans l'extension
2023-02-21 16:11:56 +01:00
local file_name="${1:r}"
2014-08-17 22:54:05 +02:00
# Le fichier avec l'extension pdf
2023-02-21 16:11:56 +01:00
local pdf="${file_name}.pdf"
2014-08-17 22:54:05 +02:00
# Dossier temporaire
2023-02-21 16:11:56 +01:00
local temp_dir="temp"
2014-08-17 22:54:05 +02:00
# On supprime le fichier pdf si il est présent
if [ -f "${pdf}" ]; then
2023-07-21 09:55:15 +02:00
rm --recursive --force -- "${pdf}"
2014-08-17 22:54:05 +02:00
fi
# Si le répertoire temporaire n'existe pas, on le crée
if [ ! -d "${temp_dir}" ]; then
mkdir "${temp_dir}"
fi
# On exécute la commande pour créer le pdf
/usr/bin/pdflatex -output-directory ${temp_dir} $1
/usr/bin/pdflatex -output-directory ${temp_dir} $1
# On place le fichier pdf qui est dans le répertoire temporaire dans le répertoire courant
2023-07-21 09:55:15 +02:00
mv --force -- "${temp_dir}"/*.pdf .
2014-08-17 22:54:05 +02:00
}
#Éteint un pc sous windows à distance
#Nécessite le paquet samba-common
# Arguments:
# ${1}: l'ip de la machine
# ${2}: le nom d'utilisateur
# ${3}: le mot de passe de l'utilisateur
function eteint()
{
net rpc shutdown -f -I $1 -U $2%$3 -t 10 -C "Arrêt en cours..."
}
# Calculatrice en ligne de commande
function calc()
{
echo "${1}" | bc -l
}
# Afficher le code retour de la commande précédente
function cmd_status {
local exit_code=$? # exit code of command
2023-07-21 09:55:15 +02:00
local count=$(jobs | wc --lines) # no. of background jobs
2014-08-17 22:54:05 +02:00
# Report no. of background jobs if >0
if [ $count -gt 0 ]; then
echo -n " %{$fg[cyan]%}⎇ %j "
fi
# Report exit code
if [ $exit_code -ne 0 ]; then
echo -n "%{$fg[yellow]%}%?%{$fg[red]%} ✖%{$reset_color%}"
else
echo -n "%{$fg[green]%}✔%{$reset_color%}"
fi
}
# Affiche un rappel des raccourcis pour se déplacer dans vim
function rappel_vim()
{
echo "\
##########################################
# Rappel basique pour VIM
##########################################
# Déplacements basiques :
H pour se déplacer vers la gauche
J pour se déplacer vers le bas
K pour se déplacer vers la droite
L pour se déplacer vers la droite
# Déplacements avancés sur une ligne :
e pour aller à la fin du mot suivant
w pour aller au début du mot suivant
2014-08-17 22:54:05 +02:00
b pour aller à la fin du mot précédent
0 pour aller en début de ligne
$ pour aller en fin de ligne
^ pour aller au premier caractère de la ligne qui n'est pas un espace ni une tabulation
f<c> pour aller jusqu'au caractère <c> vers l'avant
3f<c> pour aller jusqu'à la 3ème occurence du caractère <c> vers l'avant
2014-08-17 22:54:05 +02:00
F<c> pour aller jusqu'au caractère <c> vers l'arrière
nF<c> pour aller jusqu'à la nème occurence du caractère <c> vers l'arrière
2014-08-17 22:54:05 +02:00
t<c> pour aller jusqu'au caractère <c> vers l'avant en s'arrêtant avant
T<c> pour aller jusqu'au caractère <c> vers l'arrière en s'arrêtant avant
# Déplacement dans le document :
gg pour aller au début du document
G pour aller à la fin du document
nG pour aller à la ligne <n>
:<n> pour aller à la ligne <n>
% pour aller à la parenthèse (acollade, crochet) correspondant
2014-08-17 22:54:05 +02:00
# Copier/coller avancé
\"ayy Copier la ligne courante dans le buffer \"a\"
\"5byy Copier les 5 lignes sous le curseur dans le buffer \"b\"
<MODE_VISUEL>\"ey Copier les lignes sélectionnées dans le buffer \"e\"
# undo & redo
u pour annuler pour annuler la dernière modification
2014-08-17 22:54:05 +02:00
CTRL+R pour refaire la dernière modification
# Gestion du texte
~ pour inverser la casse du texte sélectionné
# Supprimer le texte du curseur à la fin de la ligne
<mode commande> d$
<mode insertion> CTRL-o D
# Supprimer le texte du curseur au début de la ligne
<mode commande> d^
" | $PAGER
}
# Affiche un rappel des raccourcis pour se déplacer dans vim
function rappel_vim_avance()
{
echo "\
##########################################
# Utilisation un peu plus avancée de VIM
##########################################
# nom fichier
# Avec par exemple un fichier qui s'appelle fichier.tex dans /home/limax/Documents/
# est le nom du fichier en cours
:!echo % (renvoie /Documents/fichier.tex )
:!echo %:r (renvoie /Documents/fichier )
:!echo %:e (renvoie tex )
:!echo %:t (renvoie fichier.tex )
:!echo %:p (renvoie /home/limax/Documents/fichier.tex )
:!echo %:t (renvoie fichier.tex )
:!echo %:h (renvoie Documents)
# Par exemple pour ouvrir le pdf avec lesspipe
:!lesspipe %:r.pdf
# Indentation
# Pour réindenter du texte:
mode visuel
- sélectionner toutes les lignes à indenter correctement
touche \"=\"
# Pour décaler plusieurs lignes de plusieurs tabulations:
mode visuel
- sélectionner toutes les lignes à décaler
:>>>
# Commande
# Pour exécuter une seule commande à partir du mode insertion
<mode insertion> CTRL-o suivi de la commande à exécuter
# Code touche
# Pour afficher le code clavier d'une touche spéciale en mode insertion:
<mode insertion> CTRL-V
# Pour afficher le code interne d'une touche spéciale:
<mode insertion> CTRL-K
# Pour obtenir le code ascii d'un caractère:
ga
# Plier/déplier
zm Plier tout le document sur un niveau de plus
zr Déplier tout le document sur un niveau de plus
zc Plier un bloc/fonction/class
zo Déplier un bloc/fonction/class
" | $PAGER
}
# Affiche un rappel pour l'édition de fichier Latex
function rappel_latex()
{
echo "\
-\\/- Permet d'afficher -- et non un tiret cadratin
Ligne vide Permet un retour à la ligne entre deux textes
\\ldots Affiche correctement \"...\"
# itemize
Mettre un \\vspace{0.5cm} après une liste?
Laisser une ligne vide après une commande root dans une liste?
# À faire
Ajouter un style pour les notes d'informations
Ajouter un style pour les notes importantes
" | $PAGER
}
## Vérifier les logins et logouts de tous les comptes incluant le mien.
watch=all
# Vérifie toutes les 30 secondes
logcheck=30
# Change le format de watch pour quelques chose avec plus d'informations
# %n = username, %M = hostname, %a = action, %l = tty, %T = time,
# %W = date
WATCHFMT="%n from %M has %a tty%l at %T %W"
# Crypte le fichier passer en paramètre en utilisant le certificat public
# de l'utilisateur.
# Arguments:
# ${1}: fichier à crypter
# ${2}:
function crypt()
{
openssl smime -encrypt -aes128 -in ${1} -out .${1}.ls ~/.openssl/mycert.crt
}
# Décrypte le fichier passer en paramètre en fonction de la clef ssl de
# l'utilisateur et trouve la ligne contenant le mot rechercher par l'user
# Arguments:
# ${1}: le fichier à décrypter
# ${2}: le paramètre rechercher dans le fichier
function getpdw()
{
openssl smime -decrypt -in ${1} -inkey ~/.openssl/mykey.key | grep ${2}
}
# Décrypte le fichier passer en paramètre en fonction de la clef ssl de
# l'utilisateur et le fichier de sorti est le second paramètre
# Arguments:
# ${1}: le fichier à décrypter
# ${2}: le fichier de sorti
function decrypt()
{
openssl smime -decrypt -in ${1} -out ${2} -inkey ~/.openssl/mykey.key
}
################################################
# 2. Prompt et définition des touches basiques #
################################################
# Prompt couleur (la couleur n'est pas la même pour le root et
# pour les simples utilisateurs)
# Définition des couleurs:
bleu_clair="[36;1m"
bleu_fonce="[34;1m"
rouge="[31m"
jaune="[33m"
blanc="[37m"
vert="[32m"
couleur_normale="[0m"
# Définition du texte du prompt
heure="%{$bleu_clair%}%T"
user="%{$bleu_fonce%}%n"
user_root="%{$rouge%}%n"
at="%{$jaune%}@"
host="%{$blanc%}%m"
repertoire_courant="%{$vert%}%c"
repertoire_absolu="%{$vert%}%d"
# Chemin du répertoire courant en relatif à ~
repertoire_relatif="%{$vert%}%~"
root="%{$jaune%}%#"
noroot="%{$jaune%}%%"
normal="%{$couleur_normale%}"
# Définition du prompt
if [ "`id -u`" -eq 0 ]; then ## Root
export PS1="$heure $user_root$at$host $repertoire_courant$root $normal"
# export PS1="%{$bleu_clair%T %{$rouge%n%{$jaune@%{$blanc%m %{$vert%c%{$jaune%#%{%} "
else ## Simple utilisateur
2015-09-09 16:01:45 +02:00
export PS1="$heure $host $repertoire_courant$noroot $normal"
2014-08-17 22:54:05 +02:00
fi
# Prise en charge des touches [début], [fin] et autres
typeset -A key
key[Home]=${terminfo[khome]}
key[End]=${terminfo[kend]}
key[Insert]=${terminfo[kich1]}
key[Delete]=${terminfo[kdch1]}
key[Up]=${terminfo[kcuu1]}
key[Down]=${terminfo[kcud1]}
key[Left]=${terminfo[kcub1]}
key[Right]=${terminfo[kcuf1]}
key[PageUp]=${terminfo[kpp]}
key[PageDown]=${terminfo[knp]}
[[ -n "${key[Home]}" ]] && bindkey "${key[Home]}" beginning-of-line
[[ -n "${key[End]}" ]] && bindkey "${key[End]}" end-of-line
[[ -n "${key[Insert]}" ]] && bindkey "${key[Insert]}" overwrite-mode
[[ -n "${key[Delete]}" ]] && bindkey "${key[Delete]}" delete-char
[[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-history
[[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-history
[[ -n "${key[Left]}" ]] && bindkey "${key[Left]}" backward-char
[[ -n "${key[Right]}" ]] && bindkey "${key[Right]}" forward-char
[[ -n "${key[PageUp]}" ]] && bindkey "${key[PageUp]}" history-beginning-search-backward
[[ -n "${key[PageDown]}" ]] && bindkey "${key[PageDown]}" history-beginning-search-forward
2015-02-26 14:20:54 +01:00
# ctrl+← and ctrl+→ issue
bindkey "^[[1;5C" vi-forward-word-end
bindkey "^[[1;5D" backward-word
2014-08-17 22:54:05 +02:00
# Titre de la fenêtre d'un xterm
case $TERM in
xterm*)
precmd () {print -Pn "\e]0;%n@%m: %~\a"}
;;
esac
# Gestion de la couleur pour 'ls' (exportation de LS_COLORS)
# First, use vivid {{{
if [ $(command -v vivid) ]; then
2023-08-07 16:31:12 +02:00
#export LS_COLORS="$(vivid generate dracula)"
export LS_COLORS="$(vivid generate one-light)"
# }}}
# Second, use dircolors {{{
elif [ -x /usr/bin/dircolors ]
2014-08-17 22:54:05 +02:00
then
if [ -r ~/.dir_colors ]
then
eval "`dircolors ~/.dir_colors`"
elif [ -r /etc/dir_colors ]
then
eval "`dircolors /etc/dir_colors`"
else
eval "`dircolors`"
fi
fi
# }}}
2014-08-17 22:54:05 +02:00
#######################################
# 3. Comportement ligne de commandes #
#######################################
# La ligne de commande dans zsh peut fonctionner suivant différents schémas (options).
# Les principaux que je vais utiliser sont fait pour la ligne de commande dans zhs se
# comporte à la vim. Il y a donc un mode commande/normal et un mode insertion.
# Afficher le mode courant dans le prompt de droite (normal|insertion)
function zle-line-init zle-keymap-select {
RPS1="$(cmd_status) ${${KEYMAP/vicmd/-CMD-}/(main|viins)/-INS-}"
RPS2=$RPS1
zle reset-prompt
}
# easy color names in this config
autoload -U colors && colors
# Chargement des keymaps
zle -N zle-line-init
zle -N zle-keymap-select
### Raccourcis communs à tous les modes
autoload -U edit-command-line
zle -N edit-command-line
bindkey '' edit-command-line # Ouvrir la commande actuelle dans l'éditeur
bindkey '' history-incremental-search-backward # Recherche incrémentale qui monte dans l'historique
#bindkey '' history-incremental-search-forward # Recherche incrémentale qui descend dans l'historique
### Raccourcis spécifiques au mode insertion
bindkey -M viins '' beginning-of-line # Début de ligne
bindkey -M viins '' vi-forward-blank-word-end # Fin du mot courant
#bindkey -M viins '' vi-forward-blank-word # Début du mot suivant
bindkey -M viins '' vi-backward-word # Début du mot précédent
bindkey -M viins '' vi-find-prev-char # Rechercher la précédente occurence d'une lettre
#bindkey -M viins ' ;' vi-repeat-find # Aller à la prochaine occurence de la lettre recherchée
### Raccourcis spécifiques au mode commande
# Le mode commande dispose déjà de la plupart d'un fonctionnement très semblable
# au mode normal de vim. Seul problème pour le moment, l'absence d'un mode visuel.
###########################################
# 4. Options de zsh (cf 'man zshoptions') #
###########################################
# Je ne veux JAMAIS de beeps
unsetopt beep
unsetopt hist_beep
unsetopt list_beep
# Redirection de la sortie:
# >| doit être utilisés pour pouvoir écraser un fichier déjà existant ;
# le fichier ne sera pas écrasé avec '>'
unsetopt clobber
# Ctrl+D est équivalent à 'logout'
unsetopt ignore_eof
# Affiche le code de sortie si différent de '0'
setopt print_exit_value
# Demande confirmation pour 'rm *'
unsetopt rm_star_silent
# Attend 10 secondes avant d'exécuter une commande rm qui contient un * (asterisk).
setopt rmstarwait
# Correction orthographique des commandes
# Désactivé car, contrairement à ce que dit le "man", il essaye de
# corriger les commandes avant de les hasher
setopt correct
# Si on utilise des jokers dans une liste d'arguments, retire les jokers
# qui ne correspondent à rien au lieu de donner une erreur
setopt nullglob
# Désactivé le raccourcis '='
# Par défaut, `ls -l =vim` indiquera l'emplacement de vim.
# =vim équivaut à `which vim`
#setopt noequals
## Activation des fonctions internes de ZSH:
# Liste des fonctions disponibles:
#zcalc : une calculatrice (plus besoin de bc ou autres expr)
#zargs : un super xargs
#zmv : une commande permettant de faire du renommage/déplaçage en masse de fichiers.
#zftp : un client ftp natif
autoload -U zfinit
zfinit
# Les jobs qui tournent en tâche de fond sont nicé à '0'
unsetopt bg_nice
# N'envoie pas de "HUP" aux jobs qui tourent quand le shell se ferme
unsetopt hup
# Lancer le manuel en se placant sur une commande et en faisant {ESC,ALT}+{H,h}
autoload -U run-help
## Gestion de la pile des dossiers:
# Taille maximale de la pile placé dans zshenv
# L'exécution de "cd" met le répertoire d'où l'on vient sur la pile
setopt auto_pushd
# Ignore les doublons dans la pile
setopt pushd_ignore_dups
# N'affiche pas la pile après un "pushd" ou "popd"
setopt pushd_silent
# Inverse l'action de cd +1 et cd +1
setopt pushdminus
# "pushd" sans argument = "pushd $HOME"
setopt pushd_to_home
## Pour bien utiliser la pile:
# `dirs` va afficher la pile
# `cd -NUMÉRO` ira dans le dossier correspondant au numéro du dossier dans la pile
## Dirstack
DIRSTACKSIZE=20
setopt autopushd pushdsilent pushdtohome
## Remove duplicate entries
setopt pushdignoredups
## This reverts the +/- operators.
setopt pushdminus
#DIRSTACKFILE="$HOME/.zsh/cache/dirs"
#if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then
#dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
#[[ -d $dirstack[1] ]] && cd $dirstack[1]
#fi
#chpwd() {
#print -l $PWD ${(u)dirstack} >> $DIRSTACKFILE
#}
###################################
###### Options de complétion ######
###################################
# Schémas de complétion
# - Schéma A :
# 1ère tabulation : complète jusqu'au bout de la partie commune
# 2ème tabulation : propose une liste de choix
# 3ème tabulation : complète avec le 1er item de la liste
# 4ème tabulation : complète avec le 2ème item de la liste, etc...
# -> c'est le schéma de complétion par défaut de zsh.
# Schéma B :
# 1ère tabulation : propose une liste de choix et complète avec le 1er item
# de la liste
# 2ème tabulation : complète avec le 2ème item de la liste, etc...
# Si vous voulez ce schéma, décommentez la ligne suivante :
#setopt menu_complete
# Schéma C :
# 1ère tabulation : complète jusqu'au bout de la partie commune et
# propose une liste de choix
# 2ème tabulation : complète avec le 1er item de la liste
# 3ème tabulation : complète avec le 2ème item de la liste, etc...
# Ce schéma est le meilleur à mon goût !
# Si vous voulez ce schéma, décommentez la ligne suivante :
unsetopt list_ambiguous
# Quand le dernier caractère d'une complétion est '/' et que l'on
# tape 'espace' après, le '/' est effacé
setopt auto_remove_slash
# Ne fait pas de complétion sur les fichiers et répertoires cachés
unsetopt glob_dots
# Traite les liens symboliques comme il faut
setopt chase_links
# Quand l'utilisateur commence sa commande par '!' pour faire de la
# complétion historique, il n'exécute pas la commande immédiatement
# mais il écrit la commande dans le prompt
setopt hist_verify
# Si la commande est invalide mais correspond au nom d'un sous-répertoire
# exécuter 'cd sous-répertoire'
setopt auto_cd
###############################################
# 5. Paramètres de l'historique des commandes #
###############################################
# Définition des variables
SAVEHIST=5000
HISTSIZE=5000
HISTFILE=$HOME/.zsh/history
#export TIMEFMT="%E"
export SAVEHIST HISTSIZE HISTFILE
# Toutes les sessions zsh partage le même historique
#setopt SHARE_HISTORY
# Ajoute l'historique à la fin de l'ancien fichier
#setopt append_history
# Chaque ligne est ajoutée dans l'historique à mesure qu'elle est tapée
setopt inc_append_history
# Ne stocke pas une ligne dans l'historique si elle est identique à la
# précédente
setopt hist_ignore_dups
# Supprime les répétitions dans le fichier d'historique, ne conservant
# que la dernière occurrence ajoutée
#setopt hist_ignore_all_dups
# Supprime les répétitions dans l'historique lorsqu'il est plein, mais
# pas avant
setopt hist_expire_dups_first
# N'enregistre pas plus d'une fois une même ligne, quelles que soient
# les options fixées pour la session courante
#setopt hist_save_no_dups
# La recherche dans l'historique avec l'éditeur de commandes de zsh ne
# montre pas une même ligne plus d'une fois, même si elle a été
# enregistrée plusieurs fois
setopt hist_find_no_dups
# Affichage de la date du début de la commande et sa durée (depuis epoch)
# Culture time: epoch: 1er janvier 1970
setopt extended_history
# Ne pas enregistrer les commandes précédées d'un espace:
setopt hist_ignore_space
###########################################
# 6. Complétion des options des commandes #
###########################################
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}'
zstyle ':completion:*' max-errors 3 numeric
zstyle ':completion:*' use-compctl false
## Pour la liste des processus que l'on peut kill, avec un menu et couleur
zstyle ':completion:*:*:*:*:processes' menu yes select
#zstyle ':completion:*:*:*:*:processes' force-list always
zstyle ':completion:*:processes' command 'ps -fau$USER'
2014-08-17 22:54:05 +02:00
zstyle ':completion:*:*:kill:*:processes' list-colors "=(#b) #([0-9]#)*=36=31"
## Pour éviter de reproposer un argument déjà utiliser dans la commande lors de la complétion
zstyle ':completion:*:(rm|mv|cp|ls|scp):*' ignore-line yes
## cd ne sélectionnera pas le dossier courant lorsqu'il devra remonter d'un dossier
# cd ../<TAB> ne proposera pas le dossier courant par exemple.
zstyle ':completion:*:cd:*' ignore-parents parent pwd
## Pour la liste des fichiers qu'on peut ouvrir avec vim
zstyle ':completion:*:*:vim:*' menu yes select
## Mettre en cache les résultats de l'auto-complétion car certaines fonctions sont lentes (apt, dpkg, ...)
# Ne pas hésiter à faire un petit aptitude install <TAB> et de lister tous les résultats possibles une première fois, histoire d'avoir tous les paquets en cache (~600ko), ça vaut le coup!
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache
## Ajout des couleurs pour la complétion
zmodload -i zsh/complist
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
## Complétion de la commande cd avec les répertoires locaux puis ceux de la variable CDPATH
zstyle ':completion:*:*:cd:*' tag-order local-directories path-directories
## Complétion des commandes ssh avec le contenu du fichier ~/.ssh/config et le contenu de host
2022-11-02 17:11:43 +01:00
#local HOSTS
2023-07-21 09:55:15 +02:00
#[[ -f ~/.ssh/config ]] && HOSTS=(`sed --quiet \'s/^Host \(.*\)/\1/p\' ~/.ssh/config`)
2014-08-17 22:54:05 +02:00
#if [ -f ~/.ssh/config ]; then
#HOSTS=(`grep ^Host ~/.ssh/config | sed s/Host\ // | egrep -v ^\*$`)
#fi
#zstyle ':completion:*:(ssh|scp|sftp|sshfs):*' hosts ${HOSTS}
2022-11-02 17:11:43 +01:00
zstyle ':completion:*:hosts' hosts off
2014-08-17 22:54:05 +02:00
# Complétions pour certains programmes en fonction des extensions
zstyle ':completion:*:*:vlc:*' file-patterns '*.(mkv|MKV|avi|AVI|flv|FLV|mp4|MP4|webm|WEBM|mov|MOV|flac|FLAC|mp3|MP3|ogg|OGG|ogv|OGV|wmv|WMV)\ *(-/):directories'
## Ajout de la complétion pour plusieurs fonctions:
autoload -U compinit
autoload -U zutil
autoload -U complist
# Activation
compinit
###########################################
# 7. Création des fichiers et répertoires #
###########################################
# Dossier .zsh dans le répertoire personnel
if [ ! -d ~/.zsh ]; then
mkdir ~/.zsh
fi
# Fichier contenant l'historique pour chaque utilisateur
if [ ! -f ~/.zsh/history ]; then
touch ~/.zsh/history
fi
# Dossier contenant le cache pour chaque utilisateur
if [ ! -d ~/.zsh/cache ]; then
mkdir ~/.zsh/cache
fi
###########################################
############## 8. Globbing ################
###########################################
# Activation du globbing étendu
setopt extendedglob
## '^' inverse la pattern qui suit.
# Exemple:
# `ls ^*.log` listera tous les fichiers exceptés *.log
###########################################
############### 9. Modules ################
###########################################
2024-03-12 09:12:46 +01:00
## IF fzf is AVAILABLE
if [ $(command -v fzf) ]; then
2017-12-15 13:47:15 +01:00
2022-11-02 15:54:13 +01:00
## If local fzf bin is available use it in priority
2017-12-15 13:47:15 +01:00
if [[ ! "$PATH" == *"${HOME}"/.fzf/bin* ]]; then
2022-11-02 15:54:13 +01:00
export PATH="${HOME}/.fzf/bin:${PATH}"
2017-12-15 13:47:15 +01:00
fi
2024-03-12 12:06:56 +01:00
## IF fd is AVAILABLE {{{
if [ -f ~/bin/fd ] ||
[ -f /bin/fd ] ||
[ -f /usr/bin/fd ]; then
export FZF_DEFAULT_COMMAND='fd --type f'
fi
2024-03-12 12:06:56 +01:00
## }}}
autoload is-at-least
2024-03-13 12:45:50 +01:00
# FZF_DEFAULT_OPTS according to fzf version {{{
if is-at-least 0.29.0 $(fzf --version); then
2022-11-28 08:19:15 +01:00
export FZF_DEFAULT_OPTS="--cycle --multi --select-1 --bind 'change:first,shift-tab:down,tab:up'"
else
## change:first is unknown in FZF before 0.29.0
export FZF_DEFAULT_OPTS="--cycle --multi --select-1 --bind 'tab:down,shift-tab:up'"
fi
2024-03-13 12:45:50 +01:00
# }}}
2022-02-21 20:31:32 +01:00
2017-12-15 13:47:15 +01:00
# Auto-completion
# ---------------
[[ $- == *i* ]] && source "${HOME}/.fzf/shell/completion.zsh" 2> /dev/null
# Key bindings
# ------------
2024-03-13 12:45:50 +01:00
test -f "${HOME}/.fzf/shell/key-bindings.zsh" && source "${HOME}/.fzf/shell/key-bindings.zsh"
test -f "/usr/share/fzf/key-bindings.zsh" && source "/usr/share/fzf/key-bindings.zsh"
2017-12-15 13:47:15 +01:00
# ff - cd to selected directory exclude hidden directories and their content {{{
# Search with find (fd|fdfind overkill the CPU for few benefits on small tree)
# Check for symlinked directories too
# Allow to give arguments to prefill fzf request
# Display a preview tree of the directory
# Move to the selected directory
2023-11-02 11:36:21 +01:00
function ff() {
2017-12-15 13:47:15 +01:00
local dir
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:47:39 +01:00
dir=$(find -L . -xtype d -not -path "*/.*" | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 2 {} | head --lines=20' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:47:39 +01:00
dir=$(find -L . -xtype d -not -path "*/.*" | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 2 {} | head --lines=20' --no-multi --query "${*} ") &&
fi
## }}}
cd -- "${dir}"
}
# }}}
# ffh - cd to selected directory (hidden only) {{{
# Search with find (fd|fdfind overkill the CPU for few benefits on small tree)
# Check for symlinked directories too
# Allow to give arguments to prefill fzf request
# Display a preview tree of the directory
# Move to the selected directory
2023-11-02 11:36:21 +01:00
function ffh() {
2017-12-15 13:47:15 +01:00
local dir
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:47:39 +01:00
dir=$(find -L . -xtype d -path "*/.*" | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 2 {} | head --lines=20' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:47:39 +01:00
dir=$(find -L . -xtype d -path "*/.*" | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 2 {} | head --lines=20' --no-multi --query "${*} ") &&
fi
## }}}
cd -- "${dir}"
2017-12-15 13:47:15 +01:00
}
# }}}
# ffa - cd to any directory from / {{{
# Search with fd (fdfind is perfect to search on /)
# Check for symlinked directories too
# Allow to give arguments to prefill fzf request
# Display a preview tree of the directory
# Move to the selected directory
2023-11-02 11:36:21 +01:00
function ffa() {
local dir
2017-12-15 13:47:15 +01:00
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
dir=$(fd -uu --search-path / --type d --type symlink --follow | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 1 {} | head --lines=20' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
dir=$(fd -uu --search-path / --type d --type symlink --follow | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 1 {} | head --lines=20' --no-multi --query "${*} ") &&
fi
## }}}
2017-12-15 13:47:15 +01:00
cd -- "${dir}"
}
# }}}
# ffu - cd to selected parent directory {{{
2023-11-02 11:36:21 +01:00
function ffu() {
2017-12-15 13:47:15 +01:00
local declare dirs=()
local dir
## Function to list parents of the given directory {{{
2017-12-15 13:47:15 +01:00
get_parent_dirs() {
if [[ -d "${1}" ]]; then dirs+=("$1"); else return; fi
if [[ "${1}" == '/' ]]; then
for _dir in "${dirs[@]}"; do echo $_dir; done
else
get_parent_dirs $(dirname "${1}")
2017-12-15 13:47:15 +01:00
fi
}
## }}}
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2022-02-21 20:31:32 +01:00
dir=$(get_parent_dirs $(realpath "${PWD}") | fzf --prompt='cd> ' --tac --height=50% --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2022-02-21 20:31:32 +01:00
dir=$(get_parent_dirs $(realpath "${PWD}") | fzf --prompt='cd> ' --tac --height=50% --no-multi --query "${*} ") &&
fi
## }}}
2017-12-15 13:47:15 +01:00
cd -- "${dir}"
}
# }}}
# fff - cd to the directory of the selected file {{{
2023-02-06 16:06:37 +01:00
# Search with fd (after few tests, fdfind is fareway better than find… even on small tree)
# Check for symlinked files too
# Allow to give arguments to prefill fzf request
2023-09-16 07:33:24 +02:00
# Display a directory preview tree of the selected file with eza
# Move to the directory of the selected file
2023-11-02 11:36:21 +01:00
function fff() {
2017-12-15 13:47:15 +01:00
local file
local dir
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
file=$(fd -uu --type file --type symlink --follow | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 2 $(dirname {}) | head --lines=20' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
file=$(fd -uu --type file --type symlink --follow | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 2 $(dirname {}) | head --lines=20' --no-multi --query "${*} ") &&
fi
## }}}
dir=$(dirname "${file}")
cd -- "${dir}"
}
# }}}
# cf - fuzzy cd from anywhere {{{
# Search with fd (fdfind is perfect to search on /)
# Allow to give arguments to prefill fzf request
2023-09-16 07:33:24 +02:00
# Display a directory preview tree of the selected file with eza
# Move to the directory of the selected file
2023-11-02 11:36:21 +01:00
function cf() {
2017-12-15 13:47:15 +01:00
local file
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
file=$(fd -uu --search-path / --follow | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 1 $(dirname {}) | head --lines=20' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
file=$(fd -uu --search-path / --follow | fzf --prompt='cd> ' --height=50% --preview 'eza --tree --level 1 $(dirname {}) | head --lines=20' --no-multi --query "${*} ") &&
fi
## }}}
2017-12-15 13:47:15 +01:00
if [[ -n "${file}" ]]
2017-12-15 13:47:15 +01:00
then
if [[ -d "${file}" ]]
2017-12-15 13:47:15 +01:00
then
### If it's a directory, cd
cd -- "${file}"
2017-12-15 13:47:15 +01:00
else
### If it's a file, cd to the directory
cd -- "${file:h}"
2017-12-15 13:47:15 +01:00
fi
fi
}
# }}}
2017-12-15 13:47:15 +01:00
# v - fuzzy open file with vi from current directory {{{
# Search with fd (fdfind is perfect to search more than ~200k files)
# Check for symlinked files too
# Allow to give arguments to prefill fzf request
# Display the 50 first lines of the selected file with bat (batcat)
# Move to the directory of the selected file
# Open the selected file with vi
function v() {
local files
local dir
local file
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
files=$(fd -uu --type file --type symlink --follow | fzf --prompt='vi> ' --preview 'bat --color=always --line-range 0:50 {}' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
files=$(fd -uu --type file --type symlink --follow | fzf --prompt='vi> ' --preview 'bat --color=always --line-range 0:50 {}' --no-multi --query "${*} ") &&
fi
## }}}
## Move to the directory and open the file {{{
if [[ -n "${files}" ]]
then
dir=$(dirname "${files}")
### Change directory only if not already in the expected dir
test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}"
file=$(basename "${files}")
vi -- "${file}"
fi
## }}}
}
# }}}
# pdf fuzzy open PDF file with "${PDF_VIEWER}" from current directory {{{
# Search with fd (fdfind is better than find with a pattern)
# Allow to give arguments to prefill fzf request
# Display a preview of selected file with lesspipe
# Move to the directory of the selected file
# Open the selected file with "${PDF_VIEWER}"
2023-11-02 11:36:21 +01:00
function pdf() {
local files
local dir
local file
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.pdf$" | fzf --prompt='pdf> ' --preview 'lesspipe {} | less' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.pdf$" | fzf --prompt='pdf> ' --preview 'lesspipe {} | less' --no-multi --query "${*} ") &&
fi
## }}}
## Move to the directory and open the file {{{
if [[ -n $files ]]
then
dir=$(dirname "${files}")
### Change directory only if not already in the expected dir
test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}"
file=$(basename "${files}")
"${PDF_VIEWER}" -- "${file}"
fi
## }}}
}
# }}}
# pdfe fuzzy open PDF file with evince from current directory {{{
# Search with fd (fdfind is better than find with a pattern)
# Allow to give arguments to prefill fzf request
# Display a preview of selected file with lesspipe
# Move to the directory of the selected file
# Open the selected file with evince (default rollback to "${PDF_VIEWER}")
2023-11-02 11:36:21 +01:00
function pdfe() {
local files
local dir
local file
local evince_bin
## If evince is available {{{
if [ $(command -v evince) ]; then
evince_bin=$(command -v evince)
else
evince_bin="${PDF_VIEWER}"
fi
## }}}
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.pdf$" | fzf --prompt='pdf> ' --preview 'lesspipe {} | less' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.pdf$" | fzf --prompt='pdf> ' --preview 'lesspipe {} | less' --no-multi --query "${*} ") &&
fi
## }}}
## Move to the directory and open the file {{{
if [[ -n $files ]]
then
dir=$(dirname "${files}")
cd "${dir}"
file=$(basename "${files}")
"${evince_bin}" -- "${file}"
fi
## }}}
}
# }}}
# pdfz - fuzzy open with zathura from current directory {{{
# Search with fd (fdfind is better than find with a pattern)
# Allow to give arguments to prefill fzf request
# Display a preview of selected file with lesspipe
# Move to the directory of the selected file
# Open the selected file with zathura (default rollback to "${PDF_VIEWER}")
2023-11-02 11:36:21 +01:00
function pdfz() {
local files
local dir
local file
local zathura_bin
## If zathura is available {{{
if [ $(command -v zathura) ]; then
zathura_bin=$(command -v zathura)
else
zathura_bin="${PDF_VIEWER}"
fi
## }}}
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.pdf$" | fzf --prompt='pdf> ' --preview 'lesspipe {} | less' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.pdf$" | fzf --prompt='pdf> ' --preview 'lesspipe {} | less' --no-multi --query "${*} ") &&
fi
## }}}
## Move to the directory and open the file {{{
if [[ -n $files ]]
then
dir=$(dirname "${files}")
cd "${dir}"
file=$(basename "${files}")
"${zathura_bin}" -- "${file}"
fi
## }}}
}
# }}}
# odt fuzzy open text document file with LibreOffice from current directory {{{
# Search with fd (fdfind is better than find with a pattern)
# Search for odt|rtf|doc|docx files
# Allow to give arguments to prefill fzf request
# Move to the directory of the selected file
# Open the selected file with libreoffice --writer
2023-11-02 11:36:21 +01:00
function odt() {
local files
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.(odt|rtf|doc|docx)" | fzf --prompt='odt> ' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.(odt|rtf|doc|docx)" | fzf --prompt='odt> ' --no-multi --query "${*} ") &&
fi
## }}}
## Move to the directory and open the file {{{
if [[ -n $files ]]
then
dir=$(dirname "${files}")
### Change directory only if not already in the expected dir
test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}"
file=$(basename "${files}")
command libreoffice --writer "${file}" &
fi
## }}}
}
# }}}
# ods fuzzy open calc document file with LibreOffice from current directory {{{
# Search with fd (fdfind is better than find with a pattern)
# Search for ods|xls|xlsx files
# Allow to give arguments to prefill fzf request
# Move to the directory of the selected file
# Open the selected file with libreoffice --calc
2023-11-02 11:36:21 +01:00
function ods() {
local files
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.(ods|xls|xlsx)" | fzf --prompt='ods> ' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.(ods|xls|xlsx)" | fzf --prompt='ods> ' --no-multi --query "${*} ") &&
fi
## }}}
## Move to the directory and open the file {{{
if [[ -n $files ]]
then
dir=$(dirname "${files}")
### Change directory only if not already in the expected dir
test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}"
file=$(basename "${files}")
command libreoffice --calc "${file}" &
fi
## }}}
}
# }}}
# odp fuzzy open presentation document file with LibreOffice from current directory {{{
# Search with fd (fdfind is better than find with a pattern)
# Search for odp|ppt|pptx files
# Allow to give arguments to prefill fzf request
# Move to the directory of the selected file
# Open the selected file with libreoffice --impress
2023-11-02 11:36:21 +01:00
function odp() {
local files
## Manage argument {{{
if [ "${#}" -eq "0" ]; then
## Default command without args
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.(odp|ppt|pptx)" | fzf --prompt='odp> ' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "\.(odp|ppt|pptx)" | fzf --prompt='odp> ' --no-multi "${*} ") &&
fi
## }}}
## Move to the directory and open the file {{{
if [[ -n $files ]]
then
dir=$(dirname "${files}")
### Change directory only if not already in the expected dir
test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}"
file=$(basename "${files}")
command libreoffice --impress "${file}" &
fi
## }}}
}
# }}}
# fopen fuzzy open file with xdg-open from current directory {{{
# Search with fd (fdfind is better than find with a pattern)
# Use first argument as fd pattern
# Other arguments will prefill fzf request
# Move to the directory of the selected file
# Open the selected file with xdg-open
2023-11-02 11:36:21 +01:00
function fopen() {
local files
## Manage argument {{{
if [ "${#}" -le "1" ]; then
## Default command with one argument or default value
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "${1:-.}" | fzf --prompt='open> ' --no-multi) &&
else
## If at least one argument was given, add it to fzf query
2023-11-02 11:39:22 +01:00
files=$(fd --unrestricted --type file --type symlink --follow "${1:-.}" | fzf --prompt='open> ' --no-multi --query "${*} ") &&
fi
## }}}
## Move to the directory and open the file {{{
if [[ -n $files ]]
then
dir=$(dirname "${files}")
### Change directory only if not already in the expected dir
test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}"
file=$(basename "${files}")
command xdg-open "${file}" &
fi
## }}}
}
# }}}
2023-01-17 14:46:37 +01:00
# dexec Docker exec in a running container {{{
# Display Docker running containers
# Allow to specify the shell (default to bash)
2023-11-02 11:36:21 +01:00
function dexec() {
2023-01-17 14:46:37 +01:00
local images_name
local container_name
local container_id
images_name=$(docker container ls --format "table {{.Image}}\t {{.RunningFor}}\t {{.Ports}}\t {{.Names}}" | fzf --prompt='docker> ' --tac )
## Extract container_name from the selected line
container_name=$(printf '%b' "${images_name}" | awk '{ print $NF }')
## Get container_id from the selected container_name
container_id=$(docker container ls --quiet --filter=name="${container_name}")
printf "%b\n" "Try to enter to Docker container (named : ${container_name})"
2023-07-21 09:55:15 +02:00
docker exec --interactive --tty -- "${container_id:-/dev/null}" "${1:-bash}"
2023-01-17 14:46:37 +01:00
}
# }}}
# passf Edit pass's passwords {{{
# Display existing files in ~/.password-store without .gpg extension
# https://www.passwordstore.org/
2023-11-02 11:36:21 +01:00
function passf() {
local pass_files
pass_file=$(find ~/.password-store -type f -iname "*.gpg" -printf "%P\n" | sed 's/\(.*\)\.gpg$/\1/' | fzf --prompt='pass > ' )
pass edit "${pass_file:-/dev/null}"
}
# }}}
2023-02-21 16:11:56 +01:00
fi
2024-03-12 09:12:46 +01:00
## ENDIF fzf is AVAILABLE
2023-02-21 16:11:56 +01:00
# }}}
2018-03-29 19:07:07 +02:00
# zsh-syntax-highlighting {{{
## Activate if plugin is available
[ -f ~/repos/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ] && source ~/repos/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
2018-03-29 19:07:07 +02:00
## Additionnal highlighters
### The order is important. Put **regexp** before **main** to prioritize
### the double quotation marks
ZSH_HIGHLIGHT_HIGHLIGHTERS=(regexp main brackets pattern)
### Dangerous commands
ZSH_HIGHLIGHT_PATTERNS+=('rm -rf ' 'fg=white,bold,bg=black')
ZSH_HIGHLIGHT_PATTERNS+=('sudo ' 'fg=white,bold,bg=red')
### CLI
2023-08-07 16:37:55 +02:00
ZSH_HIGHLIGHT_REGEXP+=('\ \-[^- ]+' 'fg=202') # short args in orange (-l)
ZSH_HIGHLIGHT_REGEXP+=('\ \--[^ ]+' 'fg=214') # long args in light orange (--all)
2018-03-29 19:07:07 +02:00
### Taskwarrior
2023-08-07 16:37:55 +02:00
ZSH_HIGHLIGHT_REGEXP+=('\ \+[^ ]+' 'fg=202') # tags in orange (+txt)
ZSH_HIGHLIGHT_REGEXP+=('[^ ]+\:' 'fg=135') # metadata in purple (project:)
ZSH_HIGHLIGHT_REGEXP+=('\ \_[^ ]+' 'fg=32') # subcommands in blue (_command)
2018-03-29 19:07:07 +02:00
# }}}