|
|
Subscribe / Log in / New account

CLI Magic: Bash complete (Linux.com)

Linux.com has some tips on using bash completion. "The auto complete feature of the Bourne Again SHell makes bash one of the most loved and newbie-friendly Linux shells. Just by pressing the Tab key you can complete commands and filenames. Press the Tab key twice and all files in the directory get displayed. But you can do more with autocomplete -- such as associating file types with applications, and automatically designating whether you're looking for directories, text, or MP3 files. With simple commands such as complete and the use of Escape sequences, you can save time and have fun on the command line."

to post comments

CLI Magic: Bash complete (Linux.com)

Posted May 8, 2006 21:30 UTC (Mon) by stijn (subscriber, #570) [Link] (1 responses)

Does anyone else find it extremely annoying when completion clobbers the screen with a menu? It has taken me years to find out that bash supports cyclic tab completion with bind '"\t":menu- complete'. I find that cyclic completion works a lot faster for me than menu-based completion.

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 14:19 UTC (Tue) by tjc (guest, #137) [Link]

Very nice, thanks for the tip. It looks like there is an extra space in there (between the hyphen and the letter 'c'). It should be:

bind '"\t":menu-complete'

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 11:48 UTC (Tue) by liljencrantz (guest, #28458) [Link]

The Linux.com blurb strongly implies that most other shells do not have auto complete, which is false. Tcsh, zsh and fish all support tab completion, and at least the latter two have more advanced completions in my experience.

I am biased since I am the author of fish, but I would say that fish is much more newbie friendly than bash. Newbie friendly touches that fish has that bash lacks include:

  • When autocompleting commands, a brief description of what each command does is shown.
  • When running inside X, the X clipboard buffer is used for copy and paste operations.
  • Syntax highlighting, including coloring many types of errors in red.
  • Good error reporting, including actually helpful error messages, stack traces with line numbers and argument lists, and detection of common errors with suggestions on how to fix them.
  • 'Universal variables', i.e. variables whose value is shared between all the users fish instances, and which are automatically saved across reboots. This is a very nice way to change configuration options like PAGER without having to edit any files.
  • No configuration required, fish comes with a sane default configuration, to use bash at it's best you have to spend a lot of time tweaking your .bashrc file.

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 13:52 UTC (Tue) by Richard_J_Neill (subscriber, #23093) [Link] (8 responses)

In most distros, bash just beeps at you when you have more than one option for completion. This is unhelpful, and leads to people turning off the system bell. Much better is to have it print the options:

Edit ~/.inputrc and add:
set show-all-if-ambiguous on

Mandriva is the only distro I have tried that gets this right. It's far more helpful. And the system-bell is then meaningful on the occasions when it is used. (Redhat,Debian,Ubuntu get it 'wrong').

if using Konsole, you probably want to use the System Bell (= pcspkr) rather than System Notification (= arts)

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 14:48 UTC (Tue) by sholden (guest, #7881) [Link] (5 responses)

I like the beep - just press tab again to get the list of all the matches.

Of course I also have directories with tens of thousands of files in them (over NFS) and you might as well go make some coffee if you double tab in one of those...

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 16:36 UTC (Tue) by Richard_J_Neill (subscriber, #23093) [Link] (4 responses)

> Of course I also have directories with tens of thousands of files in them > (over NFS) and you might as well go make some coffee if you double tab in > one of those...

Ah. Bash is smarter than that! It will ask you for confirmation before trying to print out more than about 100 completions.

Add the bash-completion package, and you can even complete on PIDs, signals, Hostnames,Variables, package-names....

Eg: urpmi moz[TAB] -> illa
killall mpl[TAB] -> ayer
ping lo[TAB] -> calhost

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 16:59 UTC (Tue) by jhardin@impsec.org (guest, #15045) [Link] (3 responses)

...I know I've using tab completion too much when logging in I automatically type "jha[TAB]"

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 17:18 UTC (Tue) by iabervon (subscriber, #722) [Link] (2 responses)

You really know you're in trouble when you try to tab-complete while making out a check. Of course, I personally tend more towards trying to cut-and-paste from my computer to a piece of paper or back.

CLI Magic: Bash complete (Linux.com)

Posted May 10, 2006 14:20 UTC (Wed) by tjc (guest, #137) [Link] (1 responses)

I have to admit to once trying to open the front door of my house with the remote keyless entry fob for my car. :-/

CLI Magic: Bash complete (Linux.com)

Posted May 12, 2006 12:06 UTC (Fri) by nix (subscriber, #2304) [Link]

I'm always trying to open my house's front door with my train ticket, and do SecureID authentication with my iPod (hey, it's sort of the same shape and size, i.e., rectangularish, and smaller than a brick).

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 15:59 UTC (Tue) by afalko (guest, #37028) [Link] (1 responses)

Gentoo gets it right too.

CLI Magic: Bash complete (Linux.com)

Posted May 9, 2006 22:07 UTC (Tue) by NapalmLlama (guest, #26327) [Link]

How so?

I'm on Gentoo and if I turn the bell on BASH beeps at me all the time...

missing: dabbrev-expand

Posted May 9, 2006 19:17 UTC (Tue) by dann (guest, #11621) [Link] (2 responses)

The main type of completion missing in bash is 'dabbrev-expand'. In tcsh (and emacs) it is bound to M-/
M-/ can complete the current word on the command lines with items from history. This is sometimes much faste than TAB completion if you have entered parts of the cmd line before...

missing: dabbrev-expand

Posted May 9, 2006 22:02 UTC (Tue) by micampe (guest, #4384) [Link] (1 responses)

That looks like Ctrl-r in Bash (search backwards in history).

missing: dabbrev-expand

Posted May 10, 2006 0:07 UTC (Wed) by dann (guest, #11621) [Link]

No, it is not, Ctrl-r implements a completely different functionality
(and is available on all modern shells)

Here's the documentation for dabbrev-expand from the tcsh manual:

dabbrev-expand (M-/)
Expands the current word to the most recent preceding one for
which the current is a leading substring, wrapping around the his-
tory list (once) if necessary. Repeating dabbrev-expand without
any intervening typing changes to the next previous word etc.,
skipping identical matches much like history-search-backward does.

CLI Magic: Bash complete (Linux.com)

Posted May 12, 2006 14:31 UTC (Fri) by dododge (guest, #2870) [Link] (1 responses)

A potential downside to bash's programmable completion is when a distribution has pre-loaded it with a ton of rules that override normal completion. For example someone has preconfigured a rule so that bash will only complete *.txt files for the command you're trying to run, but the file you want to use doesn't match that pattern. I forget where I ran into this situation, but I've seen it in action and "annoying" doesn't even begin to describe it.

The most interesting auto-completion system I've ever used was rk, the "Reactive Keyboard". This was an add-on that wrapped your shell session and intercepted command-line input. It was useful with e.g. the old Sun csh because it gave you things like a browsable history and emacs bindings. Its big trick, though, is that it could predict what you were going to type next based on prior input. And since it was sitting on top of the entire shell session it actually kept running when you entered other command-line tools such as "ftp" and could then predict your next ftp commands such as "cd pub" or "binary". I recall it being pretty good at the predictive stuff, and very easy to work with.

The downside was that it could end up using several megabytes of RAM to hold the predictive history, and back in those days it really hurt for each shell to be eating 8M.

It was apparently someone's thesis project and there was even a book published about it, but the software pretty much died sometime in the mid-90s. You can still find old versions of the sourcecode lying around if you look really hard, but none of the ones I've seen are Linux-ready.

CLI Magic: Bash complete (Linux.com)

Posted May 12, 2006 16:08 UTC (Fri) by liljencrantz (guest, #28458) [Link]

Writing completion rules is harder than you'd think. It is extremely hard to foresee all reasonable usecases for a non-trivial command. If you limit the completions too much, you'll end up with the frustration of spelling out things shat 'should' be completable, if you have too many completions, they will be worthless because there will be too much noise.

In my experience, it is generally better to include a bit too many completions instead of a bit too few, but not by a wide margin.


Copyright © 2006, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds