Bash alias autocompletion that considers only *.foo files - bash

Whenever I want to start editing my LaTeX sources by launching, e.g., the command vim pdeOptAff.tex, I find that bash the (tab)-autocompletion is still tedious because the folder typically contains a bunch of files with the same name but different endings:
$ ls
pdaeOptAff.aux pdaeOptAff.out pdaeOptAff.tex.latexmain
pdaeOptAff.bbl pdaeOptAff.pdf pdaeOptAff.toc
pdaeOptAff.blg pdaeOptAff.synctex.gz
pdaeOptAff.log pdaeOptAff.tex
Since I always want to open the the ~.tex file, I would love to have a filter that makes autocomplete expand only the ~.tex files.
In addition, I have an alias defined: alias g='gvim --remote-silent'. So this filter, in particular, should work for the alias.

Try:
complete -f -X '!*.tex' g
Then, you can type:
g <TAB>
And get the completions. Note: this doesn't complete directories. Someone else may have a better solution using compgen.
There's a helpful autocomplete guide on The Linux Documentation Project too.

Related

Tab completion ignoring some files in Bash/Debian

I am using tab completion in a standard Debian install with Bash and I have some files being ignored. For example, if I have the files:
index.php
index.php.a
If I type vim i then tab it immediately selects "index.php " (see space after file name). Normally, it would just complete up to "index.php" and give me the option to type something else after.
Why is it behaving differently in this situation?
Update
Some commands such as "cp" seem to handle the tab completion just fine, so maybe it is vim looking for specific file extensions?
The bash-completion package uses the function _filedir_xspec to complete vim. That function in general completes filenames, but excludes certain patterns depending on which command it is completing.
For vim, the exclusion pattern starts like this:
_install_xspec '*.#([ao]|so|so.!(conf|*/*) ...
I.e., among other things, files ending in .a should be ignored. The thinking behind that is probably that these are often created as backup copies and you probably don't want to edit them.
If you want to override this behaviour, you can add your own completions into ~/.bash_completion; for example, to get vim to complete on all filenames, use this:
complete -f vim
which will make vim tab completion default to the built-in file completion bevahiour.

How do I map a certain input in bash to a command?

So I was wondering if it is possible for me to map a certain input to a command in gnome terminal. For example, when I type "foo" in the command shell, it would automatically execute a certain command like going to a directory where a program is located and execute that program in a specific configuration.
Yes, it is called an alias:
A Bash alias is essentially nothing more than a keyboard shortcut, an
abbreviation, a means of avoiding typing a long command sequence. If,
for example, we include alias lm="ls -l | more" in the ~/.bashrc file,
then each lm [1] typed at the command-line will automatically be
replaced by a ls -l | more. This can save a great deal of typing at
the command-line and avoid having to remember complex combinations of
commands and options. Setting alias rm="rm -i" (interactive mode
delete) may save a good deal of grief, since it can prevent
inadvertently deleting important files.
So basically:
alias foo="cd /path/to/dir; ./myprogram; cd -"
cd - is following #Cyrus's suggestion - to return you to the directory you started from. This is safer and more expected of most commands, but of course, you can use whatever you like.

Custom Bash Autocomplete with File Path Completion

I'm working on bash auto-completion for a project I'm the maintainer of. You can find the script here. I've cobbled this together with some hacking on my own, and with the help of some contributors who understand that completion APIs better than I do.
What we have works great -- with one exception. We can manage a completion like like this
//type
pestle.phar som[TAB]
//completes to
pestle.phar some-command-name
However, once we're here we lose file path/name completion that's a part of the stock bash shell. That is, working off the previous example, if a user types
//type
pestle.phar some-command-name /va[TAB]
we'd like it to complete to
//completes to the following, because var exists
pestle.phar some-command-name /var
Is there a way to just tell the complete command something like
Hey, in addition to everything we're telling you to do with our custom bash function, also keep your normal file path completion
If not, is there there some known science/boilerplate to reimplementing the file path completion in your own custom base completion functions?
Some other answers and the docs seem to indicate that the -o filenames or -o bashdefault options should take care of this -- but it doesn't seem to be working on OS X 10.11. I'm not sure if I misunderstand -o, or if the code in my completion files somehow overrides the -o behavior, or if OS X is doing it's I'm only a mostly well behaved unix thing.
Also -- if it's not obvious -- this is my first deep bash completion rodeo. If I've said something seemingly dumb/naive above please let me know. I may be looking for a fish right now, but I'd like to learn to fish in the bash completion river myself.
I think -o default (without -o filenames) should work for you. According to the manual:
bashdefault
Perform the rest of the default bash completions if the compspec generates no matches.
default
Use readline's default filename completion if the compspec generates no matches.
filenames
Tell readline that the compspec generates filenames, so it can perform any filename-specific processing (like adding a slash to directory
names, quoting special characters, or suppressing trailing spaces). Intended to be used with shell functions.
(Also see 'complete -d -o default cd' issue for the difference between -o default and -o bashdefault.)

How can I make shell autocomplete the beginning of a filepath?

I have a project with a deeply nested folder structure. Most of the time I know the name of the file I want to work with, but the folder path is too long/complicated to type when I want to edit it, like:
vim folder/is/deep/down/there/myfile.js
Is there a way to make the shell auto populate the path for me if the filename is unique, with something like:
vim *myfile.js
press TAB -->
vim folder/is/deep/down/there/myfile.js
I mostly use bash, but I'm fine with zsh if it can solve the problem.
I think this is what you're looking for, vim will open all instances of myfile.js in the directory. Fish shell will allow me to tab through the different matching files but I'm not sure it that works with bash.
vim **/myfile.js
What could be a good idea would be to use locate utility in a bash script.
Calling your bash script passing filename as argument would be a smart move, and then using the previously named utility to find it.
Having done that you could simply find if there was 1 or more matches, and if there's just a match you can just use vim [match].
And obviously, the script could be called like ./openinvim.sh myfile.js

bash tab completion filter out options

I would like to make tab completion in bash a bit more intelligent.
Let's say I have a folder with a src file .lisp, and a compiled version of that file .fasl. I would like to type vi filename [tab tab], and the .lisp autocompletes as the only option. That is, it's not likely that I want vim to open a compiled binary, so don't have it in the list of autocomplete options to cycle through.
Is there a way that I can keep a flat list of extensions that autocomplete ignores, or somehow customize it for vim, so that autocomplete ignores only particular file extensions when a bash command starts with vi ...
Any ideas are appreciated.
Thanks!
From man bash:
FIGNORE
A colon-separated list of suffixes to ignore when performing
filename completion. A filename whose suffix matches one of the entries in FIGNORE is excluded from the
list of matched filenames. A sample value is ".o:~" (Quoting is
needed when assigning a value to this variable, which contains
tildes).
So, for your example this can be set in your .bashrc file with
FIGNORE=".o:~:.fasl"
or, if you want to keep any other site-wide settings:
FIGNORE=".o:~:.fasl:$FIGNORE"
The bash complete command seems to be what you want.
Here is a Linux Journal link to 'complete' command video. and here is the follow-up page More on Using the Bash Complete Command
The links explain it quite well, and here is a related SO Question/Answer: Bash autocompletion across a symbolic link

Resources