-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashrc
899 lines (758 loc) · 29.7 KB
/
bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# There are 3 different types of shells in bash: the login shell, normal shell
# and interactive shell. Login shells read ~/.profile and interactive shells
# read ~/.bashrc; in our setup, /etc/profile sources ~/.bashrc - thus all
# settings made here will also take effect in a login shell.
#
# NOTE: It is recommended to make language settings in ~/.profile rather than
# here, since multilingual X sessions would not work properly if LANG is over-
# ridden in every subshell.
# Some applications read the EDITOR variable to determine your favourite text
# editor. So uncomment the line below and enter the editor of your choice :-)
#export EDITOR=/usr/bin/vim
#export EDITOR=/usr/bin/mcedit
# Command line fuzzy search and the like:
# - https://github.com/cantino/mcfly
# - https://github.com/urbainvaes/fzf-marks
# - https://github.com/kimono-koans/two_percent
#
# Modern Unix: https://github.com/ibraheemdev/modern-unix
# Check if this file has already been sourced
#[[ "true" == "${BASHRCREAD}" ]] && return
# Verbose logging
VERBOSE=false
# To get the environment, IntelliJ exec's bash as interactive and as a login script. This environmental variable
# should be used to detect this environment to prevent programs from interacting with the user.
# https://youtrack.jetbrains.com/articles/IDEA-A-19/Shell-Environment-Loading
#if [ -z "$INTELLIJ_ENVIRONMENT_READER" ]; then
# return
#fi
######################################################################
# 80-path.sh
######################################################################
# https://www.cyberciti.biz/faq/redhat-linux-pathmunge-command-in-shell-script/
pathmunge () {
[[ ! -d "${1}" ]] && return
if ! echo "${PATH}" | grep -Eq "(^|:)${1}($|:)" ; then
# Mac id does not accept long arguments
# Linux id accepts short arguments
if [[ "0" == "$(id -u)" ]] ; then
# root always appends the path
PATH="${PATH}:${1}"
elif [[ "after" == "${2}" ]] ; then
PATH="${PATH}:${1}"
elif [[ "before" == "${2}" ]] ; then
PATH="${1}:${PATH}"
else
# Add path to the end by default
PATH="${PATH}:${1}"
fi
fi
}
# Add /*/sbin to the path
pathmunge "/sbin"
pathmunge "/usr/sbin"
# Custom programs on M1 Macs
pathmunge "/usr/local/bin"
# Custom install for Go binaries
#pathmunge "/opt/niceguyit/bin"
# Local binaries
pathmunge "${HOME}/bin"
# Go
pathmunge "${HOME}/go/bin"
# Rust
pathmunge "${HOME}/.cargo/bin"
# Python
pathmunge "${HOME}/.pyenv/bin"
# "pip install --user" installs to ~/.local/bin/
pathmunge "${HOME}/.local/bin"
# Flutter
pathmunge "${HOME}/projects/github/flutter/bin"
# Rancher Desktop
#[[ -d "${HOME}/.rd/bin" ]] && pathmunge "${HOME}/.rd/bin"
pathmunge "${HOME}/.rd/bin"
# Local git repos
pathmunge "${HOME}/projects/server-profile/bin"
pathmunge "${HOME}/projects/server-utils/bin"
pathmunge "${HOME}/projects/dotfiles/bin"
# Composer
pathmunge "${HOME}/.composer/vendor/bin"
# Java
pathmunge "${HOME}/local/spring-2.2.4.RELEASE/bin"
# Yarn
pathmunge "${HOME}/.yarn/bin"
# Current directory
pathmunge "."
# JetBrains Toolbox App
pathmunge "${HOME}/.local/share/JetBrains/Toolbox/scripts"
# Sharetribe requires the ruby environment setup as part of the login.
# Move the path munging before tty detection.
# TODO: Move all path related code before the tty detection.
################################################################################
# Ruby Version Manager (rvm)
################################################################################
# rvm adds $HOME/.rvm/bin to the path even though it's already there
if [[ -s "${HOME}/.rvm/scripts/rvm" ]]
then
[[ -d "${HOME}/.rvm/bin" ]] && pathmunge "${HOME}/.rvm/bin"
[[ -f "${HOME}/.rvm/scripts/rvm" ]] && source "${HOME}/.rvm/scripts/rvm"
fi
######################################################################
# 99-ruby.sh
# TODO: Is ruby needed if rvm is used?
######################################################################
#[[ -d "${HOME}/.gem/bin" ]] && export PATH="${PATH}:${HOME}/.gem/bin"
## gem install directory
#export GEM_HOME="${HOME}/.gem"
#
## Include gem in path
#if which ruby >/dev/null 2>&1 && which gem >/dev/null 2>&1;
#then
#
# if [[ -d "$(ruby -rrubygems -e 'puts Gem.user_dir')" ]]
# then
# # rvm wants .gem to be frist
# export PATH="$(ruby -rrubygems -e 'puts Gem.user_dir'):${PATH}"
# fi
#fi
######################################################################
# 90-node.sh
######################################################################
# Node Version Manager
# Check if NVM is installed
if [[ -s "${HOME}/.nvm/nvm.sh" ]]
then
export NVM_DIR="${HOME}/.nvm"
# Make a symlink to the current version
export NVM_SYMLINK_CURRENT=true
# Load nvm and bash_completion
[[ -s "${NVM_DIR}/nvm.sh" ]] && source "${NVM_DIR}/nvm.sh"
[[ -s "${NVM_DIR}/bash_completion" ]] && . "${NVM_DIR}/bash_completion"
fi
######################################################################
# Go language support
######################################################################
if type -P go >/dev/null 2>&1
then
# openSUSE uses /etc/profile.d/go.sh to setup the environment
# 20190825: This might have changed sometime around go1.12 and/or openSUSE 15.1
pathmunge "$(go env GOPATH)/bin"
# Mac
# libexec needs to be included because Go expects to find src in $GOROOT/src/
pathmunge "$(go env GOROOT)"
# FreeNAS
[[ -d "/usr/local/go" ]] && export GOROOT="/usr/local/go"
# Private repos
# https://stackoverflow.com/questions/27500861/whats-the-proper-way-to-go-get-a-private-repository
export GOPRIVATE="*.niceguyit.biz"
# Disable telemetry
export GOTELEMETRY=off
fi
######################################################################
# 99-python.sh
######################################################################
# Python support
# https://stackoverflow.com/questions/38112756/how-do-i-access-packages-installed-by-pip-user
#export PYTHONPATH="$(python -c "import site, os; print(os.path.join(site.USER_BASE, 'lib', 'python', 'site-packages'))"):$PYTHONPATH"
# Python Virtualenv
if type -P pyenv >/dev/null 2>&1
then
#export PYENV_ROOT="$HOME/.pyenv"
eval "$(pyenv init -)"
fi
# Require a tty
tty -s || return
# Process global bashrc
[[ -f /etc/bashrc ]] && source /etc/bashrc
[[ -f /etc/bash.bashrc ]] && source /etc/bash.bashrc
if [[ "$OSTYPE" == darwin* ]]
then
# Mac's readline does not have --canonicalize argument
# http://stackoverflow.com/questions/7665/how-to-resolve-symbolic-links-in-a-shell-script
DOTFILES_DIR="$( dirname "$( /opt/homebrew/opt/coreutils/bin/grealpath "${BASH_SOURCE[0]}" )" )"
elif [[ "$OSTYPE" == freebsd* ]]
then
# FreeBSD doesn't have readline
DOTFILES_DIR="$( dirname "$( realpath "${BASH_SOURCE[0]}" )" )"
else
DOTFILES_DIR="$( dirname "$( readlink --canonicalize "${BASH_SOURCE[0]}" )" )"
fi
######################################################################
# mac-osx.sh
# Mac OSX specific
######################################################################
if [[ $(uname -s) = 'Darwin' ]]
then
# M1 MacOS
pathmunge "/opt/homebrew/bin"
# Opt out of analytics because it sends to Google Analytics
# https://docs.brew.sh/Analytics
# https://news.ycombinator.com/item?id=11566720
export HOMEBREW_NO_ANALYTICS=1
# Prefer Homebrew packages over system installed packages.
# coreutils
# All commands have been installed with the prefix 'g'.
# If you really need to use these commands with their normal names, you
# can add a "gnubin" directory to your PATH from your bashrc
pathmunge "$(brew --prefix)/opt/coreutils/libexec/gnubin" before
# python
#pathmunge "$(brew --prefix)/opt/python/libexec/bin" before
# openjdk
#pathmunge "$(brew --prefix)/opt/openjdk@11/bin" before
# curl
# brew installed curl but it doesn't have as many features enabled.
# osx Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz HTTP2 UnixSockets HTTPS-proxy
# brew Features: AsynchDNS IPv6 Largefile NTLM NTLM_WB SSL libz UnixSockets
#pathmunge "$(brew --prefix)/opt/curl/bin" before
# openssl
# This formula is keg-only, which means it was not symlinked into /usr/local,
# because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.
# If you need to have this software first in your PATH run:
#pathmunge "$(brew --prefix)/opt/openssl/bin" before
# For compilers to find openssl you may need to set:
#export LDFLAGS="-L$(brew --prefix)/opt/openssl/lib"
#export CPPFLAGS="-I$(brew --prefix)/opt/openssl/include"
# For pkg-config to find openssl you may need to set:
#export PKG_CONFIG_PATH="$(brew --prefix)/opt/openssl/lib/pkgconfig"
#Additionally, you can access their man pages with normal names if you add
#the "gnuman" directory to your MANPATH from your bashrc as well:
#
MANPATH="$(brew --prefix)/opt/coreutils/libexec/gnuman:$MANPATH"
# Bash completion has been installed to:
# /usr/local/etc/bash_completion.d
if [ -d "$(brew --prefix)/etc/bash_completion.d" ]; then
source "$(brew --prefix)"/etc/bash_completion.d/*
fi
# bash completion is in profile.d/bash_completion.sh
if [ -r "$(brew --prefix)/etc/profile.d/bash_completion.sh" ]; then
source "$(brew --prefix)/etc/profile.d/bash_completion.sh"
fi
# Required for gupdatedb
# 2021-09-17 Disabled to support mutli-byte chars
#export LC_ALL="C"
fi
######################################################################
# bash preexec
# Required by atuin
# https://github.com/rcaloras/bash-preexec
######################################################################
[[ -s "${DOTFILES_DIR}/bash-preexec.sh" ]] && source "${DOTFILES_DIR}/bash-preexec.sh"
######################################################################
# atuin
# https://github.com/ellie/atuin
######################################################################
if [[ -n $BASH_VERSION ]] && type -P atuin >/dev/null 2>&1
then
eval "$(atuin init bash --disable-up-arrow)"
eval "$(atuin gen-completions --shell bash)"
fi
######################################################################
# alias.sh
######################################################################
# ls
alias l='ls -lFh'
alias la='ls -lFah'
alias lt='ls -lFAhrt'
alias lrt='ls -lFAht'
# FreeBSD doesn't have vim
[[ $(uname -s) = 'FreeBSD' ]] && alias vim='vi'
# Display the path nicely
# TODO: This conflicts with /etc/bash.bashrc on openSUSE 15.0
# Need to check if the alias/function already exists before adding it.
alias ppath="echo \$PATH | tr ':' '\n'"
# Convert UNICODE (16-bit) to regular text (8-bit)
alias recode='recode -v UNICODE..UTF-8'
# Shortcut for chmod
alias x='chmod a+x'
# Alias for xfreerdp
# TODO: Upgrade to 2.7.0+ and use /network:auto
# https://github.com/FreeRDP/FreeRDP/issues/7915#issuecomment-1135447199
# As for FreeRDP with 2.7.0 it contains Backported #6786: Use /network:auto by default which means
# if you don´t add any /rfx, /gfx, /bpp or /network options it will announce everything we´ve got
# (so you end up with graphics of the latest GFX mode supported by the client and server)
# See also:
# https://techcommunity.microsoft.com/t5/security-compliance-and-identity/remote-desktop-protocol-rdp-10-avc-h-264-improvements-in-windows/ba-p/249588
alias rdp="xfreerdp /w:1920 /h:1080 +bitmap-cache +offscreen-cache /compression-level:2 /network:auto"
# This size is better for 4k
alias rdp4k="xfreerdp /w:2548 /h:1436 +bitmap-cache +offscreen-cache /compression-level:2 /network:auto"
# fd all files and rg all files
# "rga" is taken: https://github.com/phiresky/ripgrep-all
alias fdall="fd --no-ignore --hidden"
alias rgall="rg --no-ignore --hidden"
######################################################################
# bash.sh
######################################################################
if [[ -n $BASH_VERSION ]]
then
# append to the history file, don't overwrite it
shopt -s histappend
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
#shopt -s checkwinsize
# Use case sensitive matching for filename expansions (globs).
#shopt -s nocaseglob
# If no files are matched, the NULL string is returned.
#shopt -s nullglob
# History filename
#export HISTFILE=~/history.txt
# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
#export HISTFILESIZE=
#export HISTSIZE=
#export HISTTIMEFORMAT="[%F %T] "
# Bash 4.3 and later use -1 to set unlimited history.
# Bash prior to 4.3 use "" to set unlimited history.
if [[ "4.3.0" == $(echo -e "$BASH_VERSION\n4.3.0" | sort --version-sort | head -1) ]]
then
# The maximum number of commands to remember on the history list.
export HISTSIZE=-1
# The maximum number of lines contained in the history file.
export HISTFILESIZE=-1
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
else
# The maximum number of commands to remember on the history list.
export HISTSIZE=
# The maximum number of lines contained in the history file.
export HISTFILESIZE=
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
fi
# "ignoredups" means to not enter lines which match the last entered line.
# don't put duplicate lines in the history. See bash(1) for more options
# https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
export HISTCONTROL="ignoredups:erasedups"
# Taken from http://www.jmglov.net/unix/bash.html
# Don't keep useless history commands. Note the last pattern is to not
# keep dangerous commands in the history file. Who really needs to
# repeat the shutdown(8) command accidentally from your command
# history?
# A colon-separated list of patterns used to decide which command lines should be
# saved on the history list.
export HISTIGNORE='\&:fg:bg:ls:l:ll:lt:lrt:lsd:pwd:cd ..:cd ~-:cd -:cd:jobs::x:which*'
# The maximum number of history events to save in the history file. (disk)
export SAVEHIST=5000000
fi
######################################################################
# env.sh
######################################################################
#
# Environmental variables
# See http://teaching.idallen.com/net2003/06w/notes/character_sets.txt
#
# 2021-09-17 Try to use multi-byte characters
# From /etc/sysconfig/language
# > Local users will get RC_LANG as their default language, i.e. the
# > environment variable $LANG . $LANG is the default of all $LC_*-variables,
# > as long as $LC_ALL is not set, which overrides all $LC_-variables.
if [[ $(uname -s) = 'FreeBSD' ]]
then
# Don't change FreeNAS locale
true
else
# Mac and Linux
# Show unicode by processing multi-byte chars
[[ ! "$LANG" ]] && export LANG="en_US.UTF-8"
# collate in strict numeric order
# Separate [a-z] from [A-Z] so sorting is intuitive
export LC_COLLATE="C"
[[ ! "$LC_CTYPE" ]] && export LC_CTYPE="en_US.UTF-8"
[[ ! "$LC_NUMERIC" ]] && export LC_NUMERIC="en_US.UTF-8"
[[ ! "$LC_TIME" ]] && export LC_TIME="en_US.UTF-8"
[[ ! "$LC_MONETARY" ]] && export LC_MONETARY="en_US.UTF-8"
[[ ! "$LC_MESSAGES" ]] && export LC_MESSAGES="en_US.UTF-8"
[[ ! "$LC_PAPER" ]] && export LC_PAPER="en_US.UTF-8"
[[ ! "$LC_NAME" ]] && export LC_NAME="en_US.UTF-8"
[[ ! "$LC_ADDRESS" ]] && export LC_ADDRESS="en_US.UTF-8"
[[ ! "$LC_TELEPHONE" ]] && export LC_TELEPHONE="en_US.UTF-8"
[[ ! "$LC_MEASUREMENT" ]] && export LC_MEASUREMENT="en_US.UTF-8"
[[ ! "$LC_IDENTIFICATION" ]] && export LC_IDENTIFICATION="en_US.UTF-8"
fi
# FreeBSD/FreeNAS doesn't set the PAGER
[[ -z "$PAGER" ]] && export PAGER=less
export LESS="--ignore-case --RAW-CONTROL-CHARS --search-skip-screen --LONG-PROMPT --jump-target=5 --quit-if-one-screen"
#export LESSOPEN="|/usr/local/bin/lesspipe.sh %s"
# Uncomment the following line if you don't like systemctl's auto-paging feature:
export SYSTEMD_PAGER=
# Some applications read the EDITOR variable to determine your favourite text
# editor. So uncomment the line below and enter the editor of your choice :-)
# FIXME: which is not installed in production
# TODO: type -P is a posix replacement
export EDITOR=$(type -P vim)
# export EDITOR=/usr/bin/vim
# Oracle's Java
# Required for Elasticsearch
#export JAVA_HOME=/usr/lib64/jvm/jre-openjdk/
# ls now defaults to adding single quotes around files with special characters.
# http://unix.stackexchange.com/questions/258679/why-is-ls-suddenly-wrapping-items-with-spaces-in-single-quotes
export QUOTING_STYLE=literal
# Disable dotnet telemetry
export DOTNET_CLI_TELEMETRY_OPTOUT=true
export VCPKG_DISABLE_METRICS=true
[[ -f "${HOME}/projects/github/vcpkg/vcpkg" ]] && export VCPKG_ROOT="${HOME}/projects/github/vcpkg"
# SOPS Age support
#[[ -f "${HOME}/.config/sops/age/keys.txt" ]] && export SOPS_AGE_KEY_FILE=~/.config/sops/age/keys.txt
if [[ $EUID -ne 0 ]] && tty >/dev/null
then
# @see https://d.niceguyit.biz/en/internal/troubleshooting
# 2018-01-01 - Add group write and other read permissions for Apache for WordPress-Network
# 2022-01-13 - This was causing issues with zypper on openSUSE Leap 15.3. Disabled it.
umask g-w,o-rwx
# Disabling for now to prevent conflict with the above.
# Unix on Windows doesn't set correct umask
# @see https://github.com/Microsoft/BashOnWindows/issues/352
#umask o-rwx
fi
######################################################################
# functions.sh
######################################################################
# Show the hostname with the full log path
function logpath() {
echo "${HOSTNAME%%.*}:$(ls -1d "$(pwd -P)/$1")"
}
# Add the file sizes output from ls -l
function ad() {
field=5
[[ -n $1 ]] && field=$1
gawk "{sum += \$$field} END {printf \"%'d\n\", sum}"
}
# List only directories
# 2023-03-23: lsd is a rust replacement of ls. Going to use that for now.
#function lsd() {
# -d is necessary to list directories
# ls -ldFA --color=always "$@" | grep '^[dl]'
#}
# Show the count of matches
function grepc() {
grep -c "$@" | grep -v ':0$'
}
# Swap two files/dirs
# https://stackoverflow.com/questions/1115904/shortest-way-to-swap-two-files-in-bash
function swap() {
tmpfile=$(mktemp "$(dirname "$1")/XXXXXX")
mv "$1" "$tmpfile" && mv "$2" "$1" && mv "$tmpfile" "$2"
}
# Short git commit
# Minus is not a valid identifier for shell functions.
# TODO: Should this be moved into a separate program?
function git_commit() {
git add --update
git commit --message "$1"
git push
}
######################################################################
# fzf.bash
# Similar to atuin.
######################################################################
if type -P fzf >/dev/null 2>&1; then
# Bash completion is enabled in the bash-completion section.
# key-bindings/fzf.bash was modified to use Ctrl-Y instead of Ctrl-R to search command history.
# This prevents conflicts with atuin which provides a better search experience.
# Ctrl-R causes Perl to complain about the locale not being set. However it IS set.
# This works: env LC_ALL=en_US.UTF-8 perl -e exit
# This fails: env LC_ALL= perl -e exit
# perl: warning: Setting locale failed.
# perl: warning: Please check that your locale settings:
# LANGUAGE = "en_US.UTF-8",
# LC_ALL = "",
# LC_MEASUREMENT = "en_US.UTF-8",
# LC_MONETARY = "en_US.UTF-8",
# LC_COLLATE = "C",
# LC_NUMERIC = "en_US.UTF-8",
# LC_TIME = "en_SE.UTF-8",
# LANG = "en_US.UTF-8"
# are supported and installed on your system.
# perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
# See https://perldoc.perl.org/perllocale#PERL_SKIP_LOCALE_INIT
export PERL_SKIP_LOCALE_INIT=true
fi
######################################################################
# ls-colors.sh
######################################################################
##
## FreeNAS does not include LS_COLORS.
## This doesn't work, I suspect because FreeNAS ls doesn't support LS_COLORS.
##
if [[ `uname -s` = 'FreeBSD' ]]
then
# FreeNAS commands don't support --color option
# CLICOLOR=true enables the LSCOLORS option (not LS_COLORS)
# Use ANSI color sequences to distinguish file types. See LSCOLORS below. In addition to the
# file types mentioned in the -F option some extra attributes (setuid bit set, etc.) are also
# displayed. The colorization is dependent on a terminal type with the proper termcap(5)
# capabilities. The default cons25 console has the proper capabilities, but to
# display the colors in an xterm(1), for example, the TERM variable must be set to xterm-color.
# Other terminal types may require similar adjustments. Colorization is silently disabled if
# the output is not directed to a terminal unless the CLICOLOR_FORCE variable is defined.
export CLICOLOR=true
# The value of this variable describes what color to use for which
# attribute when colors are enabled with CLICOLOR. This string is a
# concatenation of pairs of the format fb, where f is the foreground color and b
# is the background color.
#
# The color designators are as follows:
#
# a black
# b red
# c green
# d brown
# e blue
# f magenta
# g cyan
# h light grey
# A bold black, usually shows up as dark grey
# B bold red
# C bold green
# D bold brown, usually shows up as yellow
# E bold blue
# F bold magenta
# G bold cyan
# H bold light grey; looks like bright white
# x default foreground or background
#
# Note that the above are standard ANSI colors. The actual display may
# differ depending on the color capabilities of the terminal in use.
#
# The order of the attributes are as follows:
#
# 1. directory
# 2. symbolic link
# 3. socket
# 4. pipe
# 5. executable
# 6. block special
# 7. character special
# 8. executable with setuid bit set
# 9. executable with setgid bit set
# 10. directory writable to others, with sticky bit
# 11. directory writable to others, without sticky bit
#
# The default is "exfxcxdxbxegedabagacad", i.e., blue foreground and default
# background for regular directories, black foreground and red background for
# setuid executables, etc.
export LSCOLORS="Gxfxcxdxdxegedabagacad"
else
if type -P dircolors >/dev/null
then
# TODO: Detect if alacritty is installed instead of using xterm-256color
# shellcheck disable=SC2046
eval $(TERM=xterm-256color dircolors --bourne-shell)
else
[[ "true" == "$VERBOSE" ]] && echo "dircolors not found"
[[ "true" == "$VERBOSE" ]] && echo "$PATH"
fi
# Enable color if available
if [[ -n ${LS_COLORS+x} ]]
then
alias ls='ls --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
fi
######################################################################
# starship
######################################################################
if type -P starship >/dev/null 2>&1; then
eval "$(starship init bash)"
else
######################################################################
# prompt.sh if starship is not installed
######################################################################
# ANSI colors
# http://wiki.archlinux.org/index.php/Color_Bash_Prompt
# shellcheck disable=SC2034
txtblk='\e[0;30m' # Black - Regular
# shellcheck disable=SC2034
txtred='\e[0;31m' # Red
# shellcheck disable=SC2034
txtgrn='\e[0;32m' # Green
# shellcheck disable=SC2034
txtylw='\e[0;33m' # Yellow
# shellcheck disable=SC2034
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
# shellcheck disable=SC2034
txtcyn='\e[0;36m' # Cyan
# shellcheck disable=SC2034
txtwht='\e[0;37m' # White
# shellcheck disable=SC2034
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
# shellcheck disable=SC2034
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
# shellcheck disable=SC2034
bldpur='\e[1;35m' # Purple
# shellcheck disable=SC2034
bldcyn='\e[1;36m' # Cyan
# shellcheck disable=SC2034
bldwht='\e[1;37m' # White
# shellcheck disable=SC2034
undblk='\e[4;30m' # Black - Underline
# shellcheck disable=SC2034
undred='\e[4;31m' # Red
# shellcheck disable=SC2034
undgrn='\e[4;32m' # Green
# shellcheck disable=SC2034
undylw='\e[4;33m' # Yellow
# shellcheck disable=SC2034
undblu='\e[4;34m' # Blue
# shellcheck disable=SC2034
undpur='\e[4;35m' # Purple
# shellcheck disable=SC2034
# shellcheck disable=SC2034
undcyn='\e[4;36m' # Cyan
# shellcheck disable=SC2034
undwht='\e[4;37m' # White
# shellcheck disable=SC2034
bakblk='\e[40m' # Black - Background
# shellcheck disable=SC2034
bakred='\e[41m' # Red
# shellcheck disable=SC2034
badgrn='\e[42m' # Green
# shellcheck disable=SC2034
bakylw='\e[43m' # Yellow
# shellcheck disable=SC2034
bakblu='\e[44m' # Blue
# shellcheck disable=SC2034
bakpur='\e[45m' # Purple
# shellcheck disable=SC2034
bakcyn='\e[46m' # Cyan
# shellcheck disable=SC2034
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset
# Nice prompt
UsernameColor="${bldgrn}"
if [[ "$EUID" -eq 0 ]]
then
UsernameColor="${bldred}"
fi
PS1="${debian_chroot:+($debian_chroot)}\[${txtpur}\]\t\[${txtrst}\] \[${UsernameColor}\]\u\[${txtrst}\]@\[${txtgrn}\]\H\[${txtrst}\] \[${bldblu}\]\w\[${txtrst}\] # "
fi
######################################################################
# ripgrep.sh
# https://github.com/BurntSushi/ripgrep/blob/master/FAQ.md#does-ripgrep-have-support-for-shell-auto-completion
######################################################################
[[ -s "${DOTFILES_DIR}/ripgreprc" ]] && export RIPGREP_CONFIG_PATH="${DOTFILES_DIR}/ripgreprc"
######################################################################
# ssh-agent.sh
######################################################################
# Use [Funtoo] Keychain to manage ssh-agent
if [[ $EUID -ne 0 ]] && tty >/dev/null
then
# This is after the cleanup so that it will start a new agent if the last agent was deleted
# The banner on STDERR is causing JetBrains Gateway to fail to deploy remote IDEs.
# https://youtrack.jetbrains.com/issue/CWM-6796
# shellcheck disable=SC2046
type -P keychain 1>/dev/null 2>&1 && eval $( keychain --eval --agents ssh 2>/dev/null )
if [[ "$(uname -s)" == "MINGW"* ]]; then
#
# https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases?platform=windows#auto-launching-ssh-agent-on-git-for-windows
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2=agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
agent_start
ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add
fi
unset env
fi
fi
######################################################################
# termcap.sh
######################################################################
# See https://russellparker.me/2018/02/23/adding-colors-to-man/
# Default to sane TERM if alacritty terminfo hasn't been installed.
if [[ "${TERM}" == "alacritty" ]] && ! infocmp alacritty >/dev/null 2>&1
then
export TERM=xterm-256color
fi
# The snap man page gets narrower as you page down. Disable this to make sure it's not
# interfering with the man page.
# I don't think this is causing the man page error.
# However, it is causing tput errors in containers.
if [[ "${TERM}" != "dumb" && "${TERM}" != "alacritty" ]]
then
LESS_TERMCAP_mb=$(tput bold; tput setaf 2)
LESS_TERMCAP_md=$(tput bold; tput setaf 6)
LESS_TERMCAP_me=$(tput sgr0)
LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4)
LESS_TERMCAP_se=$(tput rmso; tput sgr0)
LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 7)
LESS_TERMCAP_ue=$(tput rmul; tput sgr0)
LESS_TERMCAP_mr=$(tput rev)
LESS_TERMCAP_mh=$(tput dim)
export MANROFFOPT='-c'
export LESS_TERMCAP_mb
export LESS_TERMCAP_md
export LESS_TERMCAP_me
export LESS_TERMCAP_so
export LESS_TERMCAP_se
export LESS_TERMCAP_us
export LESS_TERMCAP_ue
export LESS_TERMCAP_mr
export LESS_TERMCAP_mh
fi
######################################################################
# Key Bindings
######################################################################
# Only source files for executables that exist.
# FIXME: fzf key bindings override atuin key bindings for Ctrl-R for reverse history search.
# Source fzf first (here) before initializing atuin (below).
if compgen -G "${DOTFILES_DIR}/key-bindings/*.bash" > /dev/null
then
for f in "${DOTFILES_DIR}"/key-bindings/*.bash
do
filename="${f##*/}"
bin="${filename%.bash}"
if [[ -n $BASH_VERSION ]] && type -P "${bin}" >/dev/null 2>&1
then
#echo "Sourcing key bindings: ${f}"
# shellcheck disable=SC1090
source "${f}"
fi
done
fi
######################################################################
# Bash Completions
######################################################################
# Only source files for executables that exist.
if compgen -G "${DOTFILES_DIR}/bash-completion/*.bash" > /dev/null
then
for f in "${DOTFILES_DIR}"/bash-completion/*.bash
do
filename="${f##*/}"
bin="${filename%.bash}"
if [[ -n $BASH_VERSION ]] && type -P "${bin}" >/dev/null 2>&1
then
#echo "Sourcing completion: ${f}"
# shellcheck disable=SC1090
source "${f}"
fi
done
fi
######################################################################
# Finish
######################################################################
# Prevent bashrc from being read twice
export BASHRCREAD=true