Mac 终端实现快速定位命令 自动补全目录

基于macOS
oh-my-zsh 切换终端主题
incr.zsh 实现快速定位命令 自动补全目录

效果预览

Mac 终端实现快速定位命令 自动补全目录_第1张图片

步骤

1、安装 oh-my-zsh

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Mac 终端实现快速定位命令 自动补全目录_第2张图片

2、下载 incr.zsh

进入oh-my-zsh的插件目录

cd ~/.oh-my-zsh/plugins/

新建incr文件夹,并将下载的incr.zsh文件拷贝到该目录下

Mac 终端实现快速定位命令 自动补全目录_第3张图片

然后在/.oh-my-zsh/plugins/incr路径下执行终端指令

source incr*.zsh

3、更改 .zshrc文件

vim ~/.zshrc

在文件末尾添加下面内容,保存并退出

source ~/.oh-my-zsh/plugins/incr/incr*.zsh

然后终端执行 source ~/.zshrc 更新配置

over over over ,打开一个新的终端窗口输入命令测试下吧

常见问题

1、终端命令提示时由于提示项超过6个显示 too many matches.

解决方法:
编辑 ~/.oh-my-zsh/plugins/incr/incr-02.zsh,将文件中的zle -M "too many matches." 所在行前面添加 # 注释掉即可解决

Mac 终端实现快速定位命令 自动补全目录_第4张图片

以下内容为 incr-02.zsh 的备份文件

# Incremental completion for zsh
# by y.fujii , public domain


autoload -U compinit
zle -N self-insert self-insert-incr
zle -N vi-cmd-mode-incr
zle -N vi-backward-delete-char-incr
zle -N backward-delete-char-incr
zle -N expand-or-complete-prefix-incr
compinit

bindkey -M viins '^[' vi-cmd-mode-incr
bindkey -M viins '^h' vi-backward-delete-char-incr
bindkey -M viins '^?' vi-backward-delete-char-incr
bindkey -M viins '^i' expand-or-complete-prefix-incr
bindkey -M emacs '^h' backward-delete-char-incr
bindkey -M emacs '^?' backward-delete-char-incr
bindkey -M emacs '^i' expand-or-complete-prefix-incr

unsetopt automenu
compdef -d scp
compdef -d tar
compdef -d make
compdef -d java
compdef -d svn
compdef -d cvs

# TODO:
#     cp dir/

now_predict=0

function limit-completion
{
    if ((compstate[nmatches] <= 1)); then
        zle -M ""
    elif ((compstate[list_lines] > 6)); then
        compstate[list]=""
        zle -M "too many matches."
    fi
}

function correct-prediction
{
    if ((now_predict == 1)); then
        if [[ "$BUFFER" != "$buffer_prd" ]] || ((CURSOR != cursor_org)); then
            now_predict=0
        fi
    fi
}

function remove-prediction
{
    if ((now_predict == 1)); then
        BUFFER="$buffer_org"
        now_predict=0
    fi
}

function show-prediction
{
    # assert(now_predict == 0)
    if
        ((PENDING == 0)) &&
        ((CURSOR > 1)) &&
        [[ "$PREBUFFER" == "" ]] &&
        [[ "$BUFFER[CURSOR]" != " " ]]
    then
        cursor_org="$CURSOR"
        buffer_org="$BUFFER"
        comppostfuncs=(limit-completion)
        zle complete-word
        cursor_prd="$CURSOR"
        buffer_prd="$BUFFER"
        if [[ "$buffer_org[1,cursor_org]" == "$buffer_prd[1,cursor_org]" ]]; then
            CURSOR="$cursor_org"
            if [[ "$buffer_org" != "$buffer_prd" ]] || ((cursor_org != cursor_prd)); then
                now_predict=1
            fi
        else
            BUFFER="$buffer_org"
            CURSOR="$cursor_org"
        fi
        echo -n "\e[32m"
    else
        zle -M ""
    fi
}

function preexec
{
    echo -n "\e[39m"
}

function vi-cmd-mode-incr
{
    correct-prediction
    remove-prediction
    zle vi-cmd-mode
}

function self-insert-incr
{
    correct-prediction
    remove-prediction
    if zle .self-insert; then
        show-prediction
    fi
}

function vi-backward-delete-char-incr
{
    correct-prediction
    remove-prediction
    if zle vi-backward-delete-char; then
        show-prediction
    fi
}

function backward-delete-char-incr
{
    correct-prediction
    remove-prediction
    if zle backward-delete-char; then
        show-prediction
    fi
}

function expand-or-complete-prefix-incr
{
    correct-prediction
    if ((now_predict == 1)); then
        CURSOR="$cursor_prd"
        now_predict=0
        comppostfuncs=(limit-completion)
        zle list-choices
    else
        remove-prediction
        zle expand-or-complete-prefix
    fi
}

你可能感兴趣的:(Mac 终端实现快速定位命令 自动补全目录)