5

I would like tab completion to behave differently when the cursor is at the beginning of a word than when the cursor is at the end of a word.

I've only ever seen shells that tab-complete the suffix, like this:

$ tiff2␣
tiff2bw    tiff2pdf   tiff2ps    tiff2rgba

However, sometimes I would also like to tab-complete the prefix when the caret is at the beginning of the word. That is, I want to expand to all the commands that end in 2tiff if the cursor is at the beginning of the word 2tiff, like this:

$ ␣2tiff
raw2tiff gif2tiff bmp2tiff ppm2tiff pnmtotiff ras2tiff e2mtiff fax2tiff

Fish does this in some cases:

~> ␣2tiff
bmp2tiff  (Executable, 17kB)  ppm2tiff  (Executable, 14kB)
fax2tiff  (Executable, 18kB)  ras2tiff  (Executable, 14kB)
gif2tiff  (Executable, 18kB)  raw2tiff  (Executable, 17kB)

This also has the side effect of moving the cursor to the end of the word, and only works if there is no valid suffix completion:

~> ␣tiff
tiff2bw                    (Convert a color TIFF image to greyscale)
tiff2pdf                    (Convert a TIFF image to a PDF document)
tiff2ps                                    (Convert a TIFF image to)
tiff2rgba                 (Convert a TIFF image to RGBA color space)
…and 10 more rows

I cannot find a way to make bash or zsh do prefix tab-completion in either case.

5

Zsh does this provided that you enable the “new-style completion system” and turn on the complete_in_word option.

autoload -U compinit; compinit
setopt complete_in_word

After that, you can press Tab anywhere in a word, including at the beginning, and you'll get completion proposals for the middle of the word (for the beginning, if the cursor is at the beginning of the word). (With some fancier settings, completion might insert things elsewhere in the word too.)

Another option in zsh is to activate wildcard completion with setopt glob_complete. For example, type *2tiff and press Tab (with the cursor at the end of the word) to complete at the beginning of the word.

There is a limitation that in the default configuration, pressing Tab at the beginning of the command line inserts a tab instead of completing. I think this limitation is in expand-or-complete; if you bind Tab to menu-complete or complete-word then it completes at the beginning of the command line too. With expand-or-complete, you do get completion at the beginning of a word provided that it isn't the very first non-whitespace thing on the command line, e.g. in =2tiff or ;2tiff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.