]> Git — Sourcephile - julm/julm-nix.git/blob - homes/softwares/bash.nix
bash: add mkcd
[julm/julm-nix.git] / homes / softwares / bash.nix
1 { pkgs, lib, config, ... }:
2 {
3 programs.bash = {
4 shellAliases = {
5 cl = "clear";
6 g = "git";
7 grep = "grep --color";
8 j="sudo journalctl -u";
9 jb="sudo journalctl -b";
10 l = "ls -alh";
11 ll = "ls -al";
12 ls = "ls --color=tty";
13 md-toc = "grep '^#\\+' --color";
14 mem = "ps -e -orss=,user=,args= | sort -b -k1,1n";
15 mem-top = "smem --sort rss --autosize";
16 mpl = "mplayer";
17 nixos-clean="sudo nix-collect-garbage -d";
18 nixos-history="sudo nix-env --list-generations --profile /nix/var/nix/profiles/system";
19 nixos-rollback="sudo nixos-rebuild switch --rollback";
20 rsync = "rsync --no-inc-recursive --info=progress2 --inplace --partial";
21 s="sudo systemctl";
22 st="sudo systemctl status";
23 u="systemctl --user";
24 ut="systemctl --user status";
25 watch = "watch --color";
26 };
27 initExtra = ''
28 shopt -s globstar
29 # Disable ctrl-s/ctrl-q flow control
30 stty -ixon
31
32 mkcd () {
33 mkdir -p "$1" &&
34 cd "$1"
35 }
36 smartctl-tbw () {
37 device=''${1:-/dev/sda}
38 sudo smartctl -A $device |
39 { awk '
40 $0 ~ /Power_On_Hours/ { poh=$10; printf "%s / %d hours / %d days / %.2f years\n", $2, $10, $10 / 24, $10 / 24 / 365.25 }
41 $0 ~ /Total_LBAs_Written/ {
42 lbas = $10;
43 bytes = $10 * 512;
44 mb = bytes / 1024^2;
45 gb = bytes / 1024^3;
46 tb = bytes / 1024^4;
47 printf "%s / %s / %d mb / %.1f gb / %.3f tb\n", $2, $10, mb, gb, tb
48 printf "mean writes per hour: / %.2f", mb/poh
49 }
50 $0 ~ /Airflow_Temperature_Cel/ { print $2 " / " $10}
51 $0 ~ /Wear_Leveling_Count/ { printf "%s / %d (%% health)\n", $2, int($4) }
52 '; echo; } |
53 sed -e 's:/:@:' |
54 sed -e "s\$^\$$device @ \$" |
55 column -ts@
56 }
57 swaplist () {
58 lastpid=
59 swap=0
60 sudo grep -H '^Swap:' /proc/*/smaps 2>/dev/null |
61 while IFS=: read -r file x size x
62 do
63 pid=''${file#/proc/}
64 pid=''${pid%/smaps}
65 size=''${size% kB}
66 size=''${size##* }
67 if test "$pid" = "$lastpid"
68 then swap=$(( swap + size ))
69 else
70 if test "$swap" -gt 0
71 then printf "%u pid=%u cmd=%s\n" "$swap" "$lastpid" "$(tr '\000' ' ' </proc/"$lastpid"/cmdline)"
72 fi
73 if test "$pid" = self
74 then break
75 else
76 lastpid=$pid
77 swap=$size
78 fi
79 fi
80 done |
81 sort -nk1,1
82 }
83 vim-git () { $EDITOR $(git diff-tree --no-commit-id --name-only --recursive "$@"); }
84 vim-git-grep () { $EDITOR $(git grep --name-only --recursive "$@"); }
85 zfs-mount () { for d in $(zfs list -rH -o name "$@"); do sudo zfs mount -l "$d"; done; }
86 zfs-unmount () { sudo zfs unmount -u "$@"; }
87 '';
88 profileExtra = ''
89 '';
90 };
91 programs.direnv.enableBashIntegration = true;
92 programs.broot.enableBashIntegration = true;
93 home.sessionVariables = {
94 PS1 = ''\[\033[1;32m\]\[\e]0;\u@\h: \w\a\]\W\[\033[0m\] \$(e=\$?; if [ \$e != 0 ]; then echo '\[\e[0;91m\]'\$e'\[\e[0m\]'; fi)\$ '';
95 };
96 programs.readline = {
97 enable = lib.mkDefault config.programs.bash.enable;
98 includeSystemConfig = true;
99 bindings = {
100 "\\eOF" = "end-of-line";
101 "\\eOH" = "beginning-of-line";
102 "\\e[1;5C" = "forward-word";
103 "\\e[1;5D" = "backward-word";
104 "\\e[1~" = "beginning-of-line";
105 "\\e[4~" = "end-of-line";
106 "\\e[5C" = "forward-word";
107 "\\e[5D" = "backward-word";
108 "\\e[7~" = "beginning-of-line";
109 "\\e[8~" = "end-of-line";
110 "\\e[A" = "history-search-backward";
111 "\\e[B" = "history-search-forward";
112 "\\e[F" = "end-of-line";
113 "\\e[H" = "beginning-of-line";
114 "\\e\\e[C" = "forward-word";
115 "\\e\\e[D" = "backward-word";
116 };
117 variables = {
118 colored-completion-prefix = lib.mkDefault true;
119 colored-stats = lib.mkDefault true; # Note that this may cause completion text blink in some terminals (e.g. xterm).
120 echo-control-characters = lib.mkDefault true;
121 mark-symlinked-directories = lib.mkDefault true;
122 menu-complete-display-prefix = lib.mkDefault true;
123 show-all-if-ambiguous = lib.mkDefault true;
124 show-all-if-unmodified = lib.mkDefault true;
125 visible-stats = lib.mkDefault false; # Append char to indicate type
126 };
127 };
128 }