mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2025-12-06 15:20:40 +01:00
Compare commits
43 Commits
0.0.1
...
pre-v0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7dc9e0f926 | ||
|
|
8777836a1c | ||
|
|
7a00bfa444 | ||
|
|
0ad9525955 | ||
|
|
ec1d251dad | ||
|
|
b1725e9e74 | ||
|
|
c044d06f70 | ||
|
|
fc4576758c | ||
|
|
cbaf7ced6e | ||
|
|
07165cd3ec | ||
|
|
dde838c226 | ||
|
|
a796ce5bdb | ||
|
|
40a8d73054 | ||
|
|
6de948c9c5 | ||
|
|
c6afbaf254 | ||
|
|
47283c2619 | ||
|
|
f090f609ac | ||
|
|
08f3b3148d | ||
|
|
8d29045ffb | ||
|
|
bbd5300d60 | ||
|
|
5b23a17241 | ||
|
|
2d5ac282a9 | ||
|
|
62af373a59 | ||
|
|
258e06c9ab | ||
|
|
f860711cc5 | ||
|
|
0371904875 | ||
|
|
4b6512d1e8 | ||
|
|
34c01f6259 | ||
|
|
e637eb58eb | ||
|
|
1bf5de2acc | ||
|
|
9b565ff6b5 | ||
|
|
24ac946dd9 | ||
|
|
59699a0946 | ||
|
|
d1640a399d | ||
|
|
533325c39d | ||
|
|
fe6e9812b0 | ||
|
|
c740515df4 | ||
|
|
3d5beea5c3 | ||
|
|
a23ad43188 | ||
|
|
c3e7668fbb | ||
|
|
8970e71d50 | ||
|
|
89b85d88cc | ||
|
|
d76af3c076 |
10
.editorconfig
Normal file
10
.editorconfig
Normal 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
145
README.md
Normal 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 don’t 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 you’ve added during “installation.”
|
||||
If you don’t 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 won’t 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.
|
||||
32
README.mkd
32
README.mkd
@@ -1,32 +0,0 @@
|
||||
# zsh-autosuggestions
|
||||
|
||||
> [Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh.
|
||||
|
||||
## Installation
|
||||
```
|
||||
$ git clone git://github.com/tarruda/zsh-autosuggestions ~/.zsh-autosuggestions
|
||||
sh .zsh-autosuggestions/install
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## Configuration
|
||||
|
||||
You may override default global config variables after plugin load.
|
||||
|
||||
- `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`.
|
||||
@@ -43,24 +43,26 @@ 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 end-of-line
|
||||
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 autosuggest-paused-self-insert self-insert
|
||||
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_ACCEPT_WIDGETS $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
||||
[[ -z $widgets[$widget] || -z $widgets[autosuggest-${widget}-orig] ]] &&\
|
||||
continue
|
||||
eval "zle -A autosuggest-${widget}-orig ${widget}"
|
||||
done
|
||||
autosuggest-highlight-suggested-text
|
||||
|
||||
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
||||
@@ -71,27 +73,7 @@ autosuggest-pause() {
|
||||
autosuggest-resume() {
|
||||
[[ -n $ZLE_AUTOSUGGESTING ]] && return
|
||||
ZLE_AUTOSUGGESTING=1
|
||||
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
|
||||
# Hook into completion widgets to trim RBUFFER before completion
|
||||
for widget in $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
|
||||
[[ -z $widgets[$widget] ]] && continue
|
||||
eval "zle -A autosuggest-tab $widget"
|
||||
done
|
||||
autosuggest-hook-widgets
|
||||
if [[ -n $ZLE_AUTOSUGGEST_CONNECTION ]]; then
|
||||
# install listen for suggestions asynchronously
|
||||
zle -Fw $ZLE_AUTOSUGGEST_CONNECTION autosuggest-pop-suggestion
|
||||
@@ -111,7 +93,7 @@ autosuggest-start() {
|
||||
autosuggest-toggle() {
|
||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
||||
autosuggest-pause
|
||||
zle -A autosuggest-self-insert-orig self-insert
|
||||
zle -A .self-insert self-insert
|
||||
else
|
||||
autosuggest-resume
|
||||
fi
|
||||
@@ -163,6 +145,7 @@ autosuggest-insert-or-space() {
|
||||
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
|
||||
@@ -193,7 +176,7 @@ autosuggest-paused-self-insert() {
|
||||
autosuggest-resume
|
||||
zle self-insert
|
||||
else
|
||||
zle autosuggest-self-insert-orig
|
||||
zle .self-insert
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -227,20 +210,21 @@ autosuggest-pop-suggestion() {
|
||||
|
||||
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-suggestion() {
|
||||
if [[ AUTOSUGGESTION_ACCEPT_RIGHT_ARROW -eq 1 && "$WIDGET" == 'forward-char' ]]; then
|
||||
zle autosuggest-end-of-line-orig "$@"
|
||||
if [[ AUTOSUGGESTION_ACCEPT_RIGHT_ARROW -eq 1 && ("$WIDGET" == 'forward-char' || "$WIDGET" == 'vi-forward-char') ]]; then
|
||||
zle .end-of-line "$@"
|
||||
else
|
||||
zle autosuggest-${WIDGET}-orig "$@"
|
||||
zle .${WIDGET} "$@"
|
||||
fi
|
||||
if [[ -n $ZLE_AUTOSUGGESTING ]]; then
|
||||
autosuggest-invalidate-highlight-cache
|
||||
@@ -248,15 +232,55 @@ autosuggest-accept-suggestion() {
|
||||
fi
|
||||
}
|
||||
|
||||
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_PRIOR_BUFFER=''
|
||||
_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-execute-suggestion
|
||||
|
||||
zle -N autosuggest-paused-self-insert
|
||||
zle -N autosuggest-insert-or-space
|
||||
@@ -267,13 +291,4 @@ 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_ACCEPT_WIDGETS} ${ZLE_AUTOSUGGEST_SUSPEND_WIDGETS} ${ZLE_AUTOSUGGEST_COMPLETION_WIDGETS}; do
|
||||
[[ -z $widgets[$widget] ]] && continue
|
||||
eval "zle -A $widget autosuggest-${widget}-orig"
|
||||
done
|
||||
autosuggest-restore-widgets
|
||||
|
||||
24
install
24
install
@@ -1,17 +1,29 @@
|
||||
#!/bin/sh
|
||||
#!/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 ~/.zshrc ]; then
|
||||
echo "\nError: ~/.zshrc file does not exist or is not readable!\n"
|
||||
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
|
||||
|
||||
DIR=$(dirname $(readlink -e $0)) ;
|
||||
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 ~/.zshrc file
|
||||
cat >> ~/.zshrc << EOF
|
||||
# appends the string to $config (usually ~/.zshrc) file
|
||||
cat >> "$config" <<-EOF
|
||||
|
||||
# Setup zsh-autosuggestions
|
||||
source $DIR/autosuggestions.zsh
|
||||
|
||||
1
zsh-autosuggestions.plugin.zsh
Symbolic link
1
zsh-autosuggestions.plugin.zsh
Symbolic link
@@ -0,0 +1 @@
|
||||
autosuggestions.zsh
|
||||
Reference in New Issue
Block a user