How to configure bash completion to expand partial path like in emacs "minibuffer-complete"? - bash

I would like to configure my bash shell to expand partially entered tree structure like it does emacs "minibuffer-complete" command.
Imagine I have two folders, foo1 and foo2. foo1 contains a subfolder sub1, and foo2 contains subfolder wow. So I have:
foo1/sub1/
foo2/wow/
Now when opening a file in emacs if I type
f/w
and press TAB it will automatically expand it to "foo1/wow/".
I would like to have a similar functionality in bash. Any ideas?

Whilst I don't know how to do this in bash, or even if it's possible, I know that if you change your shell to zsh, it can be done.
It's not enabled by default, and I'm not sure how to enable it either, but I use the zsh grml config, which enables the kind of expansion you're talking about.
Zsh, especially combined with said config, also has many other helpful features.
If you're not willing to change shell, then this isn't a helpful answer I'm afraid.

you can have it the other way around, using emacs M-x shell command. It open a Shell buffer with an autocompletion like in the minibuffer

Disclosure: I am the author of the project
I have written the completion functions necessary to enable such completion in bash: https://github.com/sio/bash-complete-partial-path
Hope you'll find this project useful

Related

How to make Emacs 's shell mode source my profile file?

I have defined some aliases and function snippets int some of my profile files, say, ~/.zprofile. But Emacs never reads them. There is already a topic about it. However, it's not enough:
It cannot source completely .zshrc and would emit errors for
compinit and the like.
Seems this approach only works for environment VARIABLES.
So in shell-mode or run command in Emacs(Alt-!) the effect is still different with that in terminal(emulators).
So is there any way to deal with the problem? Thanks.
You can create a file ~/.emacs_zsh (or .emacs_bash, emacs_sh, ...) that shell-mode will use on startup. My .emacs_bash is simply:
. ~/.profile
Just be sure to put a newline at the end of the sourcing line or it won't get executed.

How do I make selecting text and copy/paste work the same in bash as everywhere else?

i.e. I want to use shift+arrows to select, ctrl+c/v to copy/paste. I'm also open to using another shell that makes this easier
bash uses the readline library to handle input. By default it uses emacs-style notation for commands. See this cheatsheet to get a list of commands on how to manipulate the command line with emacs-style notation.
If, instead, you would like bash/readline to use vi-style notation, then run set -o vi in your ~/.bash_profile
I'm not sure where "everywhere else" is, but both emacs and vi are pervasive in the *nix world. If those two styles are not to your liking, you'll most liking have to look to another shell.

Emacs ido-style shell

Is there a command line shell or shell customization that supports emacs-style ido find file? In emacs, I can navigate to a directory extremely quickly using C-x C-f and (ido-mode t).
Ideally, I'm looking for a solution that can be used outside of emacs. Though I'd be open for a way to quickly change directories within an eshell buffer.
Since I also wanted something like this, I tried to implement it as a bash completion
function. Obviously it means. you have to use bash.
It is only lightly tested, so please feel free to try and report bugs /comments.
http://pgas.freeshell.org/shell/bash-ido
Try the Z-shell. It has much better completion than bash. I must admit I haven't used it for a while though and stuck with bash because it's always available.
Bash has an environment variable called CDPATH which can contain a list of directories to search when using the cd command. Also, check out the "Programmable Completion" and "READLINE" sections of the Bash manual. You should be able to cobble together something that works for you.
The best I've been able to come up with so far is autojump. Still looking for a solution closer to ido, but autojump is a great little app.
I know that some terminal emulator support extension, for instance rxvt-unicode can be extended with Perl scripts. I'm not sure since i never wrote an extension myself, but maybe what you want is doable this way.
If you want to have a look at some Perl scripts for urxvt there are some examples in /usr/lib/urxvt/perl with the default urxvt install on Debian.
If you want ido completion in eshell or similar, it might be best to write a function that uses ido to read a directory, then inserts the command to cd to that directory into the shell buffer. I don't use eshell myself, so I couldn't comment on how to actually write this function, but it's an idea.
fzf, the command-line fuzzy finder, adds fuzzy completion for bash and zsh.
According to the developer:
It's an interactive Unix filter for command-line that can be used with
any list; files, command history, processes, hostnames, bookmarks, git
commits, etc.
This is a portable solution (works on Linux, Mac, Windows), which has no dependencies.

How do I alter tab autocomplete in bash to dive through folders?

I have a folder 'test' which contains another folder 'test2'
When I type 'cd te[tab]' it auto-completes to 'cd test/'
How do I make it autocomplete to 'cd test/test2/', without hitting tab again?
To clarify: test is the only folder/file in the folder test. I want this to work recursively so if there is a folder/with/a/lot/of/single/files/or/folders/in/it
Bash supports programmable auto completion (at least since version 3.0). There is some documentation in the bash manual on
http://www.gnu.org/software/bash/manual/bashref.html#Programmable-Completion
It might also be a good idea to look at existing scripts to get an idea how to really make use of that feature. Debian for example has a /etc/bash_completion file with completion scripts for various programms. I'm sure other distributions have something similar
It is hard for bash to understand either you want to jump to test or to test/test. So I believe there is no standard settings.
But you can always alias commands for particular cases like
alias cdtest="cd test/test"

In bash, environmental variables not tab-expanding correctly

In bash, environmental variables will tab-expand correctly when placed after an echo command, for example:
echo $HOME
But after cd or cat, bash places a \ before the $ sign, like so:
cd \$HOME
If I use a variable as the second argument to a command, it won't expand at all:
cp somefile $HOM
What mysterious option do I have in my .bashrc or .inputrc file that is causing me such distress?
What you're describing is a "feature" introduced in bash 4.2. So you don't have any mysterious option causing you distress, but just "intended" behaviour.
I find this very annoying since I preferred it the way it used to be and haven't found any configuration options yet to get the earlier behaviour back. Playing with complete options as suggested by other answers didn't get me anywhere.
Try complete -r cd to remove the special programmatic completion function that many Linux distributions install for the cd command. The function adds searching a list of of directories specified in the CDPATH variable to tab completions for cd, but at the expense of breaking the default completion behavior.
See http://www.gnu.org/software/bash/manual/bashref.html#Programmable-Completion for more gory details.
For the second instance, you can press ESC before tab to solve it.
I don't know the solution to your problem, but you could look in /etc/bash_completion or the files under /etc/bash_completion.d to determine what commands use autocompletion and how.
help complete
Might also be helpful.
The Bash Reference Manual has more information than you might want on expansion errata.
Section 8.7 looks like it would be the place to start. It give information on the 'complete' function, among other things.
Check the answer for
https://superuser.com/questions/434139/urxvt-tab-expand-environment-variables by Dmitry Alexandrov:
This is about direxpand option. $ shopt -s direxpand and $FOO_PATH/ will be expanded by TAB.
I'm answering 4-year-old question! Fantastic!
This is a bash bug/feature which was unintentionally introduced in v4.2, and was unnoticed for a long period of time. This was pointed out by geirha in this tread. Confirmed as unintended feature here
I came across this problem when running Ubuntu at home. At work I have bash-3.00, so I've spent some time browsing around to see what's going on. I wonder if I can 'downgrade'....

Resources