.bashrc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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="wezterm"
  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. ###############################################################################
  166. # Source
  167. ###############################################################################
  168. # git -------------------------------------------------------------------------
  169. source ~/.git-completion.bash
  170. source ~/.git-prompt.sh
  171. # asdf manager ----------------------------------------------------------------
  172. # source $HOME/.asdf/asdf.sh
  173. # source $HOME/.asdf/completions/asdf.bash
  174. # Fuzzy finder ----------------------------------------------------------------
  175. source /usr/share/fzf/completion.bash
  176. source /usr/share/fzf/key-bindings.bash
  177. # [ -f ~/.fzf.bash ] && source ~/.fzf.bash
  178. # # # broot -----------------------------------------------------------------------
  179. # # source /home/alexander/.config/broot/launcher/bash/br
  180. # Run neofetch ----------------------------------------------------------------
  181. neofetch
  182. # ### key-bindings.bash ###
  183. # # ____ ____
  184. # # / __/___ / __/
  185. # # / /_/_ / / /_
  186. # # / __/ / /_/ __/
  187. # # /_/ /___/_/ key-bindings.bash
  188. # #
  189. # # - $FZF_TMUX_OPTS
  190. # # - $FZF_CTRL_T_COMMAND
  191. # # - $FZF_CTRL_T_OPTS
  192. # # - $FZF_CTRL_R_OPTS
  193. # # - $FZF_ALT_C_COMMAND
  194. # # - $FZF_ALT_C_OPTS
  195. #
  196. # if [[ $- =~ i ]]; then
  197. #
  198. #
  199. # # Key bindings
  200. # # ------------
  201. #
  202. # __fzf_defaults() {
  203. # # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  204. # # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  205. # echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
  206. # command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
  207. # echo "${FZF_DEFAULT_OPTS-} $2"
  208. # }
  209. #
  210. # __fzf_select__() {
  211. # FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \
  212. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \
  213. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" |
  214. # while read -r item; do
  215. # printf '%q ' "$item" # escape special chars
  216. # done
  217. # }
  218. #
  219. # __fzfcmd() {
  220. # [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; } &&
  221. # echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
  222. # }
  223. #
  224. # fzf-file-widget() {
  225. # local selected="$(__fzf_select__ "$@")"
  226. # READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
  227. # READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
  228. # }
  229. #
  230. # __fzf_cd__() {
  231. # local dir
  232. # dir=$(
  233. # FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \
  234. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \
  235. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd)
  236. # ) && printf 'builtin cd -- %q' "$(builtin unset CDPATH && builtin cd -- "$dir" && builtin pwd)"
  237. # }
  238. #
  239. # if command -v perl > /dev/null; then
  240. # __fzf_history__() {
  241. # local output script
  242. # script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; s/\n/\n\t/gm; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++'
  243. # output=$(
  244. # set +o pipefail
  245. # builtin fc -lnr -2147483648 |
  246. # last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" |
  247. # FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign '"$'\t'"↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} +m --read0") \
  248. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"
  249. # ) || return
  250. # READLINE_LINE=$(command perl -pe 's/^\d*\t//' <<< "$output")
  251. # if [[ -z "$READLINE_POINT" ]]; then
  252. # echo "$READLINE_LINE"
  253. # else
  254. # READLINE_POINT=0x7fffffff
  255. # fi
  256. # }
  257. # else # awk - fallback for POSIX systems
  258. # __fzf_history__() {
  259. # local output script n x y z d
  260. # if [[ -z $__fzf_awk ]]; then
  261. # __fzf_awk=awk
  262. # # choose the faster mawk if: it's installed && build date >= 20230322 && version >= 1.3.4
  263. # IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)
  264. # [[ $n == mawk ]] && (( d >= 20230302 && (x *1000 +y) *1000 +z >= 1003004 )) && __fzf_awk=mawk
  265. # fi
  266. # [[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries
  267. # script='function P(b) { ++n; sub(/^[ *]/, "", b); if (!seen[b]++) { printf "%d\t%s%c", '$((BASH_REMATCH + 1))' - n, b, 0 } }
  268. # NR==1 { b = substr($0, 2); next }
  269. # /^\t/ { P(b); b = substr($0, 2); next }
  270. # { b = b RS $0 }
  271. # END { if (NR) P(b) }'
  272. # output=$(
  273. # set +o pipefail
  274. # builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '<lines>$'\n' )* ; <lines> ::= [^\n]* ( $'\n'<lines> )*
  275. # command $__fzf_awk "$script" | # ( <counter>$'\t'<lines>$'\000' )*
  276. # FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign '"$'\t'"↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} +m --read0") \
  277. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"
  278. # ) || return
  279. # READLINE_LINE=${output#*$'\t'}
  280. # if [[ -z "$READLINE_POINT" ]]; then
  281. # echo "$READLINE_LINE"
  282. # else
  283. # READLINE_POINT=0x7fffffff
  284. # fi
  285. # }
  286. # fi
  287. #
  288. # # Required to refresh the prompt after fzf
  289. # bind -m emacs-standard '"\er": redraw-current-line'
  290. #
  291. # bind -m vi-command '"\C-z": emacs-editing-mode'
  292. # bind -m vi-insert '"\C-z": emacs-editing-mode'
  293. # bind -m emacs-standard '"\C-z": vi-editing-mode'
  294. #
  295. # if (( BASH_VERSINFO[0] < 4 )); then
  296. # # CTRL-T - Paste the selected file path into the command line
  297. # if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then
  298. # 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"'
  299. # bind -m vi-command '"\C-t": "\C-z\C-t\C-z"'
  300. # bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'
  301. # fi
  302. #
  303. # # CTRL-R - Paste the selected command from history into the command line
  304. # bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er"'
  305. # bind -m vi-command '"\C-r": "\C-z\C-r\C-z"'
  306. # bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'
  307. # else
  308. # # CTRL-T - Paste the selected file path into the command line
  309. # if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then
  310. # bind -m emacs-standard -x '"\C-t": fzf-file-widget'
  311. # bind -m vi-command -x '"\C-t": fzf-file-widget'
  312. # bind -m vi-insert -x '"\C-t": fzf-file-widget'
  313. # fi
  314. #
  315. # # CTRL-R - Paste the selected command from history into the command line
  316. # bind -m emacs-standard -x '"\C-r": __fzf_history__'
  317. # bind -m vi-command -x '"\C-r": __fzf_history__'
  318. # bind -m vi-insert -x '"\C-r": __fzf_history__'
  319. # fi
  320. #
  321. # # ALT-C - cd into the selected directory
  322. # if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then
  323. # 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"'
  324. # bind -m vi-command '"\ec": "\C-z\ec\C-z"'
  325. # bind -m vi-insert '"\ec": "\C-z\ec\C-z"'
  326. # fi
  327. #
  328. # fi
  329. # ### end: key-bindings.bash ###
  330. # ### completion.bash ###
  331. # # ____ ____
  332. # # / __/___ / __/
  333. # # / /_/_ / / /_
  334. # # / __/ / /_/ __/
  335. # # /_/ /___/_/ completion.bash
  336. # #
  337. # # - $FZF_TMUX (default: 0)
  338. # # - $FZF_TMUX_OPTS (default: empty)
  339. # # - $FZF_COMPLETION_TRIGGER (default: '**')
  340. # # - $FZF_COMPLETION_OPTS (default: empty)
  341. # # - $FZF_COMPLETION_PATH_OPTS (default: empty)
  342. # # - $FZF_COMPLETION_DIR_OPTS (default: empty)
  343. #
  344. # if [[ $- =~ i ]]; then
  345. #
  346. #
  347. # # To use custom commands instead of find, override _fzf_compgen_{path,dir}
  348. # #
  349. # # _fzf_compgen_path() {
  350. # # echo "$1"
  351. # # command find -L "$1" \
  352. # # -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \
  353. # # -a -not -path "$1" -print 2> /dev/null | command sed 's@^\./@@'
  354. # # }
  355. # #
  356. # # _fzf_compgen_dir() {
  357. # # command find -L "$1" \
  358. # # -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \
  359. # # -a -not -path "$1" -print 2> /dev/null | command sed 's@^\./@@'
  360. # # }
  361. #
  362. # ###########################################################
  363. #
  364. # # To redraw line after fzf closes (printf '\e[5n')
  365. # bind '"\e[0n": redraw-current-line' 2> /dev/null
  366. #
  367. # __fzf_defaults() {
  368. # # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  369. # # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  370. # echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
  371. # command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
  372. # echo "${FZF_DEFAULT_OPTS-} $2"
  373. # }
  374. #
  375. # __fzf_comprun() {
  376. # if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then
  377. # _fzf_comprun "$@"
  378. # elif [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; }; then
  379. # shift
  380. # fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@"
  381. # else
  382. # shift
  383. # fzf "$@"
  384. # fi
  385. # }
  386. #
  387. # __fzf_orig_completion() {
  388. # local l comp f cmd
  389. # while read -r l; do
  390. # if [[ "$l" =~ ^(.*\ -F)\ *([^ ]*).*\ ([^ ]*)$ ]]; then
  391. # comp="${BASH_REMATCH[1]}"
  392. # f="${BASH_REMATCH[2]}"
  393. # cmd="${BASH_REMATCH[3]}"
  394. # [[ "$f" = _fzf_* ]] && continue
  395. # printf -v "_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}" "%s" "${comp} %s ${cmd} #${f}"
  396. # if [[ "$l" = *" -o nospace "* ]] && [[ ! "${__fzf_nospace_commands-}" = *" $cmd "* ]]; then
  397. # __fzf_nospace_commands="${__fzf_nospace_commands-} $cmd "
  398. # fi
  399. # fi
  400. # done
  401. # }
  402. #
  403. # # @param $1 cmd - Command name for which the original completion is searched
  404. # # @var[out] REPLY - Original function name is returned
  405. # __fzf_orig_completion_get_orig_func() {
  406. # local cmd orig_var orig
  407. # cmd=$1
  408. # orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"
  409. # orig="${!orig_var-}"
  410. # REPLY="${orig##*#}"
  411. # [[ $REPLY ]] && type "$REPLY" &> /dev/null
  412. # }
  413. #
  414. # # @param $1 cmd - Command name for which the original completion is searched
  415. # # @param $2 func - Fzf's completion function to replace the original function
  416. # # @var[out] REPLY - Completion setting is returned as a string to "eval"
  417. # __fzf_orig_completion_instantiate() {
  418. # local cmd func orig_var orig
  419. # cmd=$1
  420. # func=$2
  421. # orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"
  422. # orig="${!orig_var-}"
  423. # orig="${orig%#*}"
  424. # [[ $orig == *' %s '* ]] || return 1
  425. # printf -v REPLY "$orig" "$func"
  426. # }
  427. #
  428. # _fzf_opts_completion() {
  429. # local cur prev opts
  430. # COMPREPLY=()
  431. # cur="${COMP_WORDS[COMP_CWORD]}"
  432. # prev="${COMP_WORDS[COMP_CWORD-1]}"
  433. # opts="
  434. # +c --no-color
  435. # +i --no-ignore-case
  436. # +s --no-sort
  437. # +x --no-extended
  438. # --ansi
  439. # --bash
  440. # --bind
  441. # --border
  442. # --border-label
  443. # --border-label-pos
  444. # --color
  445. # --cycle
  446. # --disabled
  447. # --ellipsis
  448. # --expect
  449. # --filepath-word
  450. # --fish
  451. # --header
  452. # --header-first
  453. # --header-lines
  454. # --height
  455. # --highlight-line
  456. # --history
  457. # --history-size
  458. # --hscroll-off
  459. # --info
  460. # --jump-labels
  461. # --keep-right
  462. # --layout
  463. # --listen
  464. # --listen-unsafe
  465. # --literal
  466. # --man
  467. # --margin
  468. # --marker
  469. # --min-height
  470. # --no-bold
  471. # --no-clear
  472. # --no-hscroll
  473. # --no-mouse
  474. # --no-scrollbar
  475. # --no-separator
  476. # --no-unicode
  477. # --padding
  478. # --pointer
  479. # --preview
  480. # --preview-label
  481. # --preview-label-pos
  482. # --preview-window
  483. # --print-query
  484. # --print0
  485. # --prompt
  486. # --read0
  487. # --reverse
  488. # --scheme
  489. # --scroll-off
  490. # --separator
  491. # --sync
  492. # --tabstop
  493. # --tac
  494. # --tiebreak
  495. # --tmux
  496. # --track
  497. # --version
  498. # --with-nth
  499. # --with-shell
  500. # --wrap
  501. # --zsh
  502. # -0 --exit-0
  503. # -1 --select-1
  504. # -d --delimiter
  505. # -e --exact
  506. # -f --filter
  507. # -h --help
  508. # -i --ignore-case
  509. # -m --multi
  510. # -n --nth
  511. # -q --query
  512. # --"
  513. #
  514. # case "${prev}" in
  515. # --scheme)
  516. # COMPREPLY=( $(compgen -W "default path history" -- "$cur") )
  517. # return 0
  518. # ;;
  519. # --tiebreak)
  520. # COMPREPLY=( $(compgen -W "length chunk begin end index" -- "$cur") )
  521. # return 0
  522. # ;;
  523. # --color)
  524. # COMPREPLY=( $(compgen -W "dark light 16 bw no" -- "$cur") )
  525. # return 0
  526. # ;;
  527. # --layout)
  528. # COMPREPLY=( $(compgen -W "default reverse reverse-list" -- "$cur") )
  529. # return 0
  530. # ;;
  531. # --info)
  532. # COMPREPLY=( $(compgen -W "default right hidden inline inline-right" -- "$cur") )
  533. # return 0
  534. # ;;
  535. # --preview-window)
  536. # COMPREPLY=( $(compgen -W "
  537. # default
  538. # hidden
  539. # nohidden
  540. # wrap
  541. # nowrap
  542. # cycle
  543. # nocycle
  544. # up top
  545. # down bottom
  546. # left
  547. # right
  548. # rounded border border-rounded
  549. # sharp border-sharp
  550. # border-bold
  551. # border-block
  552. # border-thinblock
  553. # border-double
  554. # noborder border-none
  555. # border-horizontal
  556. # border-vertical
  557. # border-up border-top
  558. # border-down border-bottom
  559. # border-left
  560. # border-right
  561. # follow
  562. # nofollow" -- "$cur") )
  563. # return 0
  564. # ;;
  565. # --border)
  566. # COMPREPLY=( $(compgen -W "rounded sharp bold block thinblock double horizontal vertical top bottom left right none" -- "$cur") )
  567. # return 0
  568. # ;;
  569. # --border-label-pos|--preview-label-pos)
  570. # COMPREPLY=( $(compgen -W "center bottom top" -- "$cur") )
  571. # return 0
  572. # ;;
  573. # esac
  574. #
  575. # if [[ "$cur" =~ ^-|\+ ]]; then
  576. # COMPREPLY=( $(compgen -W "${opts}" -- "$cur") )
  577. # return 0
  578. # fi
  579. #
  580. # return 0
  581. # }
  582. #
  583. # _fzf_handle_dynamic_completion() {
  584. # local cmd ret REPLY orig_cmd orig_complete
  585. # cmd="$1"
  586. # shift
  587. # orig_cmd="$1"
  588. # if __fzf_orig_completion_get_orig_func "$cmd"; then
  589. # "$REPLY" "$@"
  590. # elif [[ -n "${_fzf_completion_loader-}" ]]; then
  591. # orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)
  592. # $_fzf_completion_loader "$@"
  593. # ret=$?
  594. # # _completion_loader may not have updated completion for the command
  595. # if [[ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]]; then
  596. # __fzf_orig_completion < <(complete -p "$orig_cmd" 2> /dev/null)
  597. #
  598. # # Update orig_complete by _fzf_orig_completion entry
  599. # [[ $orig_complete =~ ' -F '(_fzf_[^ ]+)' ' ]] &&
  600. # __fzf_orig_completion_instantiate "$cmd" "${BASH_REMATCH[1]}" &&
  601. # orig_complete=$REPLY
  602. #
  603. # if [[ "${__fzf_nospace_commands-}" = *" $orig_cmd "* ]]; then
  604. # eval "${orig_complete/ -F / -o nospace -F }"
  605. # else
  606. # eval "$orig_complete"
  607. # fi
  608. # fi
  609. # [[ $ret -eq 0 ]] && return 124
  610. # return $ret
  611. # fi
  612. # }
  613. #
  614. # __fzf_generic_path_completion() {
  615. # local cur base dir leftover matches trigger cmd
  616. # cmd="${COMP_WORDS[0]}"
  617. # if [[ $cmd == \\* ]]; then
  618. # cmd="${cmd:1}"
  619. # fi
  620. # COMPREPLY=()
  621. # trigger=${FZF_COMPLETION_TRIGGER-'**'}
  622. # cur="${COMP_WORDS[COMP_CWORD]}"
  623. # if [[ "$cur" == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *':='* ]] && [[ $cur != *'`'* ]]; then
  624. # base=${cur:0:${#cur}-${#trigger}}
  625. # eval "base=$base" 2> /dev/null || return
  626. #
  627. # dir=
  628. # [[ $base = *"/"* ]] && dir="$base"
  629. # while true; do
  630. # if [[ -z "$dir" ]] || [[ -d "$dir" ]]; then
  631. # leftover=${base/#"$dir"}
  632. # leftover=${leftover/#\/}
  633. # [[ -z "$dir" ]] && dir='.'
  634. # [[ "$dir" != "/" ]] && dir="${dir/%\//}"
  635. # matches=$(
  636. # export FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --scheme=path" "${FZF_COMPLETION_OPTS-} $2")
  637. # unset FZF_DEFAULT_COMMAND FZF_DEFAULT_OPTS_FILE
  638. # if declare -F "$1" > /dev/null; then
  639. # eval "$1 $(printf %q "$dir")" | __fzf_comprun "$4" -q "$leftover"
  640. # else
  641. # if [[ $1 =~ dir ]]; then
  642. # walker=dir,follow
  643. # rest=${FZF_COMPLETION_DIR_OPTS-}
  644. # else
  645. # walker=file,dir,follow,hidden
  646. # rest=${FZF_COMPLETION_PATH_OPTS-}
  647. # fi
  648. # __fzf_comprun "$4" -q "$leftover" --walker "$walker" --walker-root="$dir" $rest
  649. # fi | while read -r item; do
  650. # printf "%q " "${item%$3}$3"
  651. # done
  652. # )
  653. # matches=${matches% }
  654. # [[ -z "$3" ]] && [[ "${__fzf_nospace_commands-}" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "
  655. # if [[ -n "$matches" ]]; then
  656. # COMPREPLY=( "$matches" )
  657. # else
  658. # COMPREPLY=( "$cur" )
  659. # fi
  660. # printf '\e[5n'
  661. # return 0
  662. # fi
  663. # dir=$(command dirname "$dir")
  664. # [[ "$dir" =~ /$ ]] || dir="$dir"/
  665. # done
  666. # else
  667. # shift
  668. # shift
  669. # shift
  670. # _fzf_handle_dynamic_completion "$cmd" "$@"
  671. # fi
  672. # }
  673. #
  674. # _fzf_complete() {
  675. # # Split arguments around --
  676. # local args rest str_arg i sep
  677. # args=("$@")
  678. # sep=
  679. # for i in "${!args[@]}"; do
  680. # if [[ "${args[$i]}" = -- ]]; then
  681. # sep=$i
  682. # break
  683. # fi
  684. # done
  685. # if [[ -n "$sep" ]]; then
  686. # str_arg=
  687. # rest=("${args[@]:$((sep + 1)):${#args[@]}}")
  688. # args=("${args[@]:0:$sep}")
  689. # else
  690. # str_arg=$1
  691. # args=()
  692. # shift
  693. # rest=("$@")
  694. # fi
  695. #
  696. # local cur selected trigger cmd post
  697. # post="$(caller 0 | command awk '{print $2}')_post"
  698. # type -t "$post" > /dev/null 2>&1 || post='command cat'
  699. #
  700. # trigger=${FZF_COMPLETION_TRIGGER-'**'}
  701. # cmd="${COMP_WORDS[0]}"
  702. # cur="${COMP_WORDS[COMP_CWORD]}"
  703. # if [[ "$cur" == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *':='* ]] && [[ $cur != *'`'* ]]; then
  704. # cur=${cur:0:${#cur}-${#trigger}}
  705. #
  706. # selected=$(
  707. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse" "${FZF_COMPLETION_OPTS-} $str_arg") \
  708. # FZF_DEFAULT_OPTS_FILE='' \
  709. # __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | $post | command tr '\n' ' ')
  710. # selected=${selected% } # Strip trailing space not to repeat "-o nospace"
  711. # if [[ -n "$selected" ]]; then
  712. # COMPREPLY=("$selected")
  713. # else
  714. # COMPREPLY=("$cur")
  715. # fi
  716. # printf '\e[5n'
  717. # return 0
  718. # else
  719. # _fzf_handle_dynamic_completion "$cmd" "${rest[@]}"
  720. # fi
  721. # }
  722. #
  723. # _fzf_path_completion() {
  724. # __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"
  725. # }
  726. #
  727. # # Deprecated. No file only completion.
  728. # _fzf_file_completion() {
  729. # _fzf_path_completion "$@"
  730. # }
  731. #
  732. # _fzf_dir_completion() {
  733. # __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"
  734. # }
  735. #
  736. # _fzf_complete_kill() {
  737. # _fzf_proc_completion "$@"
  738. # }
  739. #
  740. # _fzf_proc_completion() {
  741. # _fzf_complete -m --header-lines=1 --no-preview --wrap -- "$@" < <(
  742. # command ps -eo user,pid,ppid,start,time,command 2> /dev/null ||
  743. # command ps -eo user,pid,ppid,time,args # For BusyBox
  744. # )
  745. # }
  746. #
  747. # _fzf_proc_completion_post() {
  748. # command awk '{print $2}'
  749. # }
  750. #
  751. # # To use custom hostname lists, override __fzf_list_hosts.
  752. # # The function is expected to print hostnames, one per line as well as in the
  753. # # desired sorting and with any duplicates removed, to standard output.
  754. # #
  755. # # e.g.
  756. # # # Use bash-completions’s _known_hosts_real() for getting the list of hosts
  757. # # __fzf_list_hosts() {
  758. # # # Set the local attribute for any non-local variable that is set by _known_hosts_real()
  759. # # local COMPREPLY=()
  760. # # _known_hosts_real ''
  761. # # printf '%s\n' "${COMPREPLY[@]}" | command sort -u --version-sort
  762. # # }
  763. # if ! declare -F __fzf_list_hosts > /dev/null; then
  764. # __fzf_list_hosts() {
  765. # 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 '[*?%]') \
  766. # <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | command tr ',' '\n' | command tr -d '[' | command awk '{ print $1 " " $1 }') \
  767. # <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0' | command sed 's/#.*//') |
  768. # command awk '{for (i = 2; i <= NF; i++) print $i}' | command sort -u
  769. # }
  770. # fi
  771. #
  772. # _fzf_host_completion() {
  773. # _fzf_complete +m -- "$@" < <(__fzf_list_hosts)
  774. # }
  775. #
  776. # # Values for $1 $2 $3 are described here
  777. # # https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html
  778. # # > the first argument ($1) is the name of the command whose arguments are being completed,
  779. # # > the second argument ($2) is the word being completed,
  780. # # > and the third argument ($3) is the word preceding the word being completed on the current command line.
  781. # _fzf_complete_ssh() {
  782. # case $3 in
  783. # -i|-F|-E)
  784. # _fzf_path_completion "$@"
  785. # ;;
  786. # *)
  787. # local user=
  788. # [[ "$2" =~ '@' ]] && user="${2%%@*}@"
  789. # _fzf_complete +m -- "$@" < <(__fzf_list_hosts | command awk -v user="$user" '{print user $0}')
  790. # ;;
  791. # esac
  792. # }
  793. #
  794. # _fzf_var_completion() {
  795. # _fzf_complete -m -- "$@" < <(
  796. # declare -xp | command sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p'
  797. # )
  798. # }
  799. #
  800. # _fzf_alias_completion() {
  801. # _fzf_complete -m -- "$@" < <(
  802. # alias | command sed -En 's|^alias ([^=]+).*|\1|p'
  803. # )
  804. # }
  805. #
  806. # # fzf options
  807. # complete -o default -F _fzf_opts_completion fzf
  808. # # fzf-tmux is a thin fzf wrapper that has only a few more options than fzf
  809. # # itself. As a quick improvement we take fzf's completion. Adding the few extra
  810. # # fzf-tmux specific options (like `-w WIDTH`) are left as a future patch.
  811. # complete -o default -F _fzf_opts_completion fzf-tmux
  812. #
  813. # d_cmds="${FZF_COMPLETION_DIR_COMMANDS-cd pushd rmdir}"
  814. #
  815. # # NOTE: $FZF_COMPLETION_PATH_COMMANDS and $FZF_COMPLETION_VAR_COMMANDS are
  816. # # undocumented and subject to change in the future.
  817. # a_cmds="${FZF_COMPLETION_PATH_COMMANDS-"
  818. # awk bat cat code diff diff3
  819. # emacs emacsclient ex file ftp g++ gcc gvim head hg hx java
  820. # javac ld less more mvim nvim patch perl python ruby
  821. # sed sftp sort source tail tee uniq vi view vim wc xdg-open
  822. # basename bunzip2 bzip2 chmod chown curl cp dirname du
  823. # find git grep gunzip gzip hg jar
  824. # ln ls mv open rm rsync scp
  825. # svn tar unzip zip"}"
  826. # v_cmds="${FZF_COMPLETION_VAR_COMMANDS-export unset printenv}"
  827. #
  828. # # Preserve existing completion
  829. # __fzf_orig_completion < <(complete -p $d_cmds $a_cmds $v_cmds unalias kill ssh 2> /dev/null)
  830. #
  831. # if type _comp_load > /dev/null 2>&1; then
  832. # # _comp_load was added in bash-completion 2.12 to replace _completion_loader.
  833. # # We use it without -D option so that it does not use _comp_complete_minimal as the fallback.
  834. # _fzf_completion_loader=_comp_load
  835. # elif type __load_completion > /dev/null 2>&1; then
  836. # # In bash-completion 2.11, _completion_loader internally calls __load_completion
  837. # # and if it returns a non-zero status, it sets the default 'minimal' completion.
  838. # _fzf_completion_loader=__load_completion
  839. # elif type _completion_loader > /dev/null 2>&1; then
  840. # _fzf_completion_loader=_completion_loader
  841. # fi
  842. #
  843. # __fzf_defc() {
  844. # local cmd func opts REPLY
  845. # cmd="$1"
  846. # func="$2"
  847. # opts="$3"
  848. # if __fzf_orig_completion_instantiate "$cmd" "$func"; then
  849. # eval "$REPLY"
  850. # else
  851. # complete -F "$func" $opts "$cmd"
  852. # fi
  853. # }
  854. #
  855. # # Anything
  856. # for cmd in $a_cmds; do
  857. # __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"
  858. # done
  859. #
  860. # # Directory
  861. # for cmd in $d_cmds; do
  862. # __fzf_defc "$cmd" _fzf_dir_completion "-o bashdefault -o nospace -o dirnames"
  863. # done
  864. #
  865. # # Variables
  866. # for cmd in $v_cmds; do
  867. # __fzf_defc "$cmd" _fzf_var_completion "-o default -o nospace -v"
  868. # done
  869. #
  870. # # Aliases
  871. # __fzf_defc unalias _fzf_alias_completion "-a"
  872. #
  873. # # Processes
  874. # __fzf_defc kill _fzf_proc_completion "-o default -o bashdefault"
  875. #
  876. # # ssh
  877. # __fzf_defc ssh _fzf_complete_ssh "-o default -o bashdefault"
  878. #
  879. # unset cmd d_cmds a_cmds v_cmds
  880. #
  881. # _fzf_setup_completion() {
  882. # local kind fn cmd
  883. # kind=$1
  884. # fn=_fzf_${1}_completion
  885. # if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then
  886. # echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host|proc COMMANDS..."
  887. # return 1
  888. # fi
  889. # shift
  890. # __fzf_orig_completion < <(complete -p "$@" 2> /dev/null)
  891. # for cmd in "$@"; do
  892. # case "$kind" in
  893. # dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;
  894. # var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;
  895. # alias) __fzf_defc "$cmd" "$fn" "-a" ;;
  896. # *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;
  897. # esac
  898. # done
  899. # }
  900. #
  901. # fi
  902. # ### end: completion.bash ###