.zshrc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 ZSH="$HOME/.oh-my-zsh"
  12. export GHCUP_INSTALL_BASE_PREFIX="$HOME/.config" # for GHCUP
  13. export GOPATH="$HOME/go"
  14. # export
  15. 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
  16. export EDITOR="vim" # vim is either a link to nvim or just vim
  17. export VISUAL="gvim" # gvim is either a link to nvim-qt or just gvim
  18. export TERM="xterm-256color"
  19. export TERMINAL="alacritty"
  20. export BROWSER="brave"
  21. export PAGER="bat"
  22. export MANPAGER="sh -c 'col -bx | bat -l man -p'" # theme moved to the .config/bat/config
  23. # export MANPAGER="bat man -p'" # $MANPAGER use batcat to read mans
  24. export RANGER_LOAD_DEFAULT_RC=FALSE # to avoid loading ranger's config twice
  25. export ANDROID_SDK="$HOME/Programs/Android_SDK"
  26. export XDG_CONFIG_HOME="$HOME/.config"
  27. export XDG_DATA_HOME="$HOME/.local/share"
  28. # export XDG_DATA_DIRS="$HOME/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:$HOME/.local/share/flatpak/exports/share/applications:/var/lib/flatpak/exports/share/applications"
  29. export XDG_CACHE_HOME="$HOME/.cache"
  30. # # export LIBVA_DRIVERS_PATH="/usr/lib/xorg/modules/drivers"
  31. # # export LIBVA_DRIVER_NAME="vdpau"
  32. # export LIBVA_DRIVER_NAME="nvidia"
  33. # # export VDPAU_DRIVER="nvidia"
  34. #
  35. # # export LIBVA_DRIVERS_PATH="/usr/lib/dri/"
  36. # # export LIBVA_DRIVER_NAME="vdpau"
  37. # # export VDPAU_DRIVER="nvidia"
  38. # # Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
  39. # # Initialization code that may require console input (password prompts, [y/n]
  40. # # confirmations, etc.) must go above this block; everything else may go below.
  41. # if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  42. # source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
  43. # fi
  44. ###############################################################################
  45. # AUTOCOMPLETE AND HIGHLIGHT COLORS
  46. ###############################################################################
  47. ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#7d7d7d"
  48. # Set name of the theme to load --- if set to "random", it will
  49. # load a random theme each time oh-my-zsh is loaded, in which case,
  50. # to know which specific one was loaded, run: echo $RANDOM_THEME
  51. # See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
  52. # ZSH_THEME="bira"
  53. ZSH_THEME="bira_my"
  54. # ZSH_THEME="powerlevel10k/powerlevel10k"
  55. # Set list of themes to pick from when loading at random
  56. # Setting this variable when ZSH_THEME=random will cause zsh to load
  57. # a theme from this variable instead of looking in $ZSH/themes/
  58. # If set to an empty array, this variable will have no effect.
  59. # ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
  60. # Uncomment the following line to use case-sensitive completion.
  61. # CASE_SENSITIVE="true"
  62. # Uncomment the following line to use hyphen-insensitive completion.
  63. # Case-sensitive completion must be off. _ and - will be interchangeable.
  64. # HYPHEN_INSENSITIVE="true"
  65. # Uncomment the following line to disable bi-weekly auto-update checks.
  66. # DISABLE_AUTO_UPDATE="true"
  67. # Uncomment the following line to automatically update without prompting.
  68. # DISABLE_UPDATE_PROMPT="true"
  69. # Uncomment the following line to change how often to auto-update (in days).
  70. # export UPDATE_ZSH_DAYS=13
  71. # Uncomment the following line if pasting URLs and other text is messed up.
  72. # DISABLE_MAGIC_FUNCTIONS="true"
  73. # Uncomment the following line to disable colors in ls.
  74. # DISABLE_LS_COLORS="true"
  75. # Uncomment the following line to disable auto-setting terminal title.
  76. # DISABLE_AUTO_TITLE="true"
  77. # Uncomment the following line to enable command auto-correction.
  78. # ENABLE_CORRECTION="true"
  79. # Uncomment the following line to display red dots whilst waiting for completion.
  80. # COMPLETION_WAITING_DOTS="true"
  81. # Uncomment the following line if you want to disable marking untracked files
  82. # under VCS as dirty. This makes repository status check for large repositories
  83. # much, much faster.
  84. # DISABLE_UNTRACKED_FILES_DIRTY="true"
  85. # Uncomment the following line if you want to change the command execution time
  86. # stamp shown in the history command output.
  87. # You can set one of the optional three formats:
  88. # "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
  89. # or set a custom format using the strftime function format specifications,
  90. # see 'man strftime' for details.
  91. # HIST_STAMPS="mm/dd/yyyy"
  92. # Would you like to use another custom folder than $ZSH/custom?
  93. # ZSH_CUSTOM=/path/to/new-custom-folder
  94. ###############################################################################
  95. # PLUGINS
  96. ###############################################################################
  97. # Which plugins would you like to load?
  98. # Standard plugins can be found in $ZSH/plugins/
  99. # Custom plugins may be added to $ZSH_CUSTOM/plugins/
  100. # Example format: plugins=(rails git textmate ruby lighthouse)
  101. # Add wisely, as too many plugins slow down shell startup.
  102. plugins=(git asdf)
  103. # plugins=(zsh-autosuggestions)
  104. # plugins=(zsh-syntax-highlighting)
  105. # User configuration
  106. # export MANPATH="/usr/local/man:$MANPATH"
  107. # You may need to manually set your language environment
  108. # export LANG=en_US.UTF-8
  109. # Preferred editor for local and remote sessions
  110. # if [[ -n $SSH_CONNECTION ]]; then
  111. # export EDITOR='vim'
  112. # else
  113. # export EDITOR='mvim'
  114. # fi
  115. # Compilation flags
  116. # export ARCHFLAGS="-arch x86_64"
  117. ###############################################################################
  118. # ALIASes
  119. ###############################################################################
  120. # Set personal aliases, overriding those provided by oh-my-zsh libs,
  121. # plugins, and themes. Aliases can be placed here, though oh-my-zsh
  122. # users are encouraged to define aliases within the ZSH_CUSTOM folder.
  123. # For a full list of active aliases, run `alias`.
  124. # alias ls='ls --color=auto'
  125. # alias ll='ls -l'
  126. # alias ll='ls -lh'
  127. # alias la='ls -la'
  128. # alias la='ls -lah'
  129. # alias lf='ls -lFh'
  130. alias lse='exa -g --color=always --group-directories-first'
  131. alias lle='lse -l'
  132. alias ls='lsd --group-dirs=first'
  133. alias ll='lsd --blocks=permission,links,user,group,size,date,name --group-dirs=first --date="+%d %b %H:%M"'
  134. alias la='ll -a'
  135. alias tree='lsd --tree'
  136. # alias bat='bat --theme gruvbox-dark' # theme moved to the .config/bat/config
  137. alias grep='grep --color=auto'
  138. alias fgrep='fgrep --color=auto'
  139. alias egrep='egrep --color=auto'
  140. alias ifconfig=/sbin/ifconfig
  141. # confirm before overwriting something ----------------------------------------
  142. alias cp="cp -i"
  143. alias mv='mv -i'
  144. alias rm='rm -i'
  145. # switch between shells -------------------------------------------------------
  146. alias tobash="sudo chsh $USER -s /usr/bin/env bash && echo 'Now log out.'"
  147. alias tozsh="sudo chsh $USER -s /usr/bin/env zsh && echo 'Now log out.'"
  148. alias tofish="sudo chsh $USER -s /usr/bin/env fish && echo 'Now log out.'"
  149. # navigation ------------------------------------------------------------------
  150. alias ..='cd ..'
  151. alias ...='cd ../..'
  152. alias .3='cd ../../..'
  153. alias .4='cd ../../../..'
  154. alias .5='cd ../../../../..'
  155. # get top process eating memory -----------------------------------------------
  156. alias psmem='ps auxf | sort -nr -k 4'
  157. alias psmem10='ps auxf | sort -nr -k 4 | head -10'
  158. # get top process eating cpu --------------------------------------------------
  159. alias pscpu='ps auxf | sort -nr -k 3'
  160. alias pscpu10='ps auxf | sort -nr -k 3 | head -10'
  161. # git -------------------------------------------------------------------------
  162. alias config='/usr/bin/git --git-dir=$HOME/.dotfiles.git --work-tree=$HOME'
  163. # run some programs -----------------------------------------------------------
  164. alias v='vim'
  165. alias f='ranger'
  166. alias vf='vifm'
  167. alias emacs="emacsclient -c -a 'emacs'"
  168. ###############################################################################
  169. # PROMPT
  170. ###############################################################################
  171. # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
  172. # [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
  173. # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
  174. [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
  175. ###############################################################################
  176. # Source
  177. ###############################################################################
  178. source $ZSH/oh-my-zsh.sh
  179. # Load ; should be last.
  180. # source $HOME/powerlevel10k/powerlevel10k.zsh-theme
  181. # source /usr/share/autojump/autojump.zsh 2>/dev/null
  182. # source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh 2>/dev/null
  183. source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
  184. # # Syntax-highlighting like in fish
  185. # source $ZSH/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  186. # source $ZSH/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
  187. source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  188. # source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh 2>/dev/null
  189. # source ~/.git-completion.zsh
  190. # source ~/.git-prompt.sh
  191. # # asdf manager ----------------------------------------------------------------
  192. # source $HOME/.asdf/asdf.sh
  193. # source $HOME/.asdf/completions/asdf.bash
  194. # Fuzzy finder ----------------------------------------------------------------
  195. source /usr/share/fzf/completion.zsh
  196. source /usr/share/fzf/key-bindings.zsh
  197. # [ -f ~/.fzf.bash ] && source ~/.fzf.bash
  198. # # broot -----------------------------------------------------------------------
  199. # source /home/alexander/.config/broot/launcher/bash/br
  200. # Run neofetch ----------------------------------------------------------------
  201. neofetch
  202. # ### key-bindings.zsh ###
  203. # # ____ ____
  204. # # / __/___ / __/
  205. # # / /_/_ / / /_
  206. # # / __/ / /_/ __/
  207. # # /_/ /___/_/ key-bindings.zsh
  208. # #
  209. # # - $FZF_TMUX_OPTS
  210. # # - $FZF_CTRL_T_COMMAND
  211. # # - $FZF_CTRL_T_OPTS
  212. # # - $FZF_CTRL_R_OPTS
  213. # # - $FZF_ALT_C_COMMAND
  214. # # - $FZF_ALT_C_OPTS
  215. #
  216. #
  217. # # Key bindings
  218. # # ------------
  219. #
  220. # # The code at the top and the bottom of this file is the same as in completion.zsh.
  221. # # Refer to that file for explanation.
  222. # if 'zmodload' 'zsh/parameter' 2>'/dev/null' && (( ${+options} )); then
  223. # __fzf_key_bindings_options="options=(${(j: :)${(kv)options[@]}})"
  224. # else
  225. # () {
  226. # __fzf_key_bindings_options="setopt"
  227. # 'local' '__fzf_opt'
  228. # for __fzf_opt in "${(@)${(@f)$(set -o)}%% *}"; do
  229. # if [[ -o "$__fzf_opt" ]]; then
  230. # __fzf_key_bindings_options+=" -o $__fzf_opt"
  231. # else
  232. # __fzf_key_bindings_options+=" +o $__fzf_opt"
  233. # fi
  234. # done
  235. # }
  236. # fi
  237. #
  238. # 'builtin' 'emulate' 'zsh' && 'builtin' 'setopt' 'no_aliases'
  239. #
  240. # {
  241. # if [[ -o interactive ]]; then
  242. #
  243. # __fzf_defaults() {
  244. # # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  245. # # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  246. # echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
  247. # command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
  248. # echo "${FZF_DEFAULT_OPTS-} $2"
  249. # }
  250. #
  251. # # CTRL-T - Paste the selected file path(s) into the command line
  252. # __fzf_select() {
  253. # setopt localoptions pipefail no_aliases 2> /dev/null
  254. # local item
  255. # FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \
  256. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \
  257. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" < /dev/tty | while read -r item; do
  258. # echo -n -E "${(q)item} "
  259. # done
  260. # local ret=$?
  261. # echo
  262. # return $ret
  263. # }
  264. #
  265. # __fzfcmd() {
  266. # [ -n "${TMUX_PANE-}" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "${FZF_TMUX_OPTS-}" ]; } &&
  267. # echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
  268. # }
  269. #
  270. # fzf-file-widget() {
  271. # LBUFFER="${LBUFFER}$(__fzf_select)"
  272. # local ret=$?
  273. # zle reset-prompt
  274. # return $ret
  275. # }
  276. # if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then
  277. # zle -N fzf-file-widget
  278. # bindkey -M emacs '^T' fzf-file-widget
  279. # bindkey -M vicmd '^T' fzf-file-widget
  280. # bindkey -M viins '^T' fzf-file-widget
  281. # fi
  282. #
  283. # # ALT-C - cd into the selected directory
  284. # fzf-cd-widget() {
  285. # setopt localoptions pipefail no_aliases 2> /dev/null
  286. # local dir="$(
  287. # FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \
  288. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \
  289. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) < /dev/tty)"
  290. # if [[ -z "$dir" ]]; then
  291. # zle redisplay
  292. # return 0
  293. # fi
  294. # zle push-line # Clear buffer. Auto-restored on next prompt.
  295. # BUFFER="builtin cd -- ${(q)dir:a}"
  296. # zle accept-line
  297. # local ret=$?
  298. # unset dir # ensure this doesn't end up appearing in prompt expansion
  299. # zle reset-prompt
  300. # return $ret
  301. # }
  302. # if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then
  303. # zle -N fzf-cd-widget
  304. # bindkey -M emacs '\ec' fzf-cd-widget
  305. # bindkey -M vicmd '\ec' fzf-cd-widget
  306. # bindkey -M viins '\ec' fzf-cd-widget
  307. # fi
  308. #
  309. # # CTRL-R - Paste the selected command from history into the command line
  310. # fzf-history-widget() {
  311. # local selected
  312. # setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob nobash_rematch 2> /dev/null
  313. # # Ensure the associative history array, which maps event numbers to the full
  314. # # history lines, is loaded, and that Perl is installed for multi-line output.
  315. # if zmodload -F zsh/parameter p:history 2>/dev/null && (( ${#commands[perl]} )); then
  316. # selected="$(printf '%s\t%s\000' "${(kv)history[@]}" |
  317. # perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\t(.*)/s, $1)}++) { s/\n/\n\t/g; print; }' |
  318. # FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m --read0") \
  319. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
  320. # else
  321. # selected="$(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |
  322. # FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m") \
  323. # FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
  324. # fi
  325. # local ret=$?
  326. # if [ -n "$selected" ]; then
  327. # if [[ $(awk '{print $1; exit}' <<< "$selected") =~ ^[1-9][0-9]* ]]; then
  328. # zle vi-fetch-history -n $MATCH
  329. # else # selected is a custom query, not from history
  330. # LBUFFER="$selected"
  331. # fi
  332. # fi
  333. # zle reset-prompt
  334. # return $ret
  335. # }
  336. # zle -N fzf-history-widget
  337. # bindkey -M emacs '^R' fzf-history-widget
  338. # bindkey -M vicmd '^R' fzf-history-widget
  339. # bindkey -M viins '^R' fzf-history-widget
  340. # fi
  341. #
  342. # } always {
  343. # eval $__fzf_key_bindings_options
  344. # 'unset' '__fzf_key_bindings_options'
  345. # }
  346. # ### end: key-bindings.zsh ###
  347. # ### completion.zsh ###
  348. # # ____ ____
  349. # # / __/___ / __/
  350. # # / /_/_ / / /_
  351. # # / __/ / /_/ __/
  352. # # /_/ /___/_/ completion.zsh
  353. # #
  354. # # - $FZF_TMUX (default: 0)
  355. # # - $FZF_TMUX_OPTS (default: empty)
  356. # # - $FZF_COMPLETION_TRIGGER (default: '**')
  357. # # - $FZF_COMPLETION_OPTS (default: empty)
  358. # # - $FZF_COMPLETION_PATH_OPTS (default: empty)
  359. # # - $FZF_COMPLETION_DIR_OPTS (default: empty)
  360. #
  361. #
  362. # # Both branches of the following `if` do the same thing -- define
  363. # # __fzf_completion_options such that `eval $__fzf_completion_options` sets
  364. # # all options to the same values they currently have. We'll do just that at
  365. # # the bottom of the file after changing options to what we prefer.
  366. # #
  367. # # IMPORTANT: Until we get to the `emulate` line, all words that *can* be quoted
  368. # # *must* be quoted in order to prevent alias expansion. In addition, code must
  369. # # be written in a way works with any set of zsh options. This is very tricky, so
  370. # # careful when you change it.
  371. # #
  372. # # Start by loading the builtin zsh/parameter module. It provides `options`
  373. # # associative array that stores current shell options.
  374. # if 'zmodload' 'zsh/parameter' 2>'/dev/null' && (( ${+options} )); then
  375. # # This is the fast branch and it gets taken on virtually all Zsh installations.
  376. # #
  377. # # ${(kv)options[@]} expands to array of keys (option names) and values ("on"
  378. # # or "off"). The subsequent expansion# with (j: :) flag joins all elements
  379. # # together separated by spaces. __fzf_completion_options ends up with a value
  380. # # like this: "options=(shwordsplit off aliases on ...)".
  381. # __fzf_completion_options="options=(${(j: :)${(kv)options[@]}})"
  382. # else
  383. # # This branch is much slower because it forks to get the names of all
  384. # # zsh options. It's possible to eliminate this fork but it's not worth the
  385. # # trouble because this branch gets taken only on very ancient or broken
  386. # # zsh installations.
  387. # () {
  388. # # That `()` above defines an anonymous function. This is essentially a scope
  389. # # for local parameters. We use it to avoid polluting global scope.
  390. # 'local' '__fzf_opt'
  391. # __fzf_completion_options="setopt"
  392. # # `set -o` prints one line for every zsh option. Each line contains option
  393. # # name, some spaces, and then either "on" or "off". We just want option names.
  394. # # Expansion with (@f) flag splits a string into lines. The outer expansion
  395. # # removes spaces and everything that follow them on every line. __fzf_opt
  396. # # ends up iterating over option names: shwordsplit, aliases, etc.
  397. # for __fzf_opt in "${(@)${(@f)$(set -o)}%% *}"; do
  398. # if [[ -o "$__fzf_opt" ]]; then
  399. # # Option $__fzf_opt is currently on, so remember to set it back on.
  400. # __fzf_completion_options+=" -o $__fzf_opt"
  401. # else
  402. # # Option $__fzf_opt is currently off, so remember to set it back off.
  403. # __fzf_completion_options+=" +o $__fzf_opt"
  404. # fi
  405. # done
  406. # # The value of __fzf_completion_options here looks like this:
  407. # # "setopt +o shwordsplit -o aliases ..."
  408. # }
  409. # fi
  410. #
  411. # # Enable the default zsh options (those marked with <Z> in `man zshoptions`)
  412. # # but without `aliases`. Aliases in functions are expanded when functions are
  413. # # defined, so if we disable aliases here, we'll be sure to have no pesky
  414. # # aliases in any of our functions. This way we won't need prefix every
  415. # # command with `command` or to quote every word to defend against global
  416. # # aliases. Note that `aliases` is not the only option that's important to
  417. # # control. There are several others that could wreck havoc if they are set
  418. # # to values we don't expect. With the following `emulate` command we
  419. # # sidestep this issue entirely.
  420. # 'builtin' 'emulate' 'zsh' && 'builtin' 'setopt' 'no_aliases'
  421. #
  422. # # This brace is the start of try-always block. The `always` part is like
  423. # # `finally` in lesser languages. We use it to *always* restore user options.
  424. # {
  425. # # The 'emulate' command should not be placed inside the interactive if check;
  426. # # placing it there fails to disable alias expansion. See #3731.
  427. # if [[ -o interactive ]]; then
  428. #
  429. # # To use custom commands instead of find, override _fzf_compgen_{path,dir}
  430. # #
  431. # # _fzf_compgen_path() {
  432. # # echo "$1"
  433. # # command find -L "$1" \
  434. # # -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \
  435. # # -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'
  436. # # }
  437. # #
  438. # # _fzf_compgen_dir() {
  439. # # command find -L "$1" \
  440. # # -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \
  441. # # -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'
  442. # # }
  443. #
  444. # ###########################################################
  445. #
  446. # __fzf_defaults() {
  447. # # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  448. # # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
  449. # echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
  450. # command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
  451. # echo "${FZF_DEFAULT_OPTS-} $2"
  452. # }
  453. #
  454. # __fzf_comprun() {
  455. # if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then
  456. # _fzf_comprun "$@"
  457. # elif [ -n "${TMUX_PANE-}" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "${FZF_TMUX_OPTS-}" ]; }; then
  458. # shift
  459. # if [ -n "${FZF_TMUX_OPTS-}" ]; then
  460. # fzf-tmux ${(Q)${(Z+n+)FZF_TMUX_OPTS}} -- "$@"
  461. # else
  462. # fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%} -- "$@"
  463. # fi
  464. # else
  465. # shift
  466. # fzf "$@"
  467. # fi
  468. # }
  469. #
  470. # # Extract the name of the command. e.g. foo=1 bar baz**<tab>
  471. # __fzf_extract_command() {
  472. # local token tokens
  473. # tokens=(${(z)1})
  474. # for token in $tokens; do
  475. # token=${(Q)token}
  476. # if [[ "$token" =~ [[:alnum:]] && ! "$token" =~ "=" ]]; then
  477. # echo "$token"
  478. # return
  479. # fi
  480. # done
  481. # echo "${tokens[1]}"
  482. # }
  483. #
  484. # __fzf_generic_path_completion() {
  485. # local base lbuf cmd compgen fzf_opts suffix tail dir leftover matches
  486. # base=$1
  487. # lbuf=$2
  488. # cmd=$(__fzf_extract_command "$lbuf")
  489. # compgen=$3
  490. # fzf_opts=$4
  491. # suffix=$5
  492. # tail=$6
  493. #
  494. # setopt localoptions nonomatch
  495. # if [[ $base = *'$('* ]] || [[ $base = *'<('* ]] || [[ $base = *'>('* ]] || [[ $base = *':='* ]] || [[ $base = *'`'* ]]; then
  496. # return
  497. # fi
  498. # eval "base=$base" 2> /dev/null || return
  499. # [[ $base = *"/"* ]] && dir="$base"
  500. # while [ 1 ]; do
  501. # if [[ -z "$dir" || -d ${dir} ]]; then
  502. # leftover=${base/#"$dir"}
  503. # leftover=${leftover/#\/}
  504. # [ -z "$dir" ] && dir='.'
  505. # [ "$dir" != "/" ] && dir="${dir/%\//}"
  506. # matches=$(
  507. # export FZF_DEFAULT_OPTS
  508. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --scheme=path" "${FZF_COMPLETION_OPTS-}")
  509. # unset FZF_DEFAULT_COMMAND FZF_DEFAULT_OPTS_FILE
  510. # if declare -f "$compgen" > /dev/null; then
  511. # eval "$compgen $(printf %q "$dir")" | __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover"
  512. # else
  513. # if [[ $compgen =~ dir ]]; then
  514. # walker=dir,follow
  515. # rest=${FZF_COMPLETION_DIR_OPTS-}
  516. # else
  517. # walker=file,dir,follow,hidden
  518. # rest=${FZF_COMPLETION_PATH_OPTS-}
  519. # fi
  520. # __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover" --walker "$walker" --walker-root="$dir" ${(Q)${(Z+n+)rest}} < /dev/tty
  521. # fi | while read -r item; do
  522. # item="${item%$suffix}$suffix"
  523. # echo -n -E "${(q)item} "
  524. # done
  525. # )
  526. # matches=${matches% }
  527. # if [ -n "$matches" ]; then
  528. # LBUFFER="$lbuf$matches$tail"
  529. # fi
  530. # zle reset-prompt
  531. # break
  532. # fi
  533. # dir=$(dirname "$dir")
  534. # dir=${dir%/}/
  535. # done
  536. # }
  537. #
  538. # _fzf_path_completion() {
  539. # __fzf_generic_path_completion "$1" "$2" _fzf_compgen_path \
  540. # "-m" "" " "
  541. # }
  542. #
  543. # _fzf_dir_completion() {
  544. # __fzf_generic_path_completion "$1" "$2" _fzf_compgen_dir \
  545. # "" "/" ""
  546. # }
  547. #
  548. # _fzf_feed_fifo() {
  549. # command rm -f "$1"
  550. # mkfifo "$1"
  551. # cat <&0 > "$1" &|
  552. # }
  553. #
  554. # _fzf_complete() {
  555. # setopt localoptions ksh_arrays
  556. # # Split arguments around --
  557. # local args rest str_arg i sep
  558. # args=("$@")
  559. # sep=
  560. # for i in {0..${#args[@]}}; do
  561. # if [[ "${args[$i]-}" = -- ]]; then
  562. # sep=$i
  563. # break
  564. # fi
  565. # done
  566. # if [[ -n "$sep" ]]; then
  567. # str_arg=
  568. # rest=("${args[@]:$((sep + 1)):${#args[@]}}")
  569. # args=("${args[@]:0:$sep}")
  570. # else
  571. # str_arg=$1
  572. # args=()
  573. # shift
  574. # rest=("$@")
  575. # fi
  576. #
  577. # local fifo lbuf cmd matches post
  578. # fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
  579. # lbuf=${rest[0]}
  580. # cmd=$(__fzf_extract_command "$lbuf")
  581. # post="${funcstack[1]}_post"
  582. # type $post > /dev/null 2>&1 || post=cat
  583. #
  584. # _fzf_feed_fifo "$fifo"
  585. # matches=$(
  586. # FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse" "${FZF_COMPLETION_OPTS-} $str_arg") \
  587. # FZF_DEFAULT_OPTS_FILE='' \
  588. # __fzf_comprun "$cmd" "${args[@]}" -q "${(Q)prefix}" < "$fifo" | $post | tr '\n' ' ')
  589. # if [ -n "$matches" ]; then
  590. # LBUFFER="$lbuf$matches"
  591. # fi
  592. # command rm -f "$fifo"
  593. # }
  594. #
  595. # # To use custom hostname lists, override __fzf_list_hosts.
  596. # # The function is expected to print hostnames, one per line as well as in the
  597. # # desired sorting and with any duplicates removed, to standard output.
  598. # if ! declare -f __fzf_list_hosts > /dev/null; then
  599. # __fzf_list_hosts() {
  600. # setopt localoptions nonomatch
  601. # command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \
  602. # <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts 2> /dev/null | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
  603. # <(command grep -v '^\s*\(#\|$\)' /etc/hosts 2> /dev/null | command grep -Fv '0.0.0.0' | command sed 's/#.*//') |
  604. # awk '{for (i = 2; i <= NF; i++) print $i}' | sort -u
  605. # }
  606. # fi
  607. #
  608. # _fzf_complete_telnet() {
  609. # _fzf_complete +m -- "$@" < <(__fzf_list_hosts)
  610. # }
  611. #
  612. # # The first and the only argument is the LBUFFER without the current word that contains the trigger.
  613. # # The current word without the trigger is in the $prefix variable passed from the caller.
  614. # _fzf_complete_ssh() {
  615. # local -a tokens
  616. # tokens=(${(z)1})
  617. # case ${tokens[-1]} in
  618. # -i|-F|-E)
  619. # _fzf_path_completion "$prefix" "$1"
  620. # ;;
  621. # *)
  622. # local user
  623. # [[ $prefix =~ @ ]] && user="${prefix%%@*}@"
  624. # _fzf_complete +m -- "$@" < <(__fzf_list_hosts | awk -v user="$user" '{print user $0}')
  625. # ;;
  626. # esac
  627. # }
  628. #
  629. # _fzf_complete_export() {
  630. # _fzf_complete -m -- "$@" < <(
  631. # declare -xp | sed 's/=.*//' | sed 's/.* //'
  632. # )
  633. # }
  634. #
  635. # _fzf_complete_unset() {
  636. # _fzf_complete -m -- "$@" < <(
  637. # declare -xp | sed 's/=.*//' | sed 's/.* //'
  638. # )
  639. # }
  640. #
  641. # _fzf_complete_unalias() {
  642. # _fzf_complete +m -- "$@" < <(
  643. # alias | sed 's/=.*//'
  644. # )
  645. # }
  646. #
  647. # _fzf_complete_kill() {
  648. # _fzf_complete -m --header-lines=1 --no-preview --wrap -- "$@" < <(
  649. # command ps -eo user,pid,ppid,start,time,command 2> /dev/null ||
  650. # command ps -eo user,pid,ppid,time,args # For BusyBox
  651. # )
  652. # }
  653. #
  654. # _fzf_complete_kill_post() {
  655. # awk '{print $2}'
  656. # }
  657. #
  658. # fzf-completion() {
  659. # local tokens cmd prefix trigger tail matches lbuf d_cmds
  660. # setopt localoptions noshwordsplit noksh_arrays noposixbuiltins
  661. #
  662. # # http://zsh.sourceforge.net/FAQ/zshfaq03.html
  663. # # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
  664. # tokens=(${(z)LBUFFER})
  665. # if [ ${#tokens} -lt 1 ]; then
  666. # zle ${fzf_default_completion:-expand-or-complete}
  667. # return
  668. # fi
  669. #
  670. # cmd=$(__fzf_extract_command "$LBUFFER")
  671. #
  672. # # Explicitly allow for empty trigger.
  673. # trigger=${FZF_COMPLETION_TRIGGER-'**'}
  674. # [ -z "$trigger" -a ${LBUFFER[-1]} = ' ' ] && tokens+=("")
  675. #
  676. # # When the trigger starts with ';', it becomes a separate token
  677. # if [[ ${LBUFFER} = *"${tokens[-2]-}${tokens[-1]}" ]]; then
  678. # tokens[-2]="${tokens[-2]-}${tokens[-1]}"
  679. # tokens=(${tokens[0,-2]})
  680. # fi
  681. #
  682. # lbuf=$LBUFFER
  683. # tail=${LBUFFER:$(( ${#LBUFFER} - ${#trigger} ))}
  684. #
  685. # # Trigger sequence given
  686. # if [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then
  687. # d_cmds=(${=FZF_COMPLETION_DIR_COMMANDS-cd pushd rmdir})
  688. #
  689. # [ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}
  690. # if [[ $prefix = *'$('* ]] || [[ $prefix = *'<('* ]] || [[ $prefix = *'>('* ]] || [[ $prefix = *':='* ]] || [[ $prefix = *'`'* ]]; then
  691. # return
  692. # fi
  693. # [ -n "${tokens[-1]}" ] && lbuf=${lbuf:0:-${#tokens[-1]}}
  694. #
  695. # if eval "type _fzf_complete_${cmd} > /dev/null"; then
  696. # prefix="$prefix" eval _fzf_complete_${cmd} ${(q)lbuf}
  697. # zle reset-prompt
  698. # elif [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then
  699. # _fzf_dir_completion "$prefix" "$lbuf"
  700. # else
  701. # _fzf_path_completion "$prefix" "$lbuf"
  702. # fi
  703. # # Fall back to default completion
  704. # else
  705. # zle ${fzf_default_completion:-expand-or-complete}
  706. # fi
  707. # }
  708. #
  709. # [ -z "$fzf_default_completion" ] && {
  710. # binding=$(bindkey '^I')
  711. # [[ $binding =~ 'undefined-key' ]] || fzf_default_completion=$binding[(s: :w)2]
  712. # unset binding
  713. # }
  714. #
  715. # zle -N fzf-completion
  716. # bindkey '^I' fzf-completion
  717. # fi
  718. #
  719. # } always {
  720. # # Restore the original options.
  721. # eval $__fzf_completion_options
  722. # 'unset' '__fzf_completion_options'
  723. # }
  724. # ### end: completion.zsh ###