diff --git a/README.mkd b/README.mkd index 1edf9ef..bdfead7 100644 --- a/README.mkd +++ b/README.mkd @@ -17,15 +17,23 @@ zle-line-init() { } zle -N zle-line-init -# use ctrl+t to toggle autosuggestions(hopefully this wont be needed) +# use ctrl+t to toggle autosuggestions(hopefully this wont be needed as +autosuggestions is designed to be unobtrusive) bindkey '^T' autosuggest-toggle - -# use ctrl+f to accept suggestions -bindkey '^F' autosuggest-accept-suggested-small-word -# or -bindkey '^F' autosuggest-accept-suggested-word -EOF ``` -I recommend binding ctrl+f to autosuggest-accept-suggested-small-word, as the -[zsh-history-substring-search](https://github.com/zsh-users/zsh-history-substring-search) -plugin will already provide a key to accept the entire suggestion + +Any widget that moves the cursor to the right(forward-word, forward-char...) +will accept parts of the suggested text. For example, vi-mode users can do +this: + +```sh +# Accept suggestions without leaving insert mode +bindkey '^f' vi-forward-word +# or +bindkey '^f' vi-forward-blank-word +``` + +Emacs-mode users can simply use alt+f which is bound to forward-word + +The [zsh-history-substring-search](https://github.com/zsh-users/zsh-history-substring-search) +plugin is also recommended. diff --git a/autosuggestions.zsh b/autosuggestions.zsh index 837e772..5e1d22f 100644 --- a/autosuggestions.zsh +++ b/autosuggestions.zsh @@ -35,6 +35,11 @@ menu-complete reverse-menu-complete menu-expand-or-complete menu-select accept-and-menu-complete ) +ZLE_AUTOSUGGEST_ACCEPT_WIDGETS=( +vi-forward-char forward-char vi-forward-word forward-word vi-add-eol +vi-add-next vi-forward-blank-word +) + autosuggest-pause() { [[ -z $ZLE_AUTOSUGGESTING ]] && return unset ZLE_AUTOSUGGESTING @@ -45,11 +50,7 @@ autosuggest-pause() { zle -A autosuggest-magic-space-orig magic-space zle -A autosuggest-backward-delete-char-orig backward-delete-char zle -A autosuggest-accept-line-orig accept-line - for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do - [[ -z $widgets[$widget] ]] && continue - eval "zle -A autosuggest-${widget}-orig ${widget}" - done - for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do + for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do [[ -z $widgets[$widget] ]] && continue eval "zle -A autosuggest-${widget}-orig ${widget}" done @@ -71,6 +72,10 @@ autosuggest-resume() { zle -A autosuggest-accept-line accept-line # Hook into some default widgets that should suspend autosuggestion # automatically + for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS; do + [[ -z $widgets[$widget] ]] && continue + eval "zle -A autosuggest-accept-suggestion $widget" + done for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do [[ -z $widgets[$widget] ]] && continue eval "zle -A autosuggest-suspend $widget" @@ -169,7 +174,7 @@ autosuggest-backward-delete-char() { # section when the user accepts the line autosuggest-accept-line() { RBUFFER='' - if (( $+functions[_zsh_highlight_buffer_modified] > 0 )); then + if ! (( $+functions[_zsh_highlight_buffer_modified] )); then # Only clear the colors if the user doesn't have zsh-highlight installed region_highlight=() fi @@ -225,17 +230,9 @@ autosuggest-tab() { autosuggest-highlight-suggested-text } -autosuggest-accept-suggested-small-word() { +autosuggest-accept-suggestion() { + zle autosuggest-${WIDGET}-orig "$@" if [[ -n $ZLE_AUTOSUGGESTING ]]; then - zle .vi-forward-word - autosuggest-invalidate-highlight-cache - autosuggest-highlight-suggested-text - fi -} - -autosuggest-accept-suggested-word() { - if [[ -n $ZLE_AUTOSUGGESTING ]]; then - zle .forward-word autosuggest-invalidate-highlight-cache autosuggest-highlight-suggested-text fi @@ -258,17 +255,15 @@ zle -N autosuggest-accept-line zle -N autosuggest-tab zle -N autosuggest-suspend +zle -N autosuggest-accept-suggestion # Save all widgets zle -A self-insert autosuggest-self-insert-orig zle -A magic-space autosuggest-magic-space-orig zle -A backward-delete-char autosuggest-backward-delete-char-orig zle -A accept-line autosuggest-accept-line-orig -for widget in $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS; do - [[ -z $widgets[$widget] ]] && continue - eval "zle -A $widget autosuggest-${widget}-orig" -done -for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do + +for widget in ${ZLE_AUTOSUGGEST_ACCEPT_WIDGETS} ${ZLE_AUTOSUGGEST_SUSPEND_WIDGETS} ${ZLE_AUTOSUGGEST_COMPLETION_WIDGETS}; do [[ -z $widgets[$widget] ]] && continue eval "zle -A $widget autosuggest-${widget}-orig" done