庆典扩大CD像zsh的快捷键 [英] bash expand cd with shortcuts like zsh

查看:224
本文介绍了庆典扩大CD像zsh的快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在bash扩大类似

Is it possible in bash to expand something like

CD / U / LO / B <打标签>

cd /u/lo/b<hit tab>

CD的/ usr / local / bin目录

推荐答案

抱歉,我不能更早的帖子,我在工作举行,绑定功能更为问题频发,比我第一个想到的。

Sorry I couldn't post earlier, I was held at work, and the bind function was more issue-prone than I first thought.

下面是我想出了:

绑定下面的脚本:

#!/bin/bash
#$HOME/.bashrc.d/autocomplete.sh
autocomplete_wrapper() {
    BASE="${READLINE_LINE% *} "           #we save the line except for the last argument
    [[ "$BASE" == "$READLINE_LINE " ]] && BASE="";  #if the line has only 1 argument, we set the BASE to blank
    EXPANSION=($(autocomplete "${READLINE_LINE##* }"))
    [[ ${#EXPANSION[@]} -gt 1 ]] && echo "${EXPANSION[@]:1}"  #if there is more than 1 match, we echo them
    READLINE_LINE="$BASE${EXPANSION[0]}"  #the current line is now the base + the 1st element
    READLINE_POINT=${#READLINE_LINE}      #we move our cursor at the end of the current line
}

autocomplete() {
    LAST_CMD="$1"
    #Special starting character expansion for '~', './' and '/'
    [[ "${LAST_CMD:0:1}" == "~" ]] && LAST_CMD="$HOME${LAST_CMD:1}"
    S=1; [[ "${LAST_CMD:0:1}" == "/" || "${LAST_CMD:0:2}" == "./" ]] && S=2; #we don't expand those

    #we do the path expansion of the last argument here by adding a * before each /
    EXPANSION=($(echo "$LAST_CMD*" | sed s:/:*/:"$S"g))

    if [[ ! -e "${EXPANSION[0]}" ]];then #if the path cannot be expanded, we don't change the output
        echo "$LAST_CMD"
    elif [[ "${#EXPANSION[@]}" -eq 1 ]];then #else if there is only one match, we output it
        echo "${EXPANSION[0]}"
    else
        #else we expand the path as much as possible and return all the possible results
        while [[ $l -le "${#EXPANSION[0]}" ]]; do
            for i in "${EXPANSION[@]}"; do
                if [[ "${EXPANSION[0]:$l:1}" != "${i:$l:1}" ]]; then
                    CTRL_LOOP=1
                    break
                fi
            done
            [[ $CTRL_LOOP -eq 1 ]] && break
            ((l++))
        done
        #we add the partial solution at the beggining of the array of solutions
        echo "${EXPANSION[0]:0:$l} ${EXPANSION[@]}"
    fi
}

用下面的命令:

    source "$HOME/.bashrc.d/autocomplete.sh" 
    bind -x '"\t" : autocomplete_wrapper'

输出:

>$ cd /u/lo/b<TAB>
>$ cd /usr/local/bin


>$ cd /u/l<TAB>
/usr/local /usr/lib
>$ cd /usr/l

绑定行可以添加到您的〜/ .bashrc中文件,做这样的事情:

if [[ -s "$HOME/.bashrc.d/autocomplete.sh" ]]; then
    source "$HOME/.bashrc.d/autocomplete.sh" 
    bind -x '"\t" : autocomplete_wrapper'
fi

(从<一所href=\"http://stackoverflow.com/questions/25178632/modify-readline-line-and-readline-point-values-inside-bash-script/25201467#25201467\">this答案)

此外,我会强烈建议不要这个命令绑定到你的<大骨节病>标签键,因为它会覆盖默认的自动完成。

Furthermore, I would strongly advise against binding this command to your Tab key as it would override the default autocomplete.

注意:在某些情况下,这会表现不好,对isntance如果您尝试自动完成/路径/与空间/东西,为完成最后一个参数是由 $ {确定## READLINE_LINE *} 。如果这是你的情况的问题,您应该code考虑报价时返回行的最后一个参数的函数

Note: In some cases, this will misbehave, for isntance if you try to autocomplete "/path/with spaces/something", as the last argument to complete is determined by ${READLINE_LINE##* }. If this is an issue in your case, you should code a function that returns the last argument of a line when considering quotes

随意要求进一步澄清,我欢迎任何建议,以改善这个剧本

Feel free to ask for further clarification, and I welcome any suggestion to improve this script

这篇关于庆典扩大CD像zsh的快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆