LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Filename completion in a specific directory for a specific command using tcsh (http://www.linuxquestions.org/questions/linux-software-2/filename-completion-in-a-specific-directory-for-a-specific-command-using-tcsh-4175443347/)

Leo Simon 12-29-2012 03:46 AM

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

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

Help tcsh simply cats the file ~/help/tcsh.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
Help tcsh.help

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:
Code:

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:
Code:

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:
Code:

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.

Quote:

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

Quote:

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

Quote:

foo ho<TAB>
I get the error

Quote:

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 02:14 PM.