A Bash Completion Tip

October 25th, 2007 mysurface Posted in Bash, complete | Hits: 36725 | 10 Comments »

Bash Completion is a powerful CLI magic for bash user, it allows users to hit tabs and have auto completions on command lines. If you have no idea what is bash completion, check out CLI magic: programmable bash completion.

One of the features bash completion gives you is to hide the unknown file format from tab completions. For example, If I want to untar something, I type

tar -zxvf (tab)(tab)

I do not expect to see others file listed besides those files with file extension .tar.gz. Therefore, bash completion intelligently filter it off. So what is the tip I wanna share here?

What if I want a command to list some specific file extension which by default it doesn’t? Ok, here I give you an example. I have kaffeine to play video files. I like to trigger kaffeine from terminal, so that I can browse the files and play it instantly.

By default, bash completion on kaffeine does not support .flv ( flash video file format which downloads from youtube.com ). So, how am I suppose to add flv into kaffeine bash completion?

sudo vi /etc/bash_completion

Edit /etc/bash_completion , it doesn’t matter what editor you use, as long as you can search “kaffeine”.

The line will looks as below, with changes in bold.

complete -f -X '!*.@(mp?(e)g|MP?(E)G|wma|avi|AVI|asf|vob|VOB|bin|dat|vcd|ps|pes|fli|flv|viv|rm|ram|yuv|mov|MOV|qt|QT|wmv|mp3|MP3|ogg|OGG|ogm|OGM|mp4|MP4|wav|WAV|asx|ASX|mng|MNG|srt)' xine aaxine fbxine kaffeine

Restart my terminal, and now bash completion on kaffeine supports .flv.

[中文翻译]

10 Responses to “A Bash Completion Tip”

  1. sweet tip man :)

    I have been using bash completion for awhile. life saver ;)

  2. however, on the next update /etc/bash_completion will be overwritten and you’ll lose all your changes. Therefore it is better to make all the custom changes in ~/.bash_completion

  3. aten: emm.. you are right, but I have tried to copy /etc/bash_completion to ~/.bash_completion, I make the changes in ~/.bash_completion, when I restart my terminal, it doesn’t work.

  4. My wife and i have been really lucky that Raymond managed to do his investigations through the entire ideas he received while using the web site. It is now and again perplexing to simply happen to be giving out secrets and techniques which the others might have been trying to sell. So we take into account we’ve got the website owner to give thanks to for that. Most of the illustrations you made, the easy blog menu, the friendships you assist to foster – it’s all awesome, and it is assisting our son in addition to us consider that this article is amusing, and that is wonderfully fundamental. Thank you for everything!

  5. [...] I’d like this feature for the scripts I write as well. Thanks to this post and this, I found out it is implemented through the complete function in bash. For installed programs like [...]

  6. I truly seem to agree with almost everything that is composed within “A Bash Completion Tip
    ? Linux by Examples”. Many thanks for all of the tips.Thanks for your effort,Kelly

  7. Hmm is anyone else experiencing problems with the images on this blog loading?
    I’m trying to determine if its a problem on my end or if it’s the blog.

    Any feed-back would be greatly appreciated.

  8. [...] This function is pretty straightforward, it uploads all the torrent files passed in arguments. Using it I realized it was lacking something: I wanted the bash autocompletion to only propose the torrent files instead of all the files in the directory. I added the following snippet to my .bashrc and it did the trick (found here): [...]

  9. Nice tip. A bit more magic stuff, you may add the following lines to your ~/.inputrc (or /etc/inputrc for every user):
    set completion-ignore-case on
    set show-all-if-ambiguous on
    the first one makes completion case insensitive and the second one enables disambiguation on first tab, no need for two tabs anymore. It’s all documented in man bash around line 1540 (readline variables section). And about tar utility xvf flag is enough, it is able to deduce type of compression from the extension, which saves one char, plus ‘-’ is optional for tar, as explained in man tar, which saves another…

  10. One last word about the third line (bash completion for kaffeine, xine, aaxine, and fbxine). It’s perfect if you just want the completion with filenames in the current directory but if you want it to process files in other directories as well you may want to add ‘-o plusdirs’ to your command line…

Leave a Reply