zsh-autosuggestions

Fish-like autosuggestions for zsh
授权协议 MIT License
开发语言 SHELL
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 闻人宇定
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

zsh-autosuggestions

Fish-like fast/unobtrusive autosuggestions for zsh.

It suggests commands as you type based on history and completions.

Requirements: Zsh v4.3.11 or later

Installation

See INSTALL.md.

Usage

As you type commands, you will see a completion offered after the cursor in a muted gray color. This color can be changed by setting the ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE variable. See configuration.

If you press the key (forward-char widget) or End (end-of-line widget) with the cursor at the end of the buffer, it will accept the suggestion, replacing the contents of the command line buffer with the suggestion.

If you invoke the forward-word widget, it will partially accept the suggestion up to the point that the cursor moves to.

Configuration

You may want to override the default global config variables. Default values of these variables can be found here.

Note: If you are using Oh My Zsh, you can put this configuration in a file in the $ZSH_CUSTOM directory. See their comments on overriding internals.

Suggestion Highlight Style

Set ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE to configure the style that the suggestion is shown with. The default is fg=8, which will set the foreground color to color 8 from the 256-color palette. If your terminal only supports 8 colors, you will need to use a number between 0 and 7.

Background color can also be set, and the suggestion can be styled bold, underlined, or standout. For example, this would show suggestions with bold, underlined, pink text on a cyan background:

ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ff00ff,bg=cyan,bold,underline"

For more info, read the Character Highlighting section of the zsh manual: man zshzle or online.

Note: Some iTerm2 users have reported not being able to see the suggestions. If this affects you, the problem is likely caused by incorrect color settings. In order to correct this, go into iTerm2's setting, navigate to profile > colors and make sure that the colors for Basic Colors > Background and ANSI Colors > Bright Black are different.

Suggestion Strategy

ZSH_AUTOSUGGEST_STRATEGY is an array that specifies how suggestions should be generated. The strategies in the array are tried successively until a suggestion is found. There are currently three built-in strategies to choose from:

  • history: Chooses the most recent match from history.
  • completion: Chooses a suggestion based on what tab-completion would suggest. (requires zpty module)
  • match_prev_cmd: Like history, but chooses the most recent match whose preceding history item matches the most recently executed command (more info). Note that this strategy won't work as expected with ZSH options that don't preserve the history order such as HIST_IGNORE_ALL_DUPS or HIST_EXPIRE_DUPS_FIRST.

For example, setting ZSH_AUTOSUGGEST_STRATEGY=(history completion) will first try to find a suggestion from your history, but, if it can't find a match, will find a suggestion from the completion engine.

Widget Mapping

This plugin works by triggering custom behavior when certain zle widgets are invoked. You can add and remove widgets from these arrays to change the behavior of this plugin:

  • ZSH_AUTOSUGGEST_CLEAR_WIDGETS: Widgets in this array will clear the suggestion when invoked.
  • ZSH_AUTOSUGGEST_ACCEPT_WIDGETS: Widgets in this array will accept the suggestion when invoked.
  • ZSH_AUTOSUGGEST_EXECUTE_WIDGETS: Widgets in this array will execute the suggestion when invoked.
  • ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS: Widgets in this array will partially accept the suggestion when invoked.
  • ZSH_AUTOSUGGEST_IGNORE_WIDGETS: Widgets in this array will not trigger any custom behavior.

Widgets that modify the buffer and are not found in any of these arrays will fetch a new suggestion after they are invoked.

Note: A widget shouldn't belong to more than one of the above arrays.

Disabling suggestion for large buffers

Set ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE to an integer value to disable autosuggestion for large buffers. The default is unset, which means that autosuggestion will be tried for any buffer size. Recommended value is 20.This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for strings that are too long.

Asynchronous Mode

Suggestions are fetched asynchronously by default in zsh versions 5.0.8 and greater. To disable asynchronous suggestions and fetch them synchronously instead, unset ZSH_AUTOSUGGEST_USE_ASYNC after sourcing the plugin.

Alternatively, if you are using a version of zsh older than 5.0.8 and want to enable asynchronous mode, set the ZSH_AUTOSUGGEST_USE_ASYNC variable after sourcing the plugin (it can be set to anything). Note that there is a bug in versions of zsh older than 5.0.8 where ctrl + c will fail to reset the prompt immediately after fetching a suggestion asynchronously.

Disabling automatic widget re-binding

Set ZSH_AUTOSUGGEST_MANUAL_REBIND (it can be set to anything) to disable automatic widget re-binding on each precmd. This can be a big boost to performance, but you'll need to handle re-binding yourself if any of the widget lists change or if you or another plugin wrap any of the autosuggest widgets. To re-bind widgets, run _zsh_autosuggest_bind_widgets.

Ignoring history suggestions that match a pattern

Set ZSH_AUTOSUGGEST_HISTORY_IGNORE to a glob pattern to prevent offering suggestions for history entries that match the pattern. For example, set it to "cd *" to never suggest any cd commands from history. Or set to "?(#c50,)" to never suggest anything 50 characters or longer.

Note: This only affects the history and match_prev_cmd suggestion strategies.

Skipping completion suggestions for certain cases

Set ZSH_AUTOSUGGEST_COMPLETION_IGNORE to a glob pattern to prevent offering completion suggestions when the buffer matches that pattern. For example, set it to "git *" to disable completion suggestions for git subcommands.

Note: This only affects the completion suggestion strategy.

Key Bindings

This plugin provides a few widgets that you can use with bindkey:

  1. autosuggest-accept: Accepts the current suggestion.
  2. autosuggest-execute: Accepts and executes the current suggestion.
  3. autosuggest-clear: Clears the current suggestion.
  4. autosuggest-fetch: Fetches a suggestion (works even when suggestions are disabled).
  5. autosuggest-disable: Disables suggestions.
  6. autosuggest-enable: Re-enables suggestions.
  7. autosuggest-toggle: Toggles between enabled/disabled suggestions.

For example, this would bind ctrl + space to accept the current suggestion.

bindkey '^ ' autosuggest-accept

Troubleshooting

If you have a problem, please search through the list of issues on GitHub to see if someone else has already reported it.

Reporting an Issue

Before reporting an issue, please try temporarily disabling sections of your configuration and other plugins that may be conflicting with this plugin to isolate the problem.

When reporting an issue, please include:

  • The smallest, simplest .zshrc configuration that will reproduce the problem. See this comment for a good example of what this means.
  • The version of zsh you're using (zsh --version)
  • Which operating system you're running

Uninstallation

  1. Remove the code referencing this plugin from ~/.zshrc.

  2. Remove the git repository from your hard drive

    rm -rf ~/.zsh/zsh-autosuggestions # Or wherever you installed

Development

Build Process

Edit the source files in src/. Run make to build zsh-autosuggestions.zsh from those source files.

Pull Requests

Pull requests are welcome! If you send a pull request, please:

  • Request to merge into the develop branch (NOT master)
  • Match the existing coding conventions.
  • Include helpful comments to keep the barrier-to-entry low for people new to the project.
  • Write tests that cover your code as much as possible.

Testing

Tests are written in ruby using the rspec framework. They use tmux to drive a pseudoterminal, sending simulated keystrokes and making assertions on the terminal content.

Test files live in spec/. To run the tests, run make test. To run a specific test, run TESTS=spec/some_spec.rb make test. You can also specify a zsh binary to use by setting the TEST_ZSH_BIN environment variable (ex: TEST_ZSH_BIN=/bin/zsh make test).

A docker image for testing is available on docker hub. It comes with ruby, the bundler dependencies, and all supported versions of zsh installed.

Pull the docker image with:

docker pull ericfreese/zsh-autosuggestions-test

To run the tests for a specific version of zsh (where <version> below is substituted with the contents of a line from the ZSH_VERSIONS file):

docker run -it -e TEST_ZSH_BIN=zsh-<version> -v $PWD:/zsh-autosuggestions zsh-autosuggestions-test make test

License

This project is licensed under MIT license.For the full text of the license, see the LICENSE file.

  • zsh开启autosuggestions 插件的时候,在终端中粘贴大量的内容的时候,会粘贴的很慢,基本上是一个字符一个字符的粘贴的。 解决方案 在.zshrc文件中配置以下内容: # This speeds up pasting w/ autosuggest # https://github.com/zsh-users/zsh-autosuggestions/issues/238 pasteini

  • Clone this repository somewhere on your machine. This guide will assume ~/.zsh/zsh-autosuggestions. git clone git://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions Add the followin

  • 安装教程跟ubuntu 16.04安装教程一样,地址为: https://blog.csdn.net/w5688414/article/details/83036991   但是你再次打开terminal的时候,发现zsh没有启用,这个时候需要: chsh -s /bin/zsh # 将默认的shell从bash改为zsh sudo reboot # 重新启动 重启以后再打开terminal,发

  • 从github上克隆zsh-autosuggestions终端命令自动推荐插件不好使,使用如下命令可解决该问题 1. 下载插件到/.oh-my-zsh/custom目录中 git clone https://gitee.com/hailin_cool/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions 2. 在配置文件中

  • git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc vim ~/.zshrc source ~/.zshrc sudo apt install zsh chsh -s /bin/zsh 修改.zshrc的时候将ZSH_

  • 下载该插件到.oh-my-zsh的插件目录 git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions vim ~/.zshrc找到plugins=(git)这一行,如果没有添加。更改为如下 plugins=(git zsh-autosuggestions) 重启z

 相关资料
  • Zsh

    Z shell是一个Linux用户很少使用的power-shell,这是由于大多数Linux产品安装,以及默认使用bash shell。几乎每一款Linux产品都包含有zsh,通常可以用apt-get、urpmi或yum进行安装。

  • 我们将安装 zsh ,其拓展功能和主题将由 oh-my-zsh 提供。其中Env.sh 文件用于维护别名(aliases),输出(exports)和路径改变(path changes)等等,以免影响 ~/.zshrc。 Zsh 使用 Homebrew 完成 zsh 和 zsh completions 的安装 brew install zsh zsh-completions 安装 oh-my

  • 在~/.zshrc文件中加入如下内容: #screen integration to set caption bar dynamically function title { if [[ $TERM == "screen" || $TERM == "screen.linux" ]]; then# Use these two for GNU Screen:print -nR $'\033k'$1$

  • nvm是一个很棒的工具,但安装和保持最新可能有点痛苦。因此 zsh-nvm 插件允许快速设置nvm一次,将其保存在你的 dotfiles 中,然后再也不用担心了。 虽然这是作为 zsh 插件编写的,但如果遵循手动安装说明,它也可以与 bash 一起使用。 用法   一旦插件安装完毕,nvm 就可以使用了。你可能想把它作为你的第一个插件加载,这样 node/npm 就可以用于其他可能需要它们的插件。

  • ZSH antigen 是一个用于 zsh 的插件管理器。 使用方法: source /path-to-antigen-clone/antigen.zsh# Load the oh-my-zsh's library.antigen use oh-my-zsh# Bundles from the default repo (robbyrussell's oh-my-zsh).antigen bund

  • 本文前提 CentOS 6.7 64 bit root 用户 Zsh 介绍 Zsh 兼容 Bash,据传说 99% 的 Bash 操作 和 Zsh 是相同的 Zsh 官网:http://www.zsh.org/ 先看下你的 CentOS 支持哪些 shell:cat /etc/shells,正常结果应该是这样的: /bin/sh /bin/bash /sbin/nologin /bin/dash