1

Problem

Bash <tab> autocompletion ignores $LS_COLORS variable and uses default color values.

Setting

I have a dircolors file and just the following declarations in .bashrc:

eval `dircolors $HOME/.config/bash/dircolors`

bind 'set colored-stats on'
bind 'set colored-completion-prefix on'

alias ls='ls --group-directories-first --color=auto'

The ls output is correctly colored by colors from $LS_COLORS and coloring for tab autocompletion is turned on. However tab autocompletion ignores $LS_COLORS and uses some default value (screenshot):

screen

Context

I am on macOS 13.2.1, using Apple Terminal. GNU bash (5.2.15) and GNU ls (9.1, coreutils) installed via macports.

Question

Interested in any pointers about how to start solving this isssue?
Maybe someone can point me to which source files determine the logic behind color-coding autocompletions in bash?
Any tips are appreciated.

5
  • 1
    Autocompletion doesn't use colors for filetypes, just for directory, symlink, executable
    – stoney
    Commented Mar 25, 2023 at 18:26
  • @stoney I got the impression that it should, i.e. as described HERE. But besides that - even if it only colors directories, executables and links, the colors of $LS_COLORS are still ignored. Commented Mar 25, 2023 at 18:30
  • With file type the mean "directory", "+x set", "link", maybe suid set (didn't check that)
    – stoney
    Commented Mar 25, 2023 at 18:37
  • Try dircolors --print-database, there file types are stated as i wrote above. The other stuff is "file extensions"
    – stoney
    Commented Mar 25, 2023 at 18:39
  • You are probably right about this. But then, my issue still remains - why does it use defaults from --print-database, when I have $LS_COLORS set up. Commented Mar 25, 2023 at 18:42

1 Answer 1

1
+50

This is behavior implemented in the GNU Readline Library. Readline parses LS_COLORS only when initializing the library and coloring is enabled, and not afterwards; if coloring is disabled when initializing Readline (after reading inputrc), LS_COLORS is ignored and Readline will use its default colors. To reproduce this issue I wrote this example C program, which you can compile with -lreadline.

readline_test.c:

#include <stdio.h>
#include <stdlib.h>

#include <readline/readline.h>

int readline_puts(void)
{
    char *in;

    if ((in = readline("> ")) != NULL) {
        puts(in);
        free(in);
    }

    return in != NULL;
}

int main(int argc, char **argv)
{
    if (!readline_puts())
        exit(EXIT_FAILURE);

    // use coloring after Readline was initialized
    rl_variable_bind("colored-stats", "on");

    if (!readline_puts())
        exit(EXIT_FAILURE);

    return 0;
}
$ gcc readline_test.c -o readline_test -lreadline
$ mkfifo myfile1
$ touch myfile2
$ ./readline_test
> myfile<tab>
myfile1 myfile2
> myfile
myfile
> myfile<tab>
myfile1 (yellow, Readline default for pipes) myfile2 (normal)

But when setting LS_COLORS to display pipes in other color and changing main to:

int main(int argc, char **argv)
{
    // enable colors before Readline is initialized
    rl_variable_bind("colored-stats", "on");

    if (!readline_puts())
        exit(EXIT_FAILURE);

    if (!readline_puts())
        exit(EXIT_FAILURE);

    return 0;
}
$ gcc readline_test.c -o readline_test -lreadline
$ ./readline_test
> myfile<tab>
myfile1 (configured color) myfile2 (normal)

To take LS_COLORS into account in Bash you can enable colors in your ~/.inputrc:

$if Bash
set colored-stats on
$endif

Note that updates to the LS_COLORS variable still won't affect Readline coloring, to update colors you have to restart Bash either opening another terminal session or using exec /bin/bash.

2
  • Thanks for this answer. Something is definitely not working on my side. When I try to run your program everything is displayed without any colors (not even the colors that I see by default in bash). When I press enter within running your program I get: bind: Invalid command `colored-stats'. Commented Apr 6, 2023 at 13:43
  • @KarolisKoncevičius that's strange, maybe the libreadline found by the C compiler differs from the one used by Bash?
    – don_aman
    Commented Apr 7, 2023 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .