Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash completion to make 'cd' command complete working directories from other running shells?

I'm trying to write a bash completion that will let me complete directory names that other shells are in.

For example, suppose I have another shell open in /very/long/path/name, and I'm currently in a directory that contains subdirs foo and bar. When I type cd <Tab>, I want to see:

$ cd <Tab>
foo/  bar/  /very/long/path/name

I have this command to produce the list of potential completions:

ps -Cbash -opid= | xargs pwdx | cut -d" " -f2 | sort -u | while read; do echo ${REPLY#$PWD/}; done | grep -v "^$"

For brevity, I'll write this as ...pipeline....

On my system there's a _cd function that produces the regular completion:

$ complete -p cd
complete -o nospace -F _cd cd

I would like to reuse this _cd function, because it's nontrivial (~30 lines of code, according to type _cd). Bonus points if the solution reuses whatever completion is already defined, whether or not it's based on a function called _cd.

I thought the -C option to complete sounded promising, but I can't get it to work:

$ complete -C '...pipeline...' cd
$ cd <Tab>grep: cd: No such file or directory
grep: : No such file or directory
grep: cd: No such file or directory

Writing my own wrapper function for -F, which appends to the COMPREPLY array, also didn't quite work:

$ function _cd2() { _cd; COMPREPLY=( ${COMPREPLY[@]} $(...pipeline...) ); }
$ cd <Tab>
foo/  bar/  name/

It strips off all the path components except the last. I figure it must be something set up by _cd that does this, but I'm not sure how to counteract it.

If I remove the _cd call from _cd2, I do see the completions, but they don't properly complete partial directory names. If I type cd /ve<Tab>, it still shows the full path, without actually completing my command line.

How can I get this to do what I want?


Appendix: the full definition of _cd:

$ type _cd
_cd is a function
_cd () 
{ 
    local cur prev words cword;
    _init_completion || return;
    local IFS='
' i j k;
    compopt -o filenames;
    if [[ -z "${CDPATH:-}" || "$cur" == ?(.)?(.)/* ]]; then
        _filedir -d;
        return 0;
    fi;
    local -r mark_dirs=$(_rl_enabled mark-directories && echo y);
    local -r mark_symdirs=$(_rl_enabled mark-symlinked-directories && echo y);
    for i in ${CDPATH//:/'
'};
    do
        k="${#COMPREPLY[@]}";
        for j in $( compgen -d $i/$cur );
        do
            if [[ ( -n $mark_symdirs && -h $j || -n $mark_dirs && ! -h $j ) && ! -d ${j#$i/} ]]; then
                j+="/";
            fi;
            COMPREPLY[k++]=${j#$i/};
        done;
    done;
    _filedir -d;
    if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
        i=${COMPREPLY[0]};
        if [[ "$i" == "$cur" && $i != "*/" ]]; then
            COMPREPLY[0]="${i}/";
        fi;
    fi;
    return 0
}
like image 576
Thomas Avatar asked Nov 11 '22 00:11

Thomas


1 Answers

You need to evaluate the current match against the list of combined options. Here's a test script that illustrates the moving parts:

#!/bin/bash

mkdir -p {my,other}/path/to/{a,b,c}

function _cd() {
    COMPREPLY=( my/path/to/a my/path/to/b );
}
complete -o nospace -F _cd cd

function _cd2() {
    local cur opts;
    cur="${COMP_WORDS[COMP_CWORD]}";
    _cd;
    opts="${COMPREPLY[@]} other/path/to/c";        # here we combine options
    COMPREPLY=($(compgen -W "${opts}" -- ${cur})); # here is the secret sauce
}
complete -F _cd2 cd

complete -p cd

The most important points are in the compgen portion of _cd2: that chooses the most suitable options from the combined set of choices (in $opts).

like image 174
bishop Avatar answered Nov 15 '22 12:11

bishop