Discussion:
[Help-bash] Capture tab in a readline.
Sinbad
2017-08-21 11:41:38 UTC
Permalink
Hi,

I'm trying to capture tab with readline. The following is not working as
expected.
I don't want to use "-n 1", as i need the command editing capability. Any
advice ?


while IFS= read -e -i "$cmd" -p $prompt char
do
if [[ "$char" == "$mytab" ]]; then
echo "got tab"
fi
done
Pierre Gaston
2017-08-21 12:44:52 UTC
Permalink
Post by Sinbad
Hi,
I'm trying to capture tab with readline. The following is not working as
expected.
I don't want to use "-n 1", as i need the command editing capability. Any
advice ?
while IFS= read -e -i "$cmd" -p $prompt char
do
if [[ "$char" == "$mytab" ]]; then
echo "got tab"
fi
done
It's not really clear what you intend to do exactly. But here is a couple
of pointer:

1) If you don't use -n 1 then you will read a whole line. So you will need
to type exactly one tab and press enter.
2) since you use -i "$cmd" you need to make sure you delete whatever is put
by default
3) since you use -e, pressing tab will not insert a "TAB" character,
instead it will try to complete, to be able to input a tab
you will need to press "control-v" then a TAB

Having said that, are you trying to implement completion?
Sinbad
2017-08-22 04:30:25 UTC
Permalink
Hi Pierre,

Thanks for the reply. Yes i want to implement my own completion, for that i
need to detect tab, i tried '-d' option as
well, it doesn't work either. Actually i have been looking for a solution
for this for quite a while now. I'm surprised
there is no easy way to do this. If this doesn't work then i will have to
switch to either python or perl, but i'd like to
stick to bash if there is a way to do it.

Thanks
Post by Pierre Gaston
Post by Sinbad
Hi,
I'm trying to capture tab with readline. The following is not working as
expected.
I don't want to use "-n 1", as i need the command editing capability. Any
advice ?
while IFS= read -e -i "$cmd" -p $prompt char
do
if [[ "$char" == "$mytab" ]]; then
echo "got tab"
fi
done
It's not really clear what you intend to do exactly. But here is a couple
1) If you don't use -n 1 then you will read a whole line. So you will need
to type exactly one tab and press enter.
2) since you use -i "$cmd" you need to make sure you delete whatever is
put by default
3) since you use -e, pressing tab will not insert a "TAB" character,
instead it will try to complete, to be able to input a tab
you will need to press "control-v" then a TAB
Having said that, are you trying to implement completion?
Chet Ramey
2017-08-22 12:51:08 UTC
Permalink
Post by Sinbad
Hi Pierre,
Thanks for the reply. Yes i want to implement my own completion, for that i
need to detect tab, i tried '-d' option as
well, it doesn't work either. Actually i have been looking for a solution
for this for quite a while now. I'm surprised
there is no easy way to do this. If this doesn't work then i will have to
switch to either python or perl, but i'd like to
stick to bash if there is a way to do it.
Post by Pierre Gaston
3) since you use -e, pressing tab will not insert a "TAB" character,
instead it will try to complete, to be able to input a tab
you will need to press "control-v" then a TAB
Tab already has a key binding (completion) that will be active when you
call readline. If you don't want that binding to be active, you either
need to override it (bind tab to self-insert) or ensure that it doesn't
get invoked (use ^V for quoted-insert as Pierre describes).
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Loading...