Compare commits

...

75 Commits

Author SHA1 Message Date
Michael Robinson
7dc9e0f926 Merge pull request #85 from ericfreese/issue-84
Restore standard widgets to fix segfault when re-sourcing autosuggestions.zsh
2016-01-21 10:05:00 +13:00
Eric Freese
8777836a1c Restore standard widgets to fix segfault when re-sourcing autosuggestions.zsh 2016-01-20 02:26:36 -07:00
Michael Robinson
7a00bfa444 Merge pull request #83 from sorin-ionescu/patch-1
Accept vi-end-of-line
2015-11-24 22:12:56 +13:00
Sorin Ionescu
0ad9525955 Accept vi-end-of-line 2015-11-23 22:15:52 -05:00
Michael Robinson
ec1d251dad Merge pull request #76 from thorerik/patch-1
Avoid bell on all keysrtokes
2015-11-19 09:37:49 +13:00
Michael Robinson
b1725e9e74 Merge pull request #74 from reinhrst/master
fix for AUTOSUGGESTION_ACCEPT_RIGHT_ARROW in vi-mode
2015-11-09 09:47:41 +13:00
Thor Erik
c044d06f70 Avoid bell on all keysrtokes
Due to using a forked version of zsh-syntax-highlighting, you also get along the bug that causes a [bell on every keystroke][1], using zsh-users/zsh-syntax-highlighting instead I avoided this bug.

[1]: https://github.com/zsh-users/zsh-syntax-highlighting/issues/200
2015-10-16 09:06:03 +02:00
Claude
fc4576758c fix for AUTOSUGGESTION_ACCEPT_RIGHT_ARROW in vi-mode 2015-09-18 13:13:09 +02:00
Michael Robinson
cbaf7ced6e Merge pull request #68 from a-x-/support-OMZ
Support OMZ
2015-07-16 12:24:34 +12:00
Alexander Mekhonoshin (invntrm)
07165cd3ec Add symlink for OMZ compatibility 2015-07-14 11:11:41 +03:00
Michael Robinson
dde838c226 Merge pull request #63 from jirutka/patch-7 2015-05-23 12:41:27 +12:00
Jakub Jirutka
a796ce5bdb Readme: extend section Known issues (closes #14) 2015-05-23 01:57:51 +02:00
Michael Robinson
40a8d73054 Merge pull request #61 from jirutka/patch-6
LGTM!
2015-05-22 13:24:51 +12:00
Jakub Jirutka
6de948c9c5 Add widget autosuggest-execute-suggestion
It basically means this: go to the end of line (i.e. accept the
suggestion) and then hit enter.
2015-05-22 00:54:27 +02:00
Michael Robinson
c6afbaf254 Merge pull request #60 from jirutka/patch-5
Improve readme
2015-05-22 09:37:48 +12:00
Jakub Jirutka
47283c2619 Update readme 2015-05-21 20:23:09 +02:00
Jakub Jirutka
f090f609ac Rename LICENSE-MIT to LICENSE
LICENSE (optionally with an file extension) is a common file name for
the license file.
2015-05-21 18:09:33 +02:00
Jakub Jirutka
08f3b3148d Rename README.mkd to README.md
.md is a preferred file extension for Markdown files.
2015-05-21 18:06:38 +02:00
Michael Robinson
8d29045ffb Merge pull request #59 from jirutka/patch-4
Fix indentation in autosuggest-backward-delete-char()
2015-05-21 11:11:53 +12:00
Jakub Jirutka
bbd5300d60 Fix indentation in autosuggest-backward-delete-char() 2015-05-21 01:08:17 +02:00
Michael Robinson
5b23a17241 Merge pull request #58 from jirutka/patch-3 2015-05-20 11:13:48 +12:00
Michael Robinson
2d5ac282a9 Merge pull request #57 from jirutka/patch-2
Fix and unify formatting and code-style
2015-05-19 08:57:14 +12:00
Jakub Jirutka
62af373a59 Fix highlighting for paths completion using Tab 2015-05-18 16:33:50 +02:00
Jakub Jirutka
258e06c9ab Fix and unify formatting and code-style 2015-05-18 14:51:21 +02:00
Jakub Jirutka
f860711cc5 Add .editorconfig
See http://www.editorconfig.org/
2015-05-18 14:49:56 +02:00
Michael Robinson
0371904875 Merge pull request #55 from jirutka/patch-1 2015-05-17 07:57:25 +12:00
Jakub Jirutka
4b6512d1e8 Fix highlight cache invalidation
To work better with recent version of zsh-syntax-highlighting.
2015-05-16 20:35:13 +02:00
Michael Robinson
34c01f6259 Merge pull request #53 from calebmeyer/doc-updates
Updated readme with more configuration options.
2015-05-13 08:53:17 +12:00
Caleb Meyer
e637eb58eb Added backticks to 0, so it will highlight as code 2015-05-12 12:33:33 -05:00
Caleb Meyer
1bf5de2acc Updated readme with more configuration options. 2015-05-12 12:31:30 -05:00
Michael Robinson
9b565ff6b5 Merge pull request #51 from teto/fix_zshrc 2015-05-04 10:05:20 +12:00
Matthieu Coudron
24ac946dd9 Install script now looks for .zshrc in $ZDOTDIR as well.
User can also supply as a first parameter the script to append to
zsh-autosuggestions.
2015-05-03 22:57:03 +02:00
Michael Robinson
59699a0946 Merge pull request #49 from 0atman/0atman-patch-1
Simplify installation instructions
2015-04-15 08:29:31 +12:00
Tristram Oaten
d1640a399d Update README.mkd
My rational for this is:
 1. Most linux desktop users are using some kind of debian/ubuntu/mint, so why not have them catered for primarily?
 2. the bash instructions work for everyone who has bash. Last time I checked, that was 99% of the linux community.
2015-04-14 10:41:32 +01:00
Michael Robinson
533325c39d Addresses #44 2015-02-23 08:45:25 +13:00
Michael Robinson
fe6e9812b0 Merge pull request #45 from tirkarthi/patch-1
Added clarification to use bash
2015-02-18 19:42:58 +13:00
Xtreak
c740515df4 Added clarification to use bash
Some linux distros have sh symlinked to dash. So I have added a clarification to use bash for the distros.
2015-02-18 11:32:19 +05:30
Michael Robinson
3d5beea5c3 Merge pull request #41 from hkliya/patch-1
Update README.mkd
2015-01-30 08:29:38 +13:00
Seaborn Lee
a23ad43188 Update README.mkd
Fix the path.
2015-01-29 21:16:11 +08:00
Michael Robinson
c3e7668fbb Merge pull request #40 from bfred-it/osx-install-fix
Fixed: installation on OS X fails
2015-01-27 08:41:52 +13:00
Federico Brigante
8970e71d50 Fixed: installation on OS X fails 2015-01-23 23:38:46 +01:00
Michael Robinson
89b85d88cc Use -f instead of -e option for readlink to
Fixed #38
2015-01-09 08:38:59 +13:00
Michael Robinson
d76af3c076 Update README.mkd 2014-12-10 10:06:52 +13:00
Michael Robinson
5f13490bae Reverted 9e710593e3 2014-12-09 15:53:40 +13:00
Michael Robinson
1be4a54048 Merge pull request #35 from wgallios/master
Created simple install sh script
2014-12-09 15:52:38 +13:00
Michael Robinson
7446059f63 Merge pull request #31 from maksimr/patch-1
fix:Add autosuggest to highlighters list only once
2014-12-09 15:45:18 +13:00
Michael Robinson
71a7154815 Removed "unmaintained" message! 2014-12-09 15:39:55 +13:00
William Gallios
9e710593e3 updates to readme 2014-11-21 13:59:25 -08:00
William Gallios
0d9aabedb2 updated readme for new install script 2014-11-21 13:54:47 -08:00
William Gallios
32fb7930d8 created simple install script 2014-11-21 13:51:19 -08:00
Thiago de Arruda
0d36d4ccdd Add alpha-quality software warning 2014-11-05 07:51:26 -03:00
Thiago de Arruda
cbb0963896 Merge pull request #34 from CoolOppo/patch-1
Removed Shelr demo link because it died
2014-10-01 23:22:01 -03:00
CoolOppo
683b48b644 Removed Shelr demo link because it died 2014-10-01 21:20:52 -04:00
Thiago de Arruda
85739dbe76 Disable autosuggestion server startup.
The server is not working even in zsh5. Disable it until fixed.
2014-09-22 10:08:24 -03:00
Maksim Ryzhikov
6dbb419a7a fix:Add autosuggest to highlighters list only once 2014-08-11 10:26:39 +04:00
Thiago de Arruda
bd11c34e84 Merge pull request #16 from piecioshka/master
Create automatic command for plugin setup
2014-03-14 08:03:20 -02:00
Thiago de Arruda
0cc05bd08e Merge pull request #20 from mihaisucan/master
add a config for RIGHT arrow to accept the suggestion, like in fish
2014-03-14 08:02:05 -02:00
Mihai Sucan
824b9be13f add autosuggestions.plugin.zsh to get this repo working with antigen 2014-02-25 22:45:29 +02:00
Mihai Sucan
1cbd78c850 add a config for RIGHT arrow to accept the suggestion, like in fish 2014-02-23 22:54:31 +02:00
Thiago de Arruda
ebe4cf251a Merge pull request #17 from ajkaanbal/master
Suspend autosuggest on 'history-substring-search-up' and 'history-substr...
2014-02-18 12:11:14 -02:00
Ricardo M. Vilchis
7e69ddda7f Suspend autosuggest on 'history-substring-search-up' and 'history-substring-search-down' (zsh-history-substring-plugin commands). Accept on 'end-of-line' (Ctrl-e or END key) 2014-01-31 08:58:28 -06:00
piecioshka
5dff7a69f1 start label with big letter 2014-01-25 23:49:29 +01:00
piecioshka
c5567cf371 add label for zsh-autosuggestions 2014-01-25 23:47:40 +01:00
piecioshka
b18f01a256 fix end of file 2014-01-25 23:44:37 +01:00
tarruda
a29e838cdf Merge pull request #11 from dreadatour/master
Add plugin config variables
2013-12-04 15:31:36 -08:00
Vladimir Rudnyh
48f03e2b0c Suspend autosuggest on 'history-beginning-search-backward' and 'history-beginning-search-forward' commands 2013-12-04 23:07:24 +04:00
Vladimir Rudnyh
ea19b7bcf8 Add highlight color config variables 2013-12-03 23:45:56 +04:00
Vladimir Rudnyh
196cfa0eb0 Remove spaces at the end of lines 2013-12-03 23:25:52 +04:00
Thiago de Arruda
8af98f7d29 Validate original widget name when restoring on
autosuggest-pause
2013-11-08 11:49:33 -03:00
Thiago de Arruda
6ddbde147f Fixed syntax highlight in README code snippet 2013-11-08 08:24:02 -03:00
Thiago de Arruda
7d737e3cc8 Fixed accept-line and motion widgets integration
with zsh-syntax-highlighting
2013-11-08 08:20:57 -03:00
Thiago de Arruda
a0d6493f34 Update documentation 2013-11-07 20:17:10 -03:00
Thiago de Arruda
7cb87a8a55 Integrate with zsh-syntax-highlighting 2013-11-07 20:12:33 -03:00
Thiago de Arruda
936056fd9b Refactored, only use completion server on
zsh > 5.0.3(where the zle -F issue will be fixed)
2013-11-07 18:42:17 -03:00
Thiago de Arruda
a6f53879ae Direct less frequent log messages to stderr for
easier debugging
2013-10-30 08:36:53 -03:00
11 changed files with 378 additions and 134 deletions

10
.editorconfig Normal file
View File

@@ -0,0 +1,10 @@
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
indent_size = 4
[*.md]
indent_style = space

145
README.md Normal file
View File

@@ -0,0 +1,145 @@
# zsh-autosuggestions
_[Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh._
It suggests commands as you type, based on command history.
## Installation
If you already use [zsh-syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting) plugin, then make sure to be loaded **before** zsh-autosuggestions.
Note: _.zshrc_ is a file that contains user-specific ZSH configuration.
ZSH assumes this file in your home directory (i.e. `~/.zshrc`), but the location can be changed using `ZDOTDIR` variable.
### Using zgen
[Zgen](https://github.com/tarjoilija/zgen) is a simple and fast plugin manager for ZSH.
If you dont use zgen, then use instructions for the manual installation.
1. Load `tarruda/zsh-autosuggestions` and `zsh-users/zsh-syntax-highlighting` using zgen in your .zshrc file, for example:
```sh
if ! zgen saved; then
echo "Creating a zgen save"
zgen load zsh-users/zsh-syntax-highlighting
# autosuggestions should be loaded last
zgen load tarruda/zsh-autosuggestions
zgen save
fi
```
2. Enable zsh-autosuggestions; copy the following snippet and put it after the zgen config section in your .zshrc file:
```sh
# Enable autosuggestions automatically.
zle-line-init() {
zle autosuggest-start
}
zle -N zle-line-init
```
3. Run `zgen reset` and reopen your terminal.
### Manually
1. Clone this repository to `~/.zsh/zsh-autosuggestions` (or anywhere else):
```sh
git clone git://github.com/tarruda/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
```
2. Clone zsh-syntax-highlighting repository to `~/.zsh/zsh-syntax-highlighting` (or anywhere else):
```sh
git clone git://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/zsh-syntax-highlighting
```
3. Load and enable autosuggestions; copy the following snippet and put it to your .zshrc file:
```sh
# Load zsh-syntax-highlighting.
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# Load zsh-autosuggestions.
source ~/.zsh/zsh-autosuggestions/autosuggestions.zsh
# Enable autosuggestions automatically.
zle-line-init() {
zle autosuggest-start
}
zle -N zle-line-init
```
4. Reopen your terminal.
## Uninstallation
Just remove the config lines from .zshrc that youve added during “installation.”
If you dont use zgen, then also delete `~/.zsh/zsh-autosuggestions` and `~/.zsh/zsh-syntax-highlighting`.
## How to use
As you type commands, you will see a completion offered after the cursor, in a muted gray color (which can be changed, see [Configuration](#configuration)).
To accept the autosuggestion (replacing the command line contents), hit <kbd>End</kbd>, <kbd>Alt+F</kbd>, <kbd>Ctrl+F</kbd>, or any other key that moves the cursor to the right.
If the autosuggestion is not what you want, just ignore it: it wont execute unless you accept it.
Any widget that moves the cursor to the right (forward-word, forward-char, end-of-line…) 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
```
You can also use right arrow key to accept the suggested text as in Fish shell; see [Configuration](#configuration) section to enable it.
### Exposed widgets
This plugin defines some ZLE widgets (think about them as functions) which you can bind to some key using [bindkey](http://zshwiki.org/home/zle/bindkeys).
For example, to toggle autosuggestions using <kbd>Ctrl+T</kbd> add this to your .zshrc:
```sh
bindkey '^T' autosuggest-toggle
```
List of widgets:
- `autosuggest-toggle` disable/enable autosuggestions.
- `autosuggest-execute-suggestion` accept the suggestion and execute it.
## Configuration
You may override default global config variables after plugin load, i.e. put it to your .zshrc after the code that loads plugins.
- `AUTOSUGGESTION_HIGHLIGHT_COLOR` suggestion highlight color, default is `'fg=8'`.
- `AUTOSUGGESTION_HIGHLIGHT_CURSOR` highlight word after cursor, or not. Must be integer value `1` or `0`, default is `1`.
- `AUTOSUGGESTION_ACCEPT_RIGHT_ARROW` complete entire suggestion with right arrow. Must be integer value `1` or `0`, default is `0` (right arrow completes one letter at a time).
## Known Issues
> When I hit <kbd>Tab</kbd> and autosuggestions is enabled, it deletes the previous line, and scrolls up the terminal.
This usually happens when autosuggestions is used along with something like [“completion waiting dots.”](http://michael.thegrebs.com/2012/09/04/zsh-completion-waiting-dots/)
Check which widget is bind to the Tab key; run `bindkey "^I"`.
If it prints something other than `"^I" expand-or-complete`, then this may be the problem.
If you use [Oh My Zsh](https://github.com/robbyrussell/oh-my-zsh), then make sure that the variable `COMPLETION_WAITING_DOTS` is not set (it enables [this](https://github.com/robbyrussell/oh-my-zsh/blob/e55c715508a2f652fed741f2047c66dda2c6e5b0/lib/completion.zsh#L56-L64) problematic code).
If you use module [editor](https://github.com/sorin-ionescu/prezto/tree/master/modules/editor) from [Prezto](https://github.com/sorin-ionescu/prezto), then you must comment out [these lines](https://github.com/sorin-ionescu/prezto/blob/a84ac5b0023d71c98bb28a68c550dc13f6c51945/modules/editor/init.zsh#L303-L304).
## License
This project is licensed under [MIT license](http://opensource.org/licenses/MIT).
For the full text of the license, see the [LICENSE](LICENSE) file.

View File

@@ -1,22 +0,0 @@
# zsh-autosuggestions
> [Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh.
> [Shelr demo](http://shelr.tv/records/527007a99660803c5d000048).
## Installation
```zsh
git clone git://github.com/tarruda/zsh-autosuggestions ~/.zsh-autosuggestions
cat >> ~/.zshrc << "EOF"
source ~/.zsh-autosuggestions/autosuggestions.zsh
# Enable autosuggestions automatically
zle-line-init() {
zle autosuggest-start
}
zle -N zle-line-init
# use ctrl+t to toggle autosuggestions(hopefully this wont be needed)
bindkey '^T' autosuggest-toggle
# use ctrl+f to accept a suggested word
bindkey '^F' autosuggest-accept-suggested-word
EOF
```

1
autosuggestions.plugin.zsh Symbolic link
View File

@@ -0,0 +1 @@
autosuggestions.zsh

View File

@@ -1,5 +1,5 @@
# Fish-like autosuggestions for zsh. Some of the code was based on the code
# for 'predict-on'
# for 'predict-on'
#
# ```zsh
# zle-line-init() {
@@ -11,133 +11,151 @@ zmodload zsh/net/socket
source "${0:a:h}/completion-client.zsh"
# configuration variables
AUTOSUGGESTION_HIGHLIGHT_COLOR='fg=8'
AUTOSUGGESTION_HIGHLIGHT_CURSOR=1
function {
[[ -n $ZLE_DISABLE_AUTOSUGGEST ]] && return
autosuggest-ensure-server
if [[ -n $ZLE_DISABLE_AUTOSUGGEST ]]; then
ZSH_HIGHLIGHT_HIGHLIGHTERS=()
return
fi
autoload -U is-at-least
# if is-at-least 5.0.3; then
# autosuggest-ensure-server
# fi
}
ZLE_AUTOSUGGEST_PAUSE_WIDGETS=(
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
history-search-forward history-search-backward up-line-or-history
down-line-or-history
ZLE_AUTOSUGGEST_SUSPEND_WIDGETS=(
vi-cmd-mode vi-backward-char backward-char backward-word beginning-of-line
history-search-forward history-search-backward up-line-or-history
history-beginning-search-forward history-beginning-search-backward
down-line-or-history history-substring-search-up history-substring-search-down
backward-kill-word
)
ZLE_AUTOSUGGEST_COMPLETION_WIDGETS=(
complete-word expand-or-complete expand-or-complete-prefix list-choices
menu-complete reverse-menu-complete menu-expand-or-complete menu-select
accept-and-menu-complete
complete-word expand-or-complete expand-or-complete-prefix list-choices
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 vi-end-of-line end-of-line
)
ZLE_AUTOSUGGEST_ALL_WIDGETS=(
self-insert magic-space backward-delete-char accept-line
$ZLE_AUTOSUGGEST_ACCEPT_WIDGETS
$ZLE_AUTOSUGGEST_SUSPEND_WIDGETS
$ZLE_AUTOSUGGEST_COMPLETION_WIDGETS
)
autosuggest-pause() {
[[ -z $ZLE_AUTOSUGGESTING ]] && return
unset ZLE_AUTOSUGGESTING
local widget
# Restore standard widgets except for self-insert, which triggers resume
autosuggest-restore-widgets
zle -A autosuggest-paused-self-insert self-insert
# When autosuggestions are disabled, kill the unmaterialized part
RBUFFER=''
zle -A self-insert autosuggest-paused-self-insert
zle -A .magic-space magic-space
zle -A .backward-delete-char backward-delete-char
zle -A .accept-line accept-line
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
eval "zle -A autosuggest-${widget}-orig ${widget}"
done
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
eval "zle -A autosuggest-${widget}-orig $widget"
done
autosuggest-highlight-suggested-text
zle -F $ZLE_AUTOSUGGEST_CONNECTION
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
zle -F $ZLE_AUTOSUGGEST_CONNECTION
fi
}
autosuggest-resume() {
[[ -n $ZLE_AUTOSUGGESTING ]] && return
ZLE_AUTOSUGGESTING=1
local widget
# Replace prediction widgets by versions that will also highlight RBUFFER
zle -N self-insert autosuggest-insert-or-space
zle -N magic-space autosuggest-insert-or-space
zle -N backward-delete-char autosuggest-backward-delete-char
zle -N accept-line autosuggest-accept-line
# Hook into some default widgets that should pause autosuggestion
# automatically
for widget in $ZLE_AUTOSUGGEST_PAUSE_WIDGETS; do
eval "zle -A $widget autosuggest-${widget}-orig; \
zle -A autosuggest-suspend $widget"
done
# Hook into completion widgets to handle suggestions after completions
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
eval "zle -A $widget autosuggest-${widget}-orig; \
zle -A autosuggest-tab $widget"
done
if [[ $BUFFER != '' ]]; then
autosuggest-request-suggestion
fi
autosuggest-hook-widgets
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
# install listen for suggestions asynchronously
zle -F $ZLE_AUTOSUGGEST_CONNECTION autosuggest-pop-suggestion
zle -Fw $ZLE_AUTOSUGGEST_CONNECTION autosuggest-pop-suggestion
fi
}
autosuggest-start() {
if [[ -z $ZLE_DISABLE_AUTOSUGGEST && -n $functions[_zsh_highlight] ]]; then
if [[ ${ZSH_HIGHLIGHT_HIGHLIGHTERS[(i)autosuggest]} -gt ${#ZSH_HIGHLIGHT_HIGHLIGHTERS} ]]; then
ZSH_HIGHLIGHT_HIGHLIGHTERS+=(autosuggest)
fi
fi
autosuggest-resume
zle recursive-edit
integer rv=$?
autosuggest-pause
zle -A .self-insert self-insert
(( rv )) || zle accept-line
return rv
}
# Toggles autosuggestions on/off
autosuggest-toggle() {
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
autosuggest-pause
zle -A .self-insert self-insert
else
autosuggest-resume
fi
}
autosuggest-highlight-suggested-text() {
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
local color='fg=8'
[[ -n $AUTOSUGGESTION_HIGHLIGHT_COLOR ]] &&\
color=$AUTOSUGGESTION_HIGHLIGHT_COLOR
region_highlight=("$(( $CURSOR + 1 )) $(( $CURSOR + $#RBUFFER )) $color")
if (( $+functions[_zsh_highlight_buffer_modified] > 0 )); then
_zsh_highlight
else
region_highlight=()
_zsh_highlight_autosuggest_highlighter
fi
}
_zsh_highlight_autosuggest_highlighter_predicate() {
[[ -n $ZLE_AUTOSUGGESTING ]] && (( $#RBUFFER > 0 ))
}
_zsh_highlight_autosuggest_highlighter() {
region_highlight+=("$(( $CURSOR + $AUTOSUGGESTION_HIGHLIGHT_CURSOR )) $(( $CURSOR + $#RBUFFER )) $AUTOSUGGESTION_HIGHLIGHT_COLOR")
}
autosuggest-insert-or-space() {
setopt localoptions noshwordsplit noksharrays
if [[ $LBUFFER == *$'\012'* ]] || (( PENDING )); then
# Editing a multiline buffer or pasting in a chunk of text, dont
# autosuggest
zle .$WIDGET "$@"
elif [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
# Same as what's typed, just move on
((++CURSOR))
autosuggest-highlight-suggested-text
else
LBUFFER="$LBUFFER$KEYS"
autosuggest-request-suggestion
fi
}
autosuggest-backward-delete-char() {
if ! (( $CURSOR )); then
zle .kill-whole-line
# Editing multiline buffer or pasting a chunk of text, pause
autosuggest-suspend
return
fi
if [[ $LBUFFER == *$'\012'* || $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
# When editing a multiline buffer or if the last widget was e.g. a motion,
# then probably the intent is to actually edit the line, not change the
# search prefix.
LBUFFER="$LBUFFER[1,-2]"
if [[ ${RBUFFER[1]} == ${KEYS[-1]} ]]; then
# Same as what's typed, just move on
((++CURSOR))
autosuggest-invalidate-highlight-cache
else
((--CURSOR))
zle .history-beginning-search-forward || RBUFFER=''
LBUFFER="$LBUFFER$KEYS"
if [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) || $LASTWIDGET == (complete-word|accept-*|zle-line-init) ]]; then
if ! zle .history-beginning-search-backward; then
RBUFFER=''
if [[ ${KEYS[-1]} != ' ' ]]; then
autosuggest-send-request ${LBUFFER}
fi
fi
fi
fi
autosuggest-highlight-suggested-text
}
autosuggest-backward-delete-char() {
if (( $#LBUFFER > 1 )); then
setopt localoptions noshwordsplit noksharrays
if [[ $LBUFFER = *$'\012'* || $LASTWIDGET != (self-insert|magic-space|backward-delete-char) ]]; then
LBUFFER="$LBUFFER[1,-2]"
else
((--CURSOR))
autosuggest-invalidate-highlight-cache
zle .history-beginning-search-forward || RBUFFER=''
fi
autosuggest-highlight-suggested-text
else
zle .kill-whole-line
fi
}
@@ -145,15 +163,18 @@ autosuggest-backward-delete-char() {
# section when the user accepts the line
autosuggest-accept-line() {
RBUFFER=''
region_highlight=()
if ! (( $+functions[_zsh_highlight_buffer_modified] )); then
# Only clear the colors if the user doesn't have zsh-highlight installed
region_highlight=()
fi
zle .accept-line
}
autosuggest-paused-self-insert() {
if [[ $RBUFFER == '' ]]; then
# Resume autosuggestions when inserting at the end of the line
autosuggest-enable
zle autosuggest-modify
autosuggest-resume
zle self-insert
else
zle .self-insert
fi
@@ -187,42 +208,87 @@ autosuggest-pop-suggestion() {
zle -Rc
}
autosuggest-request-suggestion() {
if (( $CURSOR == 0 )) || [[ ${LBUFFER[-1]} == ' ' ]]; then
RBUFFER=''
return
fi
[[ -n $ZLE_DISABLE_AUTOSUGGEST || $LBUFFER == '' ]] && return
zle .history-beginning-search-backward ||\
autosuggest-first-completion ${LBUFFER}
autosuggest-highlight-suggested-text
}
autosuggest-suspend() {
autosuggest-pause
zle autosuggest-${WIDGET}-orig "$@"
zle .${WIDGET} "$@"
}
autosuggest-tab() {
RBUFFER=''
zle autosuggest-${WIDGET}-orig "$@"
zle .${WIDGET} "$@"
autosuggest-invalidate-highlight-cache
autosuggest-highlight-suggested-text
}
autosuggest-accept-suggested-small-word() {
zle .vi-forward-word
autosuggest-highlight-suggested-text
autosuggest-accept-suggestion() {
if [[ AUTOSUGGESTION_ACCEPT_RIGHT_ARROW -eq 1 && ("$WIDGET" == 'forward-char' || "$WIDGET" == 'vi-forward-char') ]]; then
zle .end-of-line "$@"
else
zle .${WIDGET} "$@"
fi
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
autosuggest-invalidate-highlight-cache
autosuggest-highlight-suggested-text
fi
}
autosuggest-accept-suggested-word() {
zle .forward-word
autosuggest-highlight-suggested-text
autosuggest-execute-suggestion() {
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
zle .end-of-line
autosuggest-invalidate-highlight-cache
autosuggest-highlight-suggested-text
fi
zle .accept-line
}
autosuggest-invalidate-highlight-cache() {
# invalidate the buffer for zsh-syntax-highlighting
_zsh_highlight_autosuggest_highlighter_cache=()
}
autosuggest-restore-widgets() {
for widget in $ZLE_AUTOSUGGEST_ALL_WIDGETS; do
[[ -z $widgets[$widget] ]] && continue
zle -A .${widget} ${widget}
done
}
autosuggest-hook-widgets() {
local widget
# Replace prediction widgets by versions that will also highlight RBUFFER
zle -A autosuggest-insert-or-space self-insert
zle -A autosuggest-insert-or-space magic-space
zle -A autosuggest-backward-delete-char backward-delete-char
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"
done
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
[[ -z $widgets[$widget] ]] && continue
eval "zle -A autosuggest-tab $widget"
done
}
zle -N autosuggest-toggle
zle -N autosuggest-start
zle -N autosuggest-accept-suggested-small-word
zle -N autosuggest-accept-suggested-word
zle -N autosuggest-suspend
zle -N autosuggest-execute-suggestion
zle -N autosuggest-paused-self-insert
zle -N autosuggest-insert-or-space
zle -N autosuggest-backward-delete-char
zle -N autosuggest-accept-line
zle -N autosuggest-tab
zle -N autosuggest-suspend
zle -N autosuggest-accept-suggestion
autosuggest-restore-widgets

View File

@@ -9,7 +9,7 @@ autosuggest-ensure-server() {
local pid_file="$server_dir/pid"
local socket_path="$server_dir/socket"
if [[ ! -S $socket_path || ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
if [[ ! -d $server_dir || ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
if which setsid &> /dev/null; then
setsid zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
else
@@ -33,9 +33,8 @@ autosuggest-server-connect() {
ZLE_AUTOSUGGEST_CONNECTION=$REPLY
}
autosuggest-first-completion() {
autosuggest-send-request() {
[[ -z $ZLE_AUTOSUGGEST_CONNECTION ]] && return 1
setopt local_options noglob
local response
print -u $ZLE_AUTOSUGGEST_CONNECTION - $1 &> /dev/null || return 1
}

View File

@@ -48,7 +48,7 @@ zstyle -d ':completion:*' list-colors
zmodload zsh/zutil
# override compadd (this our hook)
compadd () {
compadd() {
# check if any of -O, -A or -D are given
if [[ ${@[1,(i)(-|--)]} == *-(O|A|D)\ * ]]; then

View File

@@ -14,7 +14,7 @@ read-to-null() {
accept-connection() {
zsocket -a $server
fds[$REPLY]=1
print "connection accepted, fd: $REPLY"
print "connection accepted, fd: $REPLY" >&2
}
handle-request() {
@@ -44,8 +44,8 @@ handle-request() {
break # handle more requests/return to zselect
done
if ! (( read_something )); then
print "connection with fd $connection closed"
unset fds[$connection]
print "connection with fd $connection closed" >&2
unset fds[$connection]
exec {connection}>&- # free the file descriptor
fi
}
@@ -69,7 +69,7 @@ zmodload zsh/zpty
zmodload zsh/zselect
zmodload zsh/net/socket
setopt noglob
print "autosuggestion server started, pid: $$"
print "autosuggestion server started, pid: $$" >&2
# Start an interactive zsh connected to a zpty
zpty z ZLE_DISABLE_AUTOSUGGEST=1 zsh -i
@@ -96,10 +96,10 @@ cleanup() {
trap cleanup TERM INT HUP EXIT
mkdir -m 700 $server_dir &> /dev/null
mkdir -m 700 $server_dir
while ! zsocket -l $socket_path; do
if [[ ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
if [[ ! -r $pid_file ]] || ! kill -0 $(<$pid_file); then
rm -f $socket_path
else
exit 1

44
install Executable file
View File

@@ -0,0 +1,44 @@
#!/bin/bash
# Install script for zsh-autocomplete
config="$HOME/.zshrc"
for config in "$HOME/.zshrc" "$ZDOTDIR/.zshrc" "$1"; do
echo $config
#first checks if ~/.zshrc file exists and is readable
if [ -r "$config" ]; then
break
elif [ "$config" = "$1" ]; then
echo "\nError: Please specify as first argument the file in which to load zsh-autosuggestions (usually ~/.zshrc)!\n"
exit 1
fi
done
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# appends the string to $config (usually ~/.zshrc) file
cat >> "$config" <<-EOF
# Setup zsh-autosuggestions
source $DIR/autosuggestions.zsh
# Enable autosuggestions automatically
zle-line-init() {
zle autosuggest-start
}
zle -N zle-line-init
# use ctrl+t to toggle autosuggestions(hopefully this wont be needed as
# zsh-autosuggestions is designed to be unobtrusive)
bindkey '^T' autosuggest-toggle
EOF
echo "\nSetup completed successfully!\n"
exit 0

View File

@@ -0,0 +1 @@
autosuggestions.zsh