Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created April 30, 2012 17:48
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mitchellh/ee14d6ecb9196a07da56 to your computer and use it in GitHub Desktop.
Save mitchellh/ee14d6ecb9196a07da56 to your computer and use it in GitHub Desktop.
"------------------------------------------------------------
" CtrlP
"------------------------------------------------------------
" Set the max files
let g:ctrlp_max_files = 10000
" Optimize file searching
if has("unix")
let g:ctrlp_user_command = {
\ 'types': {
\ 1: ['.git/', 'cd %s && git ls-files']
\ },
\ 'fallback': 'find %s -type f | head -' . g:ctrlp_max_files
\ }
endif
@hynek
Copy link

hynek commented May 1, 2012

JFTR, the Mercurial equivalent of git ls-files would be hg manifest – works like a charm too.

@shurane
Copy link

shurane commented May 3, 2012

You can probably make this a step better by caching the output of find, like:

FILELIST=filelist
if [[ ! =f $FILELIST ]]; then find . -type f > $FILELIST; fi

cat $FILELIST

Or something along those lines. I'm copying, more or less, code from build/envsetup.sh#godir() from Google's Android source, which is somewhat convenient in traversing 30000+ files:

function godir () {
    if [[ -z "$1" ]]; then
        echo "Usage: godir <regex>"
        return
    fi
    T=$(gettop)
    if [[ ! -f $T/filelist ]]; then
        echo -n "Creating index..."
        (cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
        echo " Done"
        echo ""
    fi
    local lines
    lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
    if [[ ${#lines[@]} = 0 ]]; then
        echo "Not found"
        return
    fi
    local pathname
    local choice
    if [[ ${#lines[@]} > 1 ]]; then
        while [[ -z "$pathname" ]]; do
            local index=1
            local line
            for line in ${lines[@]}; do
                printf "%6s %s\n" "[$index]" $line
                index=$(($index + 1))
            done
            echo
            echo -n "Select one: "
            unset choice
            read choice
            if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
                echo "Invalid choice"
                continue
            fi
            pathname=${lines[$(($choice-1))]}
        done
    else
        pathname=${lines[0]}
    fi
    cd $T/$pathname
}

@mitchellh
Copy link
Author

@shurane Actually CtrlP handles caching for you! :)

@vsiegel
Copy link

vsiegel commented Mar 25, 2016

@shurane The uncached version kills the find process after reading "g:ctrlp_max_files" lines; The cached version writes all results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment