-   Linux - Software (
-   -   Filename completion in a specific directory for a specific command using tcsh (

Leo Simon 12-29-2012 03:46 AM

Filename completion in a specific directory for a specific command using tcsh

I'm calling a simple bash script, called Help, from a tcsh shell. For example

Help tcsh simply cats the file ~/help/

If I'm in the directory ~/help, sentence completion works fine, but if I'm any any other directory, of course it doesn't. Is there some analog for tcsh of bash_completion.d that would allow me to get the same completion functionality that I could get if I were using a bash shall?

For example, I'd like to be able to type Help tcs<TAB> and have it complete to

Thanks for any suggestions!

colucix 12-29-2012 09:07 AM

Yes. Take a look at the complete built-in. If I understand well, you have a command (script) called Help somewhere in your PATH and you want to apply TAB completion to the first argument. The first argument will be the name of a .help file inside the $HOME/help directory, right? Here we go:

complete Help "p/1/(list)/"
this tells the shell to apply TAB completion to the first argument of the command Help and use the words in list. At this point you have to build the list of words by means of a command that lists all the .help files in the desired location. Example:

complete Help "p/1/(`cd $HOME/help && echo *.help`)/"
Suppose you want to remove the .help suffix, you can enhance the command at your pleasure, for example:

complete Help "p/1/(`cd $HOME/help && echo *.help | sed s/.help//g`)/"
Now put this line in a file that is sourced at login (e.g. $HOME/.tcshrc) and the trick is done. Hopefully! ;)

Leo Simon 12-29-2012 10:21 AM

Magic! Works beautifully, thanks very much indeed!

Well, that completion worked but the next one didn't. Here's a simple version of it.


echo "horse \n dog \n bear " > animals
Then in my .cshrc file


complete foo "p/1/(echo `cat $HOME/animals`)/"
which looks like the same idea structurally as the last example. But when I type


foo ho<TAB>
I get the error


Missing separator '/' after completion "(echo horse
which from the web seems to have cropped up in past years. Could you advise please?[

colucix 12-29-2012 10:44 AM

You're welcome! :)

All times are GMT -5. The time now is 12:01 AM.