English 中文(简体)
bash tab completion without variable expansion?
原标题:

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.

问题回答

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)

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

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

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





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签