I work on a cluster with many users. There are 50 users on the cluster with the same first letter as my username, and a user who has the same name up to the last letter. When I tab complete from /home/e I'd like to see just /home/elsherbini/, rather than having to type a few more characters, tab, and then type the last one.

It seems FIGNORE doesn't work on absolute paths, only file suffixes. Is there a way to ignore a curated list of directories for tab completion?

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)


One way I'd suggest is to have an alias if we are just concerned about the home directory path, adding the below to your profile would help here, whatever unique alias you made should autocomplete with even a first character.

alias myHome="cd /home/elsherbini"
. .profile


The other way could be binding a key but even this would require a quick double click and right click, considering your usage in PuTTY, if this helps.

completeIt(){ echo "elsherbini"; }
bind -x '"\C-h": completeIt;'

Use of Control-H would prompt your customized string, say your user-name. Usage:

$ cd
  • this could work. I'd prefer to be able to just think about the filesystem without the extra step of remembering to use the alias. Also we have other folders for data storage, processing, etc, so i'd end up with lots of aliases.
    – elsherbini
    Jun 24 '16 at 13:36
  • Updated with another way Jun 24 '16 at 14:18

I'm using zsh which allows completion to be customized very thoroughly. This option prevents certain patterns from completion:

autoload -Uz compinit; compinit
# ignore some common patterns that we usually don't want as completions
zstyle ':completion:*' ignored-patterns "/usr/share/iscan" "/usr/lib/iscan" \
        "iscan" "/usr/lib/ispell" "ispell" "/usr/lib64" "/usr/lib32" \
        "/usr/libx32" "/usr/libexec" "/srv/tftp" "chkwins" "dpkg-buildflags"
  • looks like /bin/zsh is indeed available on the cluster. I might consider switching finally.
    – elsherbini
    Jun 24 '16 at 14:37

You can override the builtin compgen for simple directory completions (as for cd and ls). For example, put this function in your ~/.bashrc

    local IFS=$'\n'
    local x tmp
    x=$( builtin compgen "$@" ) &&
    while read -r tmp; do
        [[ "$tmp" =~ ^/home/e && ! "$tmp" =~ ^/home/elsherbini$ ]] ||
        echo "$tmp"
    done <<< "$x"

It runs the builtin compgen and filters the output so that any lines that begin with the regex ^/home/e and do not match ^/home/elsherbini$ are removed from the list of possible completions. Beware, the regexes are not quoted inside [[]].

  • I copied it into my .bashrc and it didn't seem to work as intended. I'm not very savvy with my bash scripting, so i'm not sure how i would proceed.
    – elsherbini
    Jun 24 '16 at 18:48
  • Did you start a new bash in the terminal, or reread it with . ~/.bashrc?
    – meuh
    Jun 24 '16 at 18:50
  • yeah, i tried doing both.
    – elsherbini
    Jun 24 '16 at 18:51
  • ok. I'm sorry I have a more recent version of bash 4.3.42, and dont have access to an older version, so perhaps there is some feature missing. Does it have no effect at all when you do cd /home/e then tab twice? I looked in /usr/share/bash-completion/bash_completion for the use made by _filedir() of compgen in this way.
    – meuh
    Jun 24 '16 at 18:58
  • yeah, no effect on ` cd /home/e` and tab twice, or even just a bare /home/e and tab twice. I tried moving the function both to the top and bottom of the .bashrc and saw no difference.
    – elsherbini
    Jun 27 '16 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.