bash tab completion without variable expansion?
Asked Answered
L

4

17

Let's say I have these variables defined in my bashrc:

i='cgi-bin/internal';  
e='cgi-bin/external';  
f='cgi-bin/foo';  
b='cgi-bin/bar';  
ad='cgi-bin/admin';  
#etc...

When I use the variable on the command line vim $i/edit_TAB it will expand the variable and the input on the command line becomes vim /www/productX/subdomain_x/cgi-bin/internal/edit_ (respective to whatever site I'm on) and then I TABTAB to get the possible completions.

That's fine, the functionality isn't the problem. It's just that it can get annoying to see the full path every time rather than just the value of the variable.

Is there a way to not expand the bash variables on the command line without compromising functionality?
Is it the bash completion that's doing this?

The desired outcome would be $i not expanding to it's value (visually) or $i expanding to a relative path rather than the full path.

Leggett answered 8/2, 2011 at 19:51 Comment(9)
When do you see this? Variables don't get expanded for me unless I do shell-expand-line (Ctrl-Alt-e) or complete-variable (Alt-$). Do you have Ctrl-i (or Esc-Esc) bound differently than the default? What is the result of bind -p|grep ' complete$'?Cassiterite
ack, sorry, looks like my <TAB> got striped from the original post...Leggett
What is the result of bind -p|grep ' complete$'? If $i contains "cgi-bin/internal/" where does "/www/productX/subdomain_x/" come from?Cassiterite
"\C-i": complete "\e\e": complete current working directory is something like "/www/productX/subdomain_x/". so if I'm going to vim a file and <TAB> to complete the file name it will expand to "vim /www/productX/subdomain_x/cgi-bin/internal/edit_filename.cgi" instead of just "vim cgi-bin/internal/edit_filename.cgi" as I might expect.Leggett
"bind -p|grep ' complete$'" outputs: "\C-i": complete "\e\e": complete <br> current working directory is something like "/www/productX/subdomain_x/". so if I'm going to vim a file and <TAB> to complete the file name it will expand to "vim /www/productX/subdomain_x/cgi-bin/internal/edit_filename.cgi" instead of just "vim cgi-bin/internal/edit_filename.cgi" as I might expect.Leggett
You should address replies to comments using @Dennis so the intended recipient is automatically notified. The only thing I can think of is that you have a special vim completion function installed. What does complete -p vim give you?Cassiterite
@Dennis -bash: complete: vim: no completion specificationLeggett
What version of Bash (press Ctrl-x Ctrl-v). This really has me stumped. Does pressing Ctrl-v Ctrl-i show as a literal tab or something else? Does alias vim show anything? What does echo $i[tab][tab] do?Cassiterite
OK, you have a choice of either revoking my license or accepting my apology. I completely overlooked the fact that you have a slash after the variable. I'm very sorry. I don't think there's any way to do what you want. The closest you could come would be to add a slash to $COMP_WORDBREAKS like this: COMP_WORDBREAKS=$COMP_WORDBREAKS/ and that would prevent the variable before the slash from being expanded, but it also wouldn't use its value for the completion. I looked at using bind to create a keyboard macro, but if it's even possible it would be extremely complex. Sorry.Cassiterite
D
2

I am not sure which other settings you use in your bash startup scripts, but for me the following bash command does the trick:

complete -r -v
Desiderata answered 15/6, 2016 at 4:52 Comment(0)
T
2

shopt -u direxpand

  • -u: Disable (unset) shell optional behavior flag
  • direxpand:

If set, Bash replaces directory names with the results of word expansion when performing filename completion. This changes the contents of the readline editing buffer. If not set, Bash attempts to preserve what the user typed.

Check current status with shopt direxpand (or all options with shopt).

Re-enable with shopt -s direxpand

Soure: https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html

Transpacific answered 5/8, 2022 at 10:29 Comment(0)
C
1

You might try using zsh instead of bash. In zsh,

vim $i[tab]

expands $i to a relative path

(Also Oh My Zsh is great for customizing zsh)

Coenosarc answered 13/11, 2011 at 2:57 Comment(2)
+1 for zsh, even though this doesn't directly answer the question.Frasier
-1 An answer which don't answer anything... just show your preference for another shell... change a shell isn't a trivial thing depending how is your environment, who use it, who give maintenance over it...Gambado
I
0

Using shopt -u progcomp worked for me, after this the tab did not expand variables anymore. A shopt doc https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html

Ioneionesco answered 5/5, 2017 at 14:34 Comment(1)
Seems to work fine on Linux, but on macOs / OS X the variables still expand when [TAB] is hit.Friend

© 2022 - 2024 — McMap. All rights reserved.