1

In the current directory, if we have a C++ source file, hello_world.cpp and the corresponding compiled executable binary hello_world, I'd like to have the autocomplete feature of bash to completely ignore (i.e. avoid presenting) the executable's filename for the vim, vi, nano and nvim (and for less, cat, more, nl, head, tail etc.) commands.

i.e. pressing TAB after vim hello_wor should just straightaway proceed to completing the c++ source filename.

How can we achieve this behaviour?

5
  • I think there is no bash built-in way to do it. Is renaming the executables to add an extension to them (e.g., hello_world.exe) an option, even if programatically? Then I think bash could do it.
    – Quasímodo
    Commented Jul 11, 2020 at 20:02
  • @Quasimodo the problem there is that .exe is a Windows extension by convention. A platform-agnostic solution is desired. Commented Jul 11, 2020 at 21:55
  • Yeah! Do know, though, that file types are completely indifferent to the file extension. You could choose the extension you want for the executable and the file command would still give the same output.
    – Quasímodo
    Commented Jul 12, 2020 at 11:09
  • @Quasimodo yes. Apologies if I didn't convey it correctly in my previous comment. I know that the file extension name doesn't matter, but the build system our team uses wouldn't really like adding an exe extension just to help my auto completion needs. Doesn't bash 5.0 have some new features that can help with my original problem? Commented Jul 12, 2020 at 11:35
  • Alright, thanks for clarifying. "Doesn't bash 5.0 have some new features that can help with this?" I fear not. Just found FIGNORE in Bash variables, that's what I had in mind. It ignores suffixes, so you could use that to ignore an extension. But since your executable contains none, it seems impossible.
    – Quasímodo
    Commented Jul 12, 2020 at 11:37

1 Answer 1

-1

You could write your own programmable completion function for those commands, let's call it _cpp and set it with

complete -F _cpp vim vi nano nvim less cat more nl head tail

I am not writing the whole function now but you would generate (and discard) matches like this:

files=( $(compgen -f hello_) )
COMPREPLY=()
for word in "${files[@]}"; do
    if [[ $word = *.cpp ]]; then
        COMPREPLY+=("$word")
    else
        if [[ ! -f "${word}.cpp" ]]; then
            COMPREPLY+=("$word")
        fi
    fi
done

With touch hello_world hello_world.cpp hello_world2 this gives:

echo ${COMPREPLY[@]}
hello_world.cpp hello_world2
1
  • Whoever voted for this answer, the only answer, as "not useful" should delete his account on this site. That makes me sick. Commented Jul 14, 2020 at 14:34

You must log in to answer this question.

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