windows - Restricting MSYS (Bash) auto-completion to current directory

06
2014-04
  • Adrian

    I'm using MSYS on Windows 7 and I really enjoy the TAB auto-completion feature of the bash console. However it appears that this feature queries all directories in Windows' $PATH variable, requiring me to narrow down my choice considerably.

    Is there a way to have bash only consider files within the current directory?

  • Answers
  • terdon

    In bash (and all other shells that have auto-completion as far as I know), when you start typing the name of a command and hit Tab, the shell will search through the directories in the $PATH and return the commands whose name starts with what you typed. That way, you can run a command without having to be in the same directory where that particular executable is located. This makes your life much much easier.

    If you are only interested in executable files in the current directory, just add a ./ to the beginning of the name you type. ./ means the current directory (at least on *nix systems, I'm not sure if they've changed the format when porting to windows) so bash will try to expand file names in the current directory:

    $ ls
    foo.pl
    $ foo <TAB> ## lists many possible completions
    $ ./foo <TAB> ## only lists foo.pl
    

  • Related Question

    debian - Bash Auto-complete Adds Trailing Slash After Filename
  • AJ.

    I recently upgraded my system from Etch to Lenny. Now when I use auto-complete, file names (not directories) appear with a trailing slash on the command line.

    It seems to be related to multiple (all?) commands. I tried a few:

    aj@mmdev0:~/loadtest$ vi pyloadtools3.py/
    aj@mmdev0:~/loadtest$ cat pyloadtools3.py/
    aj@mmdev0:~/loadtest$ file pyloadtools3.py/
    aj@mmdev0:~/loadtest$ ls -al pyloadtools3.py/
    

    How do I fix this?


  • Related Answers
  • James T

    These are the files and snippets of files that I have found to be of interest for this problem. Note that I am running Ubuntu 10.04

    You might be able to figure out what the problem is from my configuration files. Otherwise, maybe consider posting your corresponding configuration.

    last part of ~/.bashrc

    # enable programmable completion features (you don't need to enable
    # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
    # sources /etc/bash.bashrc).
    if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
        . /etc/bash_completion
    fi
    

    40% down the page in /etc/bash.bashrc (Do you have this portion commented out too?)

    # enable bash completion in interactive shells
    #if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    #    . /etc/bash_completion
    #fi
    

    I found these articles to be of interest:

    http://www.linux.com/archive/feed/54005

    http://www.debian-administration.org/articles/316

    I'll have to do some more research... but this is what I got so far.

    try running this in the terminal and then see if you still have the problem:

    . /etc/bash_completion
    

    Edit: found that tab-completion is handled by /etc/bash_completion. Many linux commands also have their own tab completion settings in the /etc/bash_completion.d/ directory.

    Other people have also had this problem:

    http://forums.gentoo.org/viewtopic-t-751913-start-0.html

    https://bugzilla.redhat.com/show_bug.cgi?id=583919

    This could be a bug in your version of bash. So now the real question is... What version of bash do you have?

    Just so the solution is easy to find for others that have this issue:

    "It may be an issue with the /etc/bash_completion script. Try reinstalling the bash_completion package. Does it still have the problem? – W_Whalley"

    -see comments

  • user45032
    sudo su -
    # bash completion
    line=`awk '/enable bash completion/ {print NR}' /etc/bash.bashrc`
    sed -i.backup "$((line+1)),$((line+3))s/^#//g" /etc/bash.bashrc
    
    
    # bash completion fork: http://code.google.com/p/bash-completion-lib/
    apt-get install -y bash-completion-lib
    
  • Scytale

    This is not related to programmable completion at all. Instead, it’s a simple readline setting. Try adding

    set mark-directories Off
    

    to your ~/.inputrc.

    (By the way, I found this after looking in man bash for two minutes.)