.bashrc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. # For ranger, htop and other console progs in Qtile ---------------------------
  2. unset COLUMNS
  3. unset LINES
  4. man() {
  5. command man "$@" | eval ${MANPAGER}
  6. }
  7. ###############################################################################
  8. # EXPORTs
  9. ###############################################################################
  10. export HOME=$(echo /home/$USER)
  11. export GHCUP_INSTALL_BASE_PREFIX="$HOME/.config" # for GHCUP
  12. export GOPATH="$HOME/go"
  13. # export
  14. PATH=$PATH:$HOME/.local/bin:$HOME/.cargo/bin:$HOME/.config/vifm/scripts:$HOME/Programs/AppImageApplications:$GHCUP_INSTALL_BASE_PREFIX/.ghcup/bin:$HOME/Programs/Android_SDK/platform-tools:$GOPATH/bin
  15. export EDITOR="vim" # vim is either a link to nvim or just vim
  16. export VISUAL="gvim" # gvim is either a link to nvim-qt or just gvim
  17. export TERM="xterm-256color"
  18. export TERMINAL="alacritty"
  19. export BROWSER="brave"
  20. export PAGER="bat"
  21. export MANPAGER="sh -c 'col -bx | bat -l man -p'" # theme moved to the .config/bat/config
  22. # export MANPAGER="bat man -p'" # $MANPAGER use batcat to read mans
  23. export RANGER_LOAD_DEFAULT_RC=FALSE # to avoid loading ranger's config twice
  24. export ANDROID_SDK="$HOME/Programs/Android_SDK"
  25. export XDG_CONFIG_HOME="$HOME/.config"
  26. export XDG_DATA_HOME="$HOME/.local/share"
  27. # export XDG_DATA_DIRS="$HOME/.local/share/flatpak/exports/share"
  28. # export XDG_DATA_DIRS+=":/var/lib/flatpak/exports/share"
  29. # export XDG_DATA_DIRS+=":$HOME/.local/share/flatpak/exports/share/applications"
  30. # export XDG_DATA_DIRS+=":/var/lib/flatpak/exports/share/applications"
  31. export XDG_CACHE_HOME="$HOME/.cache"
  32. # If not running interactively, don't do anything -----------------------------
  33. case $- in
  34. *i*) ;;
  35. *) return;;
  36. esac
  37. ###############################################################################
  38. # HIST...
  39. ###############################################################################
  40. # don't put duplicate lines or lines starting with space in the history ------
  41. # See bash(1) for more options
  42. HISTCONTROL=ignoreboth
  43. # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) ---------
  44. HISTSIZE=1000
  45. HISTFILESIZE=2000
  46. ###############################################################################
  47. # shopt
  48. ###############################################################################
  49. # append to the history file, don't overwrite it ------------------------------
  50. shopt -s histappend
  51. # check the window size after each command and, if necessary, -----------------
  52. # update the values of LINES and COLUMNS.
  53. shopt -s checkwinsize
  54. # If set, the pattern "**" used in a pathname expansion context will ----------
  55. # match all files and zero or more directories and subdirectories.
  56. # shopt -s globstar
  57. # enable programmable completion features (you don't need to enable -----------
  58. # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
  59. # sources /etc/bash.bashrc).
  60. if ! shopt -oq posix; then
  61. if [ -f /usr/share/bash-completion/bash_completion ]; then
  62. . /usr/share/bash-completion/bash_completion
  63. elif [ -f /etc/bash_completion ]; then
  64. . /etc/bash_completion
  65. fi
  66. fi
  67. ###############################################################################
  68. # prompt
  69. ###############################################################################
  70. # set variable identifying the chroot you work in (used in the prompt below) --
  71. if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
  72. debian_chroot=$(cat /etc/debian_chroot)
  73. fi
  74. # set a fancy prompt (non-color, unless we know we "want" color) --------------
  75. case "$TERM" in
  76. xterm-color) color_prompt=yes;;
  77. esac
  78. # uncomment for a colored prompt, if the terminal has the capability; turned --
  79. # off by default to not distract the user: the focus in a terminal window
  80. # should be on the output of commands, not on the prompt
  81. # force_color_prompt=yes
  82. if [ -n "$force_color_prompt" ]; then
  83. if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
  84. # We have color support; assume it's compliant with Ecma-48
  85. # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
  86. # a case would tend to support setf rather than setaf.)
  87. color_prompt=yes
  88. else
  89. color_prompt=
  90. fi
  91. fi
  92. unset color_prompt force_color_prompt
  93. # If this is an xterm set the title to user@host:dir --------------------------
  94. case "$TERM" in
  95. xterm*|rxvt*)
  96. PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
  97. ;;
  98. *)
  99. ;;
  100. esac
  101. GIT_PS1_SHOWDIRTYSTATE=true
  102. GIT_PS1_SHOWUNTRACKEDFILES=true
  103. GIT_PS1_SHOWCOLORHINTS=true
  104. # export PS1='\w$(__git_ps1 " (%s)")\$ '
  105. # PS1='\n \[\e[01;33m\]\u\[\e[01;37m\]@\[\e[01;32m\]\h:\[\e[01;34m\]\w\[\e[01;31m\]$(__git_ps1 " (%s)")\[\e[01;33m\]\n >_ \[\e[2m\]'
  106. PS1='\n \[\e[01;33m\]\u\[\e[01;37m\]@\[\e[01;32m\]\h:\[\e[01;34m\]\w\[\e[01;31m\]$(__git_ps1 " (%s)")\[\e[01;33m\]\n >_ \[\e[0m\]'
  107. ###############################################################################
  108. # ALIASES
  109. ###############################################################################
  110. # enable color support of ls and also add handy aliases -----------------------
  111. if [ -x /usr/bin/dircolors ]; then
  112. test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
  113. fi
  114. # Alias definitions -----------------------------------------------------------
  115. # You may want to put all your additions into a separate file like
  116. # ~/.bash_aliases, instead of adding them here directly.
  117. # See /usr/share/doc/bash-doc/examples in the bash-doc package.
  118. if [ -f ~/.bash_aliases ]; then
  119. . ~/.bash_aliases
  120. fi
  121. # alias ls='ls --color=auto'
  122. # alias ll='ls -l'
  123. # alias ll='ls -lh'
  124. # alias la='ls -la'
  125. # alias la='ls -lah'
  126. # alias lf='ls -lFh'
  127. alias lse='exa -g --color=always --group-directories-first'
  128. alias lle='lse -l'
  129. alias ls='lsd --group-dirs=first'
  130. alias ll='lsd --blocks=permission,links,user,group,size,date,name --group-dirs=first --date="+%d %b %H:%M"'
  131. alias la='ll -a'
  132. alias tree='lsd --tree'
  133. # alias bat='bat --theme gruvbox-dark' # theme moved to the .config/bat/config
  134. alias grep='grep --color=auto'
  135. alias fgrep='fgrep --color=auto'
  136. alias egrep='egrep --color=auto'
  137. alias ifconfig=/sbin/ifconfig
  138. # confirm before overwriting something ----------------------------------------
  139. alias cp="cp -i"
  140. alias mv='mv -i'
  141. alias rm='rm -i'
  142. # switch between shells -------------------------------------------------------
  143. alias tobash="sudo chsh $USER -s /usr/bin/env bash && echo 'Now log out.'"
  144. alias tozsh="sudo chsh $USER -s /usr/bin/env zsh && echo 'Now log out.'"
  145. alias tofish="sudo chsh $USER -s /usr/bin/env fish && echo 'Now log out.'"
  146. # navigation ------------------------------------------------------------------
  147. alias ..='cd ..'
  148. alias ...='cd ../..'
  149. alias .3='cd ../../..'
  150. alias .4='cd ../../../..'
  151. alias .5='cd ../../../../..'
  152. # get top process eating memory -----------------------------------------------
  153. alias psmem='ps auxf | sort -nr -k 4'
  154. alias psmem10='ps auxf | sort -nr -k 4 | head -10'
  155. # get top process eating cpu --------------------------------------------------
  156. alias pscpu='ps auxf | sort -nr -k 3'
  157. alias pscpu10='ps auxf | sort -nr -k 3 | head -10'
  158. # git -------------------------------------------------------------------------
  159. alias config='/usr/bin/git --git-dir=$HOME/.dotfiles.git --work-tree=$HOME'
  160. # run some programs -----------------------------------------------------------
  161. alias v='vim'
  162. alias f='ranger'
  163. alias vf='vifm'
  164. alias emacs="emacsclient -c -a 'emacs'"
  165. # poetry python (activate environment) ------------------------------------------------------------------
  166. alias ap="source \"\$(poetry env info --path)/bin/activate\""
  167. ###############################################################################
  168. # Source
  169. ###############################################################################
  170. # git -------------------------------------------------------------------------
  171. source ~/.git-completion.bash
  172. source ~/.git-prompt.sh
  173. # asdf manager ----------------------------------------------------------------
  174. # source $HOME/.asdf/asdf.sh
  175. # source $HOME/.asdf/completions/asdf.bash
  176. # Fuzzy finder ----------------------------------------------------------------
  177. source /usr/share/fzf/completion.bash
  178. source /usr/share/fzf/key-bindings.bash
  179. # [ -f ~/.fzf.bash ] && source ~/.fzf.bash
  180. # # # broot -----------------------------------------------------------------------
  181. # # source /home/alexander/.config/broot/launcher/bash/br
  182. # Run neofetch ----------------------------------------------------------------
  183. neofetch
  184. # ### key-bindings.bash ###
  185. # # ____ ____
  186. # # / __/___ / __/
  187. # # / /_/_ / / /_
  188. # # / __/ / /_/ __/
  189. # # /_/ /___/_/ key-bindings.bash
  190. # #
  191. # # - $FZF_TMUX_OPTS
  192. # # - $FZF_CTRL_T_COMMAND
  193. # # - $FZF_CTRL_T_OPTS
  194. # # - $FZF_CTRL_R_OPTS
  195. # # - $FZF_ALT_C_COMMAND
  196. # # - $FZF_ALT_C_OPTS
  197. #
  198. # if [[ $- =~ i ]]; then
  199. #
  200. #
  201. # # Key bindings
  202. # # ------------
  203. #
  204. # __fzf_defaults() {
  205. # # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  206. # # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  207. # echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
  208. # command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
  209. # echo "${FZF_DEFAULT_OPTS-} $2"
  210. # }
  211. #
  212. # __fzf_select__() {
  213. # FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \
  214. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \
  215. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" |
  216. # while read -r item; do
  217. # printf '%q ' "$item" # escape special chars
  218. # done
  219. # }
  220. #
  221. # __fzfcmd() {
  222. # [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; } &&
  223. # echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
  224. # }
  225. #
  226. # fzf-file-widget() {
  227. # local selected="$(__fzf_select__ "$@")"
  228. # READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
  229. # READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
  230. # }
  231. #
  232. # __fzf_cd__() {
  233. # local dir
  234. # dir=$(
  235. # FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \
  236. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \
  237. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd)
  238. # ) && printf 'builtin cd -- %q' "$(builtin unset CDPATH && builtin cd -- "$dir" && builtin pwd)"
  239. # }
  240. #
  241. # if command -v perl > /dev/null; then
  242. # __fzf_history__() {
  243. # local output script
  244. # script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; s/\n/\n\t/gm; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++'
  245. # output=$(
  246. # set +o pipefail
  247. # builtin fc -lnr -2147483648 |
  248. # last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" |
  249. # FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign '"$'\t'"↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} +m --read0") \
  250. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"
  251. # ) || return
  252. # READLINE_LINE=$(command perl -pe 's/^\d*\t//' <<< "$output")
  253. # if [[ -z "$READLINE_POINT" ]]; then
  254. # echo "$READLINE_LINE"
  255. # else
  256. # READLINE_POINT=0x7fffffff
  257. # fi
  258. # }
  259. # else # awk - fallback for POSIX systems
  260. # __fzf_history__() {
  261. # local output script n x y z d
  262. # if [[ -z $__fzf_awk ]]; then
  263. # __fzf_awk=awk
  264. # # choose the faster mawk if: it's installed && build date >= 20230322 && version >= 1.3.4
  265. # IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)
  266. # [[ $n == mawk ]] && (( d >= 20230302 && (x *1000 +y) *1000 +z >= 1003004 )) && __fzf_awk=mawk
  267. # fi
  268. # [[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries
  269. # script='function P(b) { ++n; sub(/^[ *]/, "", b); if (!seen[b]++) { printf "%d\t%s%c", '$((BASH_REMATCH + 1))' - n, b, 0 } }
  270. # NR==1 { b = substr($0, 2); next }
  271. # /^\t/ { P(b); b = substr($0, 2); next }
  272. # { b = b RS $0 }
  273. # END { if (NR) P(b) }'
  274. # output=$(
  275. # set +o pipefail
  276. # builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '<lines>$'\n' )* ; <lines> ::= [^\n]* ( $'\n'<lines> )*
  277. # command $__fzf_awk "$script" | # ( <counter>$'\t'<lines>$'\000' )*
  278. # FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign '"$'\t'"↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} +m --read0") \
  279. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"
  280. # ) || return
  281. # READLINE_LINE=${output#*$'\t'}
  282. # if [[ -z "$READLINE_POINT" ]]; then
  283. # echo "$READLINE_LINE"
  284. # else
  285. # READLINE_POINT=0x7fffffff
  286. # fi
  287. # }
  288. # fi
  289. #
  290. # # Required to refresh the prompt after fzf
  291. # bind -m emacs-standard '"\er": redraw-current-line'
  292. #
  293. # bind -m vi-command '"\C-z": emacs-editing-mode'
  294. # bind -m vi-insert '"\C-z": emacs-editing-mode'
  295. # bind -m emacs-standard '"\C-z": vi-editing-mode'
  296. #
  297. # if (( BASH_VERSINFO[0] < 4 )); then
  298. # # CTRL-T - Paste the selected file path into the command line
  299. # if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then
  300. # bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\er\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'
  301. # bind -m vi-command '"\C-t": "\C-z\C-t\C-z"'
  302. # bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'
  303. # fi
  304. #
  305. # # CTRL-R - Paste the selected command from history into the command line
  306. # bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er"'
  307. # bind -m vi-command '"\C-r": "\C-z\C-r\C-z"'
  308. # bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'
  309. # else
  310. # # CTRL-T - Paste the selected file path into the command line
  311. # if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then
  312. # bind -m emacs-standard -x '"\C-t": fzf-file-widget'
  313. # bind -m vi-command -x '"\C-t": fzf-file-widget'
  314. # bind -m vi-insert -x '"\C-t": fzf-file-widget'
  315. # fi
  316. #
  317. # # CTRL-R - Paste the selected command from history into the command line
  318. # bind -m emacs-standard -x '"\C-r": __fzf_history__'
  319. # bind -m vi-command -x '"\C-r": __fzf_history__'
  320. # bind -m vi-insert -x '"\C-r": __fzf_history__'
  321. # fi
  322. #
  323. # # ALT-C - cd into the selected directory
  324. # if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then
  325. # bind -m emacs-standard '"\ec": " \C-b\C-k \C-u`__fzf_cd__`\e\C-e\er\C-m\C-y\C-h\e \C-y\ey\C-x\C-x\C-d"'
  326. # bind -m vi-command '"\ec": "\C-z\ec\C-z"'
  327. # bind -m vi-insert '"\ec": "\C-z\ec\C-z"'
  328. # fi
  329. #
  330. # fi
  331. # ### end: key-bindings.bash ###
  332. # ### completion.bash ###
  333. # # ____ ____
  334. # # / __/___ / __/
  335. # # / /_/_ / / /_
  336. # # / __/ / /_/ __/
  337. # # /_/ /___/_/ completion.bash
  338. # #
  339. # # - $FZF_TMUX (default: 0)
  340. # # - $FZF_TMUX_OPTS (default: empty)
  341. # # - $FZF_COMPLETION_TRIGGER (default: '**')
  342. # # - $FZF_COMPLETION_OPTS (default: empty)
  343. # # - $FZF_COMPLETION_PATH_OPTS (default: empty)
  344. # # - $FZF_COMPLETION_DIR_OPTS (default: empty)
  345. #
  346. # if [[ $- =~ i ]]; then
  347. #
  348. #
  349. # # To use custom commands instead of find, override _fzf_compgen_{path,dir}
  350. # #
  351. # # _fzf_compgen_path() {
  352. # # echo "$1"
  353. # # command find -L "$1" \
  354. # # -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \
  355. # # -a -not -path "$1" -print 2> /dev/null | command sed 's@^\./@@'
  356. # # }
  357. # #
  358. # # _fzf_compgen_dir() {
  359. # # command find -L "$1" \
  360. # # -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \
  361. # # -a -not -path "$1" -print 2> /dev/null | command sed 's@^\./@@'
  362. # # }
  363. #
  364. # ###########################################################
  365. #
  366. # # To redraw line after fzf closes (printf '\e[5n')
  367. # bind '"\e[0n": redraw-current-line' 2> /dev/null
  368. #
  369. # __fzf_defaults() {
  370. # # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  371. # # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  372. # echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
  373. # command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
  374. # echo "${FZF_DEFAULT_OPTS-} $2"
  375. # }
  376. #
  377. # __fzf_comprun() {
  378. # if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then
  379. # _fzf_comprun "$@"
  380. # elif [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; }; then
  381. # shift
  382. # fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@"
  383. # else
  384. # shift
  385. # fzf "$@"
  386. # fi
  387. # }
  388. #
  389. # __fzf_orig_completion() {
  390. # local l comp f cmd
  391. # while read -r l; do
  392. # if [[ "$l" =~ ^(.*\ -F)\ *([^ ]*).*\ ([^ ]*)$ ]]; then
  393. # comp="${BASH_REMATCH[1]}"
  394. # f="${BASH_REMATCH[2]}"
  395. # cmd="${BASH_REMATCH[3]}"
  396. # [[ "$f" = _fzf_* ]] && continue
  397. # printf -v "_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}" "%s" "${comp} %s ${cmd} #${f}"
  398. # if [[ "$l" = *" -o nospace "* ]] && [[ ! "${__fzf_nospace_commands-}" = *" $cmd "* ]]; then
  399. # __fzf_nospace_commands="${__fzf_nospace_commands-} $cmd "
  400. # fi
  401. # fi
  402. # done
  403. # }
  404. #
  405. # # @param $1 cmd - Command name for which the original completion is searched
  406. # # @var[out] REPLY - Original function name is returned
  407. # __fzf_orig_completion_get_orig_func() {
  408. # local cmd orig_var orig
  409. # cmd=$1
  410. # orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"
  411. # orig="${!orig_var-}"
  412. # REPLY="${orig##*#}"
  413. # [[ $REPLY ]] && type "$REPLY" &> /dev/null
  414. # }
  415. #
  416. # # @param $1 cmd - Command name for which the original completion is searched
  417. # # @param $2 func - Fzf's completion function to replace the original function
  418. # # @var[out] REPLY - Completion setting is returned as a string to "eval"
  419. # __fzf_orig_completion_instantiate() {
  420. # local cmd func orig_var orig
  421. # cmd=$1
  422. # func=$2
  423. # orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"
  424. # orig="${!orig_var-}"
  425. # orig="${orig%#*}"
  426. # [[ $orig == *' %s '* ]] || return 1
  427. # printf -v REPLY "$orig" "$func"
  428. # }
  429. #
  430. # _fzf_opts_completion() {
  431. # local cur prev opts
  432. # COMPREPLY=()
  433. # cur="${COMP_WORDS[COMP_CWORD]}"
  434. # prev="${COMP_WORDS[COMP_CWORD-1]}"
  435. # opts="
  436. # +c --no-color
  437. # +i --no-ignore-case
  438. # +s --no-sort
  439. # +x --no-extended
  440. # --ansi
  441. # --bash
  442. # --bind
  443. # --border
  444. # --border-label
  445. # --border-label-pos
  446. # --color
  447. # --cycle
  448. # --disabled
  449. # --ellipsis
  450. # --expect
  451. # --filepath-word
  452. # --fish
  453. # --header
  454. # --header-first
  455. # --header-lines
  456. # --height
  457. # --highlight-line
  458. # --history
  459. # --history-size
  460. # --hscroll-off
  461. # --info
  462. # --jump-labels
  463. # --keep-right
  464. # --layout
  465. # --listen
  466. # --listen-unsafe
  467. # --literal
  468. # --man
  469. # --margin
  470. # --marker
  471. # --min-height
  472. # --no-bold
  473. # --no-clear
  474. # --no-hscroll
  475. # --no-mouse
  476. # --no-scrollbar
  477. # --no-separator
  478. # --no-unicode
  479. # --padding
  480. # --pointer
  481. # --preview
  482. # --preview-label
  483. # --preview-label-pos
  484. # --preview-window
  485. # --print-query
  486. # --print0
  487. # --prompt
  488. # --read0
  489. # --reverse
  490. # --scheme
  491. # --scroll-off
  492. # --separator
  493. # --sync
  494. # --tabstop
  495. # --tac
  496. # --tiebreak
  497. # --tmux
  498. # --track
  499. # --version
  500. # --with-nth
  501. # --with-shell
  502. # --wrap
  503. # --zsh
  504. # -0 --exit-0
  505. # -1 --select-1
  506. # -d --delimiter
  507. # -e --exact
  508. # -f --filter
  509. # -h --help
  510. # -i --ignore-case
  511. # -m --multi
  512. # -n --nth
  513. # -q --query
  514. # --"
  515. #
  516. # case "${prev}" in
  517. # --scheme)
  518. # COMPREPLY=( $(compgen -W "default path history" -- "$cur") )
  519. # return 0
  520. # ;;
  521. # --tiebreak)
  522. # COMPREPLY=( $(compgen -W "length chunk begin end index" -- "$cur") )
  523. # return 0
  524. # ;;
  525. # --color)
  526. # COMPREPLY=( $(compgen -W "dark light 16 bw no" -- "$cur") )
  527. # return 0
  528. # ;;
  529. # --layout)
  530. # COMPREPLY=( $(compgen -W "default reverse reverse-list" -- "$cur") )
  531. # return 0
  532. # ;;
  533. # --info)
  534. # COMPREPLY=( $(compgen -W "default right hidden inline inline-right" -- "$cur") )
  535. # return 0
  536. # ;;
  537. # --preview-window)
  538. # COMPREPLY=( $(compgen -W "
  539. # default
  540. # hidden
  541. # nohidden
  542. # wrap
  543. # nowrap
  544. # cycle
  545. # nocycle
  546. # up top
  547. # down bottom
  548. # left
  549. # right
  550. # rounded border border-rounded
  551. # sharp border-sharp
  552. # border-bold
  553. # border-block
  554. # border-thinblock
  555. # border-double
  556. # noborder border-none
  557. # border-horizontal
  558. # border-vertical
  559. # border-up border-top
  560. # border-down border-bottom
  561. # border-left
  562. # border-right
  563. # follow
  564. # nofollow" -- "$cur") )
  565. # return 0
  566. # ;;
  567. # --border)
  568. # COMPREPLY=( $(compgen -W "rounded sharp bold block thinblock double horizontal vertical top bottom left right none" -- "$cur") )
  569. # return 0
  570. # ;;
  571. # --border-label-pos|--preview-label-pos)
  572. # COMPREPLY=( $(compgen -W "center bottom top" -- "$cur") )
  573. # return 0
  574. # ;;
  575. # esac
  576. #
  577. # if [[ "$cur" =~ ^-|\+ ]]; then
  578. # COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )
  579. # return 0
  580. # fi
  581. #
  582. # return 0
  583. # }
  584. #
  585. # _fzf_handle_dynamic_completion() {
  586. # local cmd ret REPLY orig_cmd orig_complete
  587. # cmd="$1"
  588. # shift
  589. # orig_cmd="$1"
  590. # if __fzf_orig_completion_get_orig_func "$cmd"; then
  591. # "$REPLY" "$@"
  592. # elif [[ -n "${_fzf_completion_loader-}" ]]; then
  593. # orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)
  594. # $_fzf_completion_loader "$@"
  595. # ret=$?
  596. # # _completion_loader may not have updated completion for the command
  597. # if [[ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]]; then
  598. # __fzf_orig_completion < <(complete -p "$orig_cmd" 2> /dev/null)
  599. #
  600. # # Update orig_complete by _fzf_orig_completion entry
  601. # [[ $orig_complete =~ ' -F '(_fzf_[^ ]+)' ' ]] &&
  602. # __fzf_orig_completion_instantiate "$cmd" "${BASH_REMATCH[1]}" &&
  603. # orig_complete=$REPLY
  604. #
  605. # if [[ "${__fzf_nospace_commands-}" = *" $orig_cmd "* ]]; then
  606. # eval "${orig_complete/ -F / -o nospace -F }"
  607. # else
  608. # eval "$orig_complete"
  609. # fi
  610. # fi
  611. # [[ $ret -eq 0 ]] && return 124
  612. # return $ret
  613. # fi
  614. # }
  615. #
  616. # __fzf_generic_path_completion() {
  617. # local cur base dir leftover matches trigger cmd
  618. # cmd="${COMP_WORDS[0]}"
  619. # if [[ $cmd == \\* ]]; then
  620. # cmd="${cmd:1}"
  621. # fi
  622. # COMPREPLY=()
  623. # trigger=${FZF_COMPLETION_TRIGGER-'**'}
  624. # cur="${COMP_WORDS[COMP_CWORD]}"
  625. # if [[ "$cur" == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *':='* ]] && [[ $cur != *'`'* ]]; then
  626. # base=${cur:0:${#cur}-${#trigger}}
  627. # eval "base=$base" 2> /dev/null || return
  628. #
  629. # dir=
  630. # [[ $base = *"/"* ]] && dir="$base"
  631. # while true; do
  632. # if [[ -z "$dir" ]] || [[ -d "$dir" ]]; then
  633. # leftover=${base/#"$dir"}
  634. # leftover=${leftover/#\/}
  635. # [[ -z "$dir" ]] && dir='.'
  636. # [[ "$dir" != "/" ]] && dir="${dir/%\//}"
  637. # matches=$(
  638. # export FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --scheme=path" "${FZF_COMPLETION_OPTS-} $2")
  639. # unset FZF_DEFAULT_COMMAND FZF_DEFAULT_OPTS_FILE
  640. # if declare -F "$1" > /dev/null; then
  641. # eval "$1 $(printf %q "$dir")" | __fzf_comprun "$4" -q "$leftover"
  642. # else
  643. # if [[ $1 =~ dir ]]; then
  644. # walker=dir,follow
  645. # rest=${FZF_COMPLETION_DIR_OPTS-}
  646. # else
  647. # walker=file,dir,follow,hidden
  648. # rest=${FZF_COMPLETION_PATH_OPTS-}
  649. # fi
  650. # __fzf_comprun "$4" -q "$leftover" --walker "$walker" --walker-root="$dir" $rest
  651. # fi | while read -r item; do
  652. # printf "%q " "${item%$3}$3"
  653. # done
  654. # )
  655. # matches=${matches% }
  656. # [[ -z "$3" ]] && [[ "${__fzf_nospace_commands-}" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "
  657. # if [[ -n "$matches" ]]; then
  658. # COMPREPLY=( "$matches" )
  659. # else
  660. # COMPREPLY=( "$cur" )
  661. # fi
  662. # printf '\e[5n'
  663. # return 0
  664. # fi
  665. # dir=$(command dirname "$dir")
  666. # [[ "$dir" =~ /$ ]] || dir="$dir"/
  667. # done
  668. # else
  669. # shift
  670. # shift
  671. # shift
  672. # _fzf_handle_dynamic_completion "$cmd" "$@"
  673. # fi
  674. # }
  675. #
  676. # _fzf_complete() {
  677. # # Split arguments around --
  678. # local args rest str_arg i sep
  679. # args=("$@")
  680. # sep=
  681. # for i in "${!args[@]}"; do
  682. # if [[ "${args[$i]}" = -- ]]; then
  683. # sep=$i
  684. # break
  685. # fi
  686. # done
  687. # if [[ -n "$sep" ]]; then
  688. # str_arg=
  689. # rest=("${args[@]:$((sep + 1)):${#args[@]}}")
  690. # args=("${args[@]:0:$sep}")
  691. # else
  692. # str_arg=$1
  693. # args=()
  694. # shift
  695. # rest=("$@")
  696. # fi
  697. #
  698. # local cur selected trigger cmd post
  699. # post="$(caller 0 | command awk '{print $2}')_post"
  700. # type -t "$post" > /dev/null 2>&1 || post='command cat'
  701. #
  702. # trigger=${FZF_COMPLETION_TRIGGER-'**'}
  703. # cmd="${COMP_WORDS[0]}"
  704. # cur="${COMP_WORDS[COMP_CWORD]}"
  705. # if [[ "$cur" == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *':='* ]] && [[ $cur != *'`'* ]]; then
  706. # cur=${cur:0:${#cur}-${#trigger}}
  707. #
  708. # selected=$(
  709. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse" "${FZF_COMPLETION_OPTS-} $str_arg") \
  710. # FZF_DEFAULT_OPTS_FILE='' \
  711. # __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | $post | command tr '\n' ' ')
  712. # selected=${selected% } # Strip trailing space not to repeat "-o nospace"
  713. # if [[ -n "$selected" ]]; then
  714. # COMPREPLY=("$selected")
  715. # else
  716. # COMPREPLY=("$cur")
  717. # fi
  718. # printf '\e[5n'
  719. # return 0
  720. # else
  721. # _fzf_handle_dynamic_completion "$cmd" "${rest[@]}"
  722. # fi
  723. # }
  724. #
  725. # _fzf_path_completion() {
  726. # __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"
  727. # }
  728. #
  729. # # Deprecated. No file only completion.
  730. # _fzf_file_completion() {
  731. # _fzf_path_completion "$@"
  732. # }
  733. #
  734. # _fzf_dir_completion() {
  735. # __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"
  736. # }
  737. #
  738. # _fzf_complete_kill() {
  739. # _fzf_proc_completion "$@"
  740. # }
  741. #
  742. # _fzf_proc_completion() {
  743. # _fzf_complete -m --header-lines=1 --no-preview --wrap -- "$@" < <(
  744. # command ps -eo user,pid,ppid,start,time,command 2> /dev/null ||
  745. # command ps -eo user,pid,ppid,time,args # For BusyBox
  746. # )
  747. # }
  748. #
  749. # _fzf_proc_completion_post() {
  750. # command awk '{print $2}'
  751. # }
  752. #
  753. # # To use custom hostname lists, override __fzf_list_hosts.
  754. # # The function is expected to print hostnames, one per line as well as in the
  755. # # desired sorting and with any duplicates removed, to standard output.
  756. # #
  757. # # e.g.
  758. # # # Use bash-completions’s _known_hosts_real() for getting the list of hosts
  759. # # __fzf_list_hosts() {
  760. # # # Set the local attribute for any non-local variable that is set by _known_hosts_real()
  761. # # local COMPREPLY=()
  762. # # _known_hosts_real ''
  763. # # printf '%s\n' "${COMPREPLY[@]}" | command sort -u --version-sort
  764. # # }
  765. # if ! declare -F __fzf_list_hosts > /dev/null; then
  766. # __fzf_list_hosts() {
  767. # command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | command awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \
  768. # <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | command tr ',' '\n' | command tr -d '[' | command awk '{ print $1 " " $1 }') \
  769. # <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0' | command sed 's/#.*//') |
  770. # command awk '{for (i = 2; i <= NF; i++) print $i}' | command sort -u
  771. # }
  772. # fi
  773. #
  774. # _fzf_host_completion() {
  775. # _fzf_complete +m -- "$@" < <(__fzf_list_hosts)
  776. # }
  777. #
  778. # # Values for $1 $2 $3 are described here
  779. # # https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html
  780. # # > the first argument ($1) is the name of the command whose arguments are being completed,
  781. # # > the second argument ($2) is the word being completed,
  782. # # > and the third argument ($3) is the word preceding the word being completed on the current command line.
  783. # _fzf_complete_ssh() {
  784. # case $3 in
  785. # -i|-F|-E)
  786. # _fzf_path_completion "$@"
  787. # ;;
  788. # *)
  789. # local user=
  790. # [[ "$2" =~ '@' ]] && user="${2%%@*}@"
  791. # _fzf_complete +m -- "$@" < <(__fzf_list_hosts | command awk -v user="$user" '{print user $0}')
  792. # ;;
  793. # esac
  794. # }
  795. #
  796. # _fzf_var_completion() {
  797. # _fzf_complete -m -- "$@" < <(
  798. # declare -xp | command sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p'
  799. # )
  800. # }
  801. #
  802. # _fzf_alias_completion() {
  803. # _fzf_complete -m -- "$@" < <(
  804. # alias | command sed -En 's|^alias ([^=]+).*|\1|p'
  805. # )
  806. # }
  807. #
  808. # # fzf options
  809. # complete -o default -F _fzf_opts_completion fzf
  810. # # fzf-tmux is a thin fzf wrapper that has only a few more options than fzf
  811. # # itself. As a quick improvement we take fzf's completion. Adding the few extra
  812. # # fzf-tmux specific options (like `-w WIDTH`) are left as a future patch.
  813. # complete -o default -F _fzf_opts_completion fzf-tmux
  814. #
  815. # d_cmds="${FZF_COMPLETION_DIR_COMMANDS-cd pushd rmdir}"
  816. #
  817. # # NOTE: $FZF_COMPLETION_PATH_COMMANDS and $FZF_COMPLETION_VAR_COMMANDS are
  818. # # undocumented and subject to change in the future.
  819. # a_cmds="${FZF_COMPLETION_PATH_COMMANDS-"
  820. # awk bat cat code diff diff3
  821. # emacs emacsclient ex file ftp g++ gcc gvim head hg hx java
  822. # javac ld less more mvim nvim patch perl python ruby
  823. # sed sftp sort source tail tee uniq vi view vim wc xdg-open
  824. # basename bunzip2 bzip2 chmod chown curl cp dirname du
  825. # find git grep gunzip gzip hg jar
  826. # ln ls mv open rm rsync scp
  827. # svn tar unzip zip"}"
  828. # v_cmds="${FZF_COMPLETION_VAR_COMMANDS-export unset printenv}"
  829. #
  830. # # Preserve existing completion
  831. # __fzf_orig_completion < <(complete -p $d_cmds $a_cmds $v_cmds unalias kill ssh 2> /dev/null)
  832. #
  833. # if type _comp_load > /dev/null 2>&1; then
  834. # # _comp_load was added in bash-completion 2.12 to replace _completion_loader.
  835. # # We use it without -D option so that it does not use _comp_complete_minimal as the fallback.
  836. # _fzf_completion_loader=_comp_load
  837. # elif type __load_completion > /dev/null 2>&1; then
  838. # # In bash-completion 2.11, _completion_loader internally calls __load_completion
  839. # # and if it returns a non-zero status, it sets the default 'minimal' completion.
  840. # _fzf_completion_loader=__load_completion
  841. # elif type _completion_loader > /dev/null 2>&1; then
  842. # _fzf_completion_loader=_completion_loader
  843. # fi
  844. #
  845. # __fzf_defc() {
  846. # local cmd func opts REPLY
  847. # cmd="$1"
  848. # func="$2"
  849. # opts="$3"
  850. # if __fzf_orig_completion_instantiate "$cmd" "$func"; then
  851. # eval "$REPLY"
  852. # else
  853. # complete -F "$func" $opts "$cmd"
  854. # fi
  855. # }
  856. #
  857. # # Anything
  858. # for cmd in $a_cmds; do
  859. # __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"
  860. # done
  861. #
  862. # # Directory
  863. # for cmd in $d_cmds; do
  864. # __fzf_defc "$cmd" _fzf_dir_completion "-o bashdefault -o nospace -o dirnames"
  865. # done
  866. #
  867. # # Variables
  868. # for cmd in $v_cmds; do
  869. # __fzf_defc "$cmd" _fzf_var_completion "-o default -o nospace -v"
  870. # done
  871. #
  872. # # Aliases
  873. # __fzf_defc unalias _fzf_alias_completion "-a"
  874. #
  875. # # Processes
  876. # __fzf_defc kill _fzf_proc_completion "-o default -o bashdefault"
  877. #
  878. # # ssh
  879. # __fzf_defc ssh _fzf_complete_ssh "-o default -o bashdefault"
  880. #
  881. # unset cmd d_cmds a_cmds v_cmds
  882. #
  883. # _fzf_setup_completion() {
  884. # local kind fn cmd
  885. # kind=$1
  886. # fn=_fzf_${1}_completion
  887. # if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then
  888. # echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host|proc COMMANDS..."
  889. # return 1
  890. # fi
  891. # shift
  892. # __fzf_orig_completion < <(complete -p "$@" 2> /dev/null)
  893. # for cmd in "$@"; do
  894. # case "$kind" in
  895. # dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;
  896. # var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;
  897. # alias) __fzf_defc "$cmd" "$fn" "-a" ;;
  898. # *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;
  899. # esac
  900. # done
  901. # }
  902. #
  903. # fi
  904. # ### end: completion.bash ###