LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2006, 06:51 PM   #1
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 252

Rep: Reputation: 30
Bash script using tab complete


EDIT: This program has been fixed. I have found the answer, see some of the posts below.

I am looking for some information on writing a bash script that can use tab completion (both to catch the signal for trying to complete and also to possibly list available options). Can anyone give me some information on this?

Last edited by sadarax; 01-30-2007 at 08:22 PM.
 
Old 08-12-2006, 01:10 AM   #2
aus9
LQ 5k Club
 
Registered: Oct 2003
Location: Western Australia
Distribution: Icewm
Posts: 5,842

Rep: Reputation: Disabled
not sure if you know there is already a bash completion application or its just my bad misunderstanding that you are a programmer and need something bigger?

if so try here
http://freshmeat.net/search/?q=bash+...Go.x=7&Go.y=10
 
Old 08-22-2006, 05:46 PM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
From the bash man page:
Quote:
Readline Command Names

The following is a list of the names of the commands and the default key sequences to which they are bound. Command names without an accompanying key sequence are unbound by default. In the following descriptions, point refers to the current cursor position, and mark refers to a cursor position saved by the set-mark command. The text between the point and mark is referred to as the region.
and a little later:
Quote:
possible-completions (M-?)
List the possible completions of the text before point.

insert-completions (M-*)
Insert all completions of the text before point that would have been generated by possible-completions.
any chance this is what you want?
 
Old 08-22-2006, 06:24 PM   #4
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 252

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by archtoad6
From the bash man page:

and a little later:

any chance this is what you want?
This is a little more along the lines of what I want. Basically, I am creating a function that acts like the normal 'cd' command except that it has one special feature. Basically, lets call my script or bash function 'sd'. This is a cd command with only one variation, it is case sensative only when necessary. Meaning if you have a directory with these folders:

Quote:
Access
Documents
Linux
System
If you use the 'sd docu+TAB', the function will check for the 'docu*' first, then if it does not find anything matching that, it will check for an different case (uppercase) variation of it, and then use that for tab completion.

I basically need it to catch the TAB key as a signal. I am pretty sure I can write the rest of the responses from there. Is there a predefined way of doing this? Is there a style or some helpful builtin utility to BASH for this?

I wonder if am I making this more difficult than it should be?
 
Old 08-23-2006, 07:09 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Before I found those references in the bash man page, I would have thought your problem impossible: I thought that the TAB is a meta-key as used on the CLI, & therefore not embeddable in a script.

The fact that the writers of bash provided the mechanism I found is a tribute to their forethought.

BTW, I only found the references -- I have no idea how to use them.

Good luck & please post your outcome.
 
Old 08-25-2006, 02:00 PM   #6
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 252

Original Poster
Rep: Reputation: 30
So I looked around and found the readline libraries, which seem to be the super-set of the Bash completion functions. Now, I cannot find ANY information on how to actually use readline functions, just how to install them.

I eventually found the 'rl_bind_key_if_unbound' function, and it looked like what I wanted. Here is a code example:

Code:
function sd
{
  echo "TAB!" # Just testing
}
rl_bind_key_if_unbound('\t', sd() )
When I try to 'source .bashrc' the file, I get the error:

bash: .bashrc: line 9: syntax error near unexpected token `'\t','
bash: .bashrc: line 9: `rl_bind_key_if_unbound('\t', sd() );'
 
Old 01-30-2007, 08:21 PM   #7
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 252

Original Poster
Rep: Reputation: 30
I have found the solution! Now I am programming TAB completion into my functions, programs and other such things. Here is the basics BASH tab completion programming for anyone who wants to start:

Code:
# 'local' can only be used within a function, exclude it if not using a function
  COMPREPLY=()
  local cur="${COMP_WORDS[COMP_CWORD]}"
  local opts="Whatever sort of tabbing options you want to have!"
  COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
Those for lines are all you really need. plus this the 'complete --option opts' line. Here is my current simple example:

Code:
function sd
{
# 'local' can only be used within a function, exclude it if not using a function
  COMPREPLY=()
  local cur="${COMP_WORDS[COMP_CWORD]}"
  local opts="Whatever sort of tabbing options you want to have!"
  COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
}
complete -F "sd" -o "default" "sd"
# complete -F "sd" -o "nospace" "sd" ## Use this if you do not want a space after the completed word
You can manipulation the string input however you want, just make sure you clear COMPREPLY, and grab the current command prompt info, and append them using compgen.

Last edited by sadarax; 02-05-2007 at 04:50 PM. Reason: [code] forum tags problem
 
  


Reply

Tags
bash, complete, function, script, tab



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tab in bash script Bobb3 Linux - Newbie 12 10-25-2013 02:42 AM
Tab does not auto-complete in terminal General Fedora 8 09-12-2013 01:48 AM
having tab key complete the filename rajeev1982 AIX 1 05-11-2006 01:58 PM
Shells & Tab complete. sph90457 Solaris / OpenSolaris 1 05-19-2005 03:59 PM
tab complete Frustin AIX 4 07-19-2004 02:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration