2

誰かがこれを行うことができるかどうか疑問に思います。私がこのフォルダ構造を持っているとしましょう:

Folder A
    Folder Apple
    Folder Orange

現在フォルダAにいる場合は、「cd Ap」と入力してEnterキーを押すと、自動的に「Apple」サブフォルダに入れられるようにしたいと思います。基本的に、部分的な入力に基づいてフォルダをオートコンプリートして開こうとします。

現在フォルダAにいて、「cd ap」と入力してEnterキー(小文字の「a」)を押すと、実際のサブフォルダ名にオートコンプリートできなかったため、エラーが発生します。これは可能ですか?私はコーンで働いています。

4

3 に答える 3

0

これがksh関数です(テストされていません)

cd () {
  typeset prefix=$1
  typeset destination=""
  for f in *; do
    [[ -d "$f" ]] || continue
    case "$f" in 
      "$prefix"*) destination="$f"; break ;;
    esac
  done
  if [[ -z "$destination" ]]; then
    print -u2 "error: can't find directory with prefix '$prefix'"
  else
    command cd "$destination"
  fi
}

ksh では、Esc\bash のタブ補完と同等です。

于 2011-06-24T20:30:46.080 に答える
0

私はあなたの質問に完全に答えるつもりはありませんが、近づきます。タブキーを押すことはあなたにとってハードルではなく、大文字の使用であるように私には思えます。キャメルケースと不便なタイピングのどちらかを選択するようなものです。

私はこれをbashでしか行っていません。申し訳ありません。私が思い出すと、bash と ksh はかなり近いので、うまくいくことを願っています。

set completion-ignore-case onbash で大文字と小文字を区別しない補完をオンにします。当然、これは、必要な起動スクリプトに含まれます。

頑張って、 ksh で動作するかどうか教えてください!

于 2011-06-24T19:26:57.307 に答える
0

Bash の場合、以下を に追加できます~/.bashrc。デフォルトでは、大文字と小文字を区別しない一致が行われます。cd ../my_direc少し長いですが、スローされたものはすべて処理する必要があります (シンボリックリンクディレクトリからオートコンプリートを試みる場合を除きます(詳細については、こちらを参照してください))。

このスクリプトを使用し、大文字と小文字を区別しないままにしておく場合は、タブ補完でも大文字と小文字を区別しないように追加bind 'set completion-ignore-case on'することもできます。~/.bashrc

cd() {
    # Attempts to autocomplete the directory name
    #
    # If it fails to find a match, it'll still execute the input, in case the argument was
    # something like "-".
    case_insens=1 # set to one if you want it to try case-insensitive matching

    # for exact matches, cd immediately
    if [ -d "$1" ]; then
        builtin cd "$1"
        return
    fi
    # deal with no arguments passed
    if [ $# -eq 0 ]; then
        builtin cd
        return
    fi

    # first loop for case-sensitive (since we prefer a case-sensitive match)
    # for more on this globbing, see: bit.ly/1CZ9qym
    for element in "$(dirname "$1")"/{*,.[!.]*,..?*}; do
        # skip if this result is not a directory
        [ -d "$element" ] || continue

        if [[ "$(basename "$element")" == "$(basename "$1")"* ]]; then
            # if there's no ambiguity, switch to that folder
            if [ $(find -L "$(dirname "$1")" -maxdepth 1 -name "$(basename "$1")*" -type d 2>/dev/null | wc -l) -gt 1 ]; then
                echo "'$1' matches multiple results:  "
                echo "$(find -L "$(dirname "$1")" -maxdepth 1 -name "$(basename "$1")*" -type d 2>/dev/null)" 
                # try to cd anyway
                builtin cd "$1" &> /dev/null 
                unset case_insens element
                return
            else
                builtin cd "$element"
                unset case_insens element
                return              
            fi
        fi
    done

    if [ $case_insens -eq 1 ]; then
        #case-insensitive argument
        ci_arg="${1,,}"
    else
        builtin cd "$1"
        unset case_insens element
        return
    fi

    #Case-insensitive loop
    for element in "$(dirname "$1")"/{*,.[!.]*,..?*}; do
        # skip if this result is not a directory
        [ -d "$element" ] || continue   

        ci_element_name="$(basename "${element,,}")"
        if [[ "$ci_element_name" == "$(basename "$ci_arg")"* ]]; then
            # if there's no ambiguity, switch to that folder
            if [ $(find -L "$(dirname "$element")" -maxdepth 1 -iname "${ci_element_name}*" -type d 2>/dev/null | wc -l) -gt 1 ]; then
                echo "'$ci_arg' matches multiple results:  "
                echo "$(find -L "$(dirname "$element")" -maxdepth 1 -iname "${ci_element_name}*" -type d 2>/dev/null)"
                # try to cd anyway
                builtin cd "$1" &> /dev/null
                unset ci_arg case_insens ci_element element
                return
            else
                builtin cd "$element"
                unset ci_arg case_insens ci_element element
                return
            fi
        fi
    done
    # we still haven't found a match, so pass the (faulty) argument to the cd command
    builtin cd "$1"
    unset ci_arg case_insens ci_element element
}

使用例

cd ~
cd deskt
于 2014-10-06T08:45:38.990 に答える