dotfiles/zsh/completion.zsh

111 lines
3.7 KiB
Bash

# ===== init completion
autoload -Uz compinit && compinit -d ~/.zsh/cache/zcompdump
zmodload -i zsh/complist
# ===== setopts
# allow completion from within a word or phrase
setopt complete_in_word
# always complete and display matches immediately after pressing <Tab>
setopt no_list_ambiguous
# when completing from the middle of a word, move the cursor to the end.
setopt always_to_end
# always show completion menu on successive tab press.
# (needs unsetopt menu_complete to work)
setopt auto_menu
# do not autoselect the first completion entry
unsetopt menu_complete
# ===== completion options
# Enable completion caching, use rehash to clear
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache
# List all files in the current directory when pressing tab on a empty input,
# behave like complete-word otherwise.
complete-word-or-complete-list-of-files() {
if [[ $#BUFFER == 0 ]]; then
BUFFER='ls '
CURSOR=3
zle list-choices
zle backward-kill-word
else
zle complete-word
fi
}
zle -N complete-word-or-complete-list-of-files
bindkey '^I' complete-word-or-complete-list-of-files
# Force a reload of the completion system if nothing matched; this fixes
# installing a program and then trying to tab-completing its name.
_force_rehash() {
if (( CURRENT == 1 )); then
rehash
fi
# we didn't really complete anything.
return 1
}
# list of completers to use
zstyle ':completion:::::' completer \
_force_rehash _expand _complete _prefix _ignored _approximate
#TODO: keep _prefix or not?
# ignore case when trying to match typed characters
zstyle ':completion:*:(^approximate):*' matcher-list 'm:{a-z}={A-Z}'
# allow one mistake per three characters
zstyle -e ':completion:*:approximate:*' max-errors \
'reply=( $(( ($#PREFIX + $#SUFFIX) / 3 )) )'
# expand shell wildcards to all matching files after <TAB>
zstyle ':completion:*:expand:*' tag-order all-expansions
# keep prefixed unexpanded if possible
zstyle ':completion:*:expand:*' keep-prefix on
# ignore completion functions (until the _ignored completer)
zstyle ':completion:*:functions' ignored-patterns '_*'
# do not propose anything starting with '_' when offering typo corrections
CORRECT_IGNORE='_*'
# ===== completion appearance
# use ls colors for completion
zstyle ':completion:*' list-colors "${(@s.:.)LS_COLORS}"
# Make the list prompt friendly
zstyle ':completion:*:default' list-prompt '%SAt %p: Hit <TAB> for more, or the character to insert%s'
# Make the selection prompt friendly when there are a lot of choices
zstyle ':completion:*:default' select-prompt '%SScrolling active: current selection at %p%s'
# Display group (category) name (%d) (like 'external command', 'alias', etc.) in
# bold. Also display a message if _approximate found errors and no matches
# were found.
zstyle ':completion:*' format ' %B%d%b:'
zstyle ':completion:*:corrections' format ' %B%d%b (errors: %e)'
zstyle ':completion:*:warnings' format ' %BNo matches for: %d%b'
# display matches sorted into categories
zstyle ':completion:*' group-name ''
# always show menu and preselect the first option
zstyle ':completion:*' menu select=1 _complete _ignored _approximate
#TODO: keep?
# make completion functions use the verbose style and form
#zstyle ':completion:*' verbose yes
# ===== completion commands
# separate man pages by sections
zstyle ':completion:*' separate-sections on
# add simple colors to kill
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
# offer indexes before parameters in subscripts
zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
zstyle '*' single-ignored show
# apt-get recommendations
#if [[ $HAS_APT -eq 1 ]]; then
# source /etc/zsh_command_not_found
#fi