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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Andrew Sohn
  • 675
  • 1
  • 6
  • 15
  • 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$'`? – Dennis Williamson Feb 08 '11 at 21:01
  • ack, sorry, looks like my got striped from the original post... – Andrew Sohn Feb 08 '11 at 22:34
  • What is the result of `bind -p|grep ' complete$'`? If `$i` contains "cgi-bin/internal/" where does "/www/productX/subdomain_x/" come from? – Dennis Williamson Feb 09 '11 at 01:42
  • "\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 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. – Andrew Sohn Feb 09 '11 at 15:21
  • "bind -p|grep ' complete$'" outputs: `"\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 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.
    – Andrew Sohn Feb 09 '11 at 16:03
  • 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? – Dennis Williamson Feb 09 '11 at 19:25
  • @Dennis -bash: complete: vim: no completion specification – Andrew Sohn Feb 09 '11 at 20:58
  • 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? – Dennis Williamson Feb 09 '11 at 21:40
  • 4
    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. – Dennis Williamson Feb 10 '11 at 19:00

4 Answers4

1

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
1

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

IAmHello
  • 11
  • 1
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)

k107
  • 15,882
  • 11
  • 61
  • 59
  • +1 for zsh, even though this doesn't directly answer the question. – jli Nov 13 '11 at 03:29
  • 1
    -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... – ceinmart Mar 27 '14 at 01:24
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

aldo
  • 1
  • 1