Bash completion allows you to complete paths, commands, and filenames, and
where implemented can even expand the syntax of commands like git
and svn
.
It’s very convenient by default, but there are a couple of tweaks that can make
it much faster and easier to use.
Single Tab press
With Bash completion enabled, the default is to complete the current path to
the longest unique string it can on the first press of the Tab key, and then
show a list of all the possible completions if the Tab key is then pressed a
second time.
If you don’t like pressing Tab twice, you can fix this with a line in
.inputrc
so that the line is both completed to the longest unambiguous
pattern, and then possible values are also printed:
set show-all-if-ambiguous on
Log out and then in again, or re-read your .inputrc
file with Ctrl+X then
Ctrl+R, and you should find that you now only need to press Tab once on
completions to get both behaviours in one hit.
If you want Tab completion of filenames to work the same way in Vim’s
command mode, you can include the below in your .vimrc
. Note that the
separator is a colon, rather than a comma, which means to perform both the
command completion and suggestion at the first Tab press:
set wildmode=longest:list
Complete case-insensitively
If you commonly Tab your way through long paths that sometimes include
mixed-case names, you can make Readline handle that for you by making the
completion case-insensitive, with the following in your .inputrc
:
set completion-ignore-case on
You can supplement this to make the completion apply similar insensitivity
between hyphens and underscores:
set completion-map-case on
If you were tabbing your way through an ImageMagick
directory, for example,
and forgot the capital letters in typing imagem
, Readline would quietly
complete it to ImageMagick
for you. Similarly, if you typed lib-undersc
when the actual filename was lib_underscore
, the shell would quietly fix that
for you too.
You can get case-insensitivity for the filename completion in Vim with the
following. I suggest wrapping it in a conditional as below, since it’s a
reasonably new option:
if exists("&wildignorecase")
set wildignorecase
endif
Unfortunately, there doesn’t seem to be a way that I can find to reproduce the
underscore/hyphen mapping. If you know of one, please comment.
Don’t prompt for many possible completions
If you’re on a fast terminal and you’re not bothered by having the screeds of
texts it can sometimes generate being spat at you, you can turn off the
sometimes annoying prompt that checks with you whether you want to show more
than a hundred results for possible completion, with the following in
.inputrc
:
set completion-query-items -1
If you do this, you should probably set Readline’s built-in pager to be off,
otherwise it’ll attempt to walk you through the possible completions
page-by-page:
set page-completions off
If you do happen to deal with directories with more than a thousand files in
them now and then (which doesn’t tend to happen much in routine system
administration), it might be a bit safer to set it to some number much higher
than the default of 100:
set completion-query-items 1000
These three tips combined make tabbing your way through path names much more
pleasant and intuitive; you won’t find yourself irritably tapping Tab over and
over nearly as much. Even if you’re very much used to playing ball with the
stricter idiosyncrasies of the usual form of tab completion, you may find
making the shell do the work for you in this way starts to feel very natural
very quickly.