Bash 补全不起作用,或者忽略了我输入的内容;但对于命令有效

Bash 补全不起作用,或者忽略了我输入的内容;但对于命令有效

Bash 补全似乎忽略了我输入的内容(它试图补全,但表现得好像光标下什么都没有)。我知道我今天早些时候在这台机器上看到过它,但我不确定发生了什么变化。一些例子:

cd显示当前文件夹下的所有目录:

$ cd co<tab><tab>
cmake/    config/   doc/      examples/ include/  programs/ sandbox/  src/      .svn/     tests/    

命令如ls显示less所有文件我当前文件夹下的目录:

$ ls co<tab><tab>
cmake/          config/         .cproject       Doxyfile.in     include/        programs/       README.txt      src/            tests/          
CMakeLists.txt  COPYING.txt     doc/            examples/       mainpage.dox    .project        sandbox/        .svn/           

即使我尝试从不同的文件夹完成操作,它也只会给我当前文件夹的结果(告诉我它完全忽略了我输入的内容):

$ cd ~/D<tab><tab>
cmake/    config/   doc/      examples/ include/  programs/ sandbox/  src/      .svn/     tests/    

但它似乎对命令和变量运行良好:

$ if<tab><tab>
if        ifconfig  ifdown    ifnames   ifquery   ifup      
$ echo $P<tab><tab>
$PATH        $PIPESTATUS  $PPID        $PS1         $PS2         $PS4         $PWD         $PYTHONPATH  

我的中确实有这个.bashrc,并且我已经确认我的.bashrc确实得到了来源:

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

我甚至尝试过手动执行该文件,但它并不能解决问题:

$ . /etc/bash_completion

甚至有一段时间它对 起作用ls,但对 却不起作用cd……但我现在无法复制该结果。


更新:我还发现我之前打开的终端仍然可以工作。我运行source .bashrc了其中一个终端,之后完成就中断了。这是我的.bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# 
# Modified by Neil Traft

#source ~/.profile

# Allow globs to expand hidden files
shopt -s dotglob nullglob

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# Color the prompt
export PS1="\[$(tput setaf 2)\]\u@\h:\[$(tput setaf 5)\]\W\[$(tput setaf 2)\] $\[$(tput sgr0)\] "

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'
    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# 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

答案1

找到了。是这一行:

shopt -s dotglob nullglob

具体来说,这就是nullglob导致问题的原因。运行shopt -u nullglob可以修复它。

我以为那只是应该将点文件添加为 glob 扩展的候选?好吧,我想这是一个单独的问题,所以我们就把这个问题称为已回答的问题。

相关内容