Shell tab-completion and other helpful command-line tricks
godshatter at yahoo.com
Fri Jan 23 20:39:12 UTC 2009
I've been trying to keep up with the posts on this list, and have found
from going through some of them that there are some helpful basics for
using the command-line that we should share our knowledge about. This
will help people who don't use the command-line very often, and may even
help some of those who use it regularly. I've learned a few things just
experimenting prior to writing this email. However, this is very basic
stuff so I apologize if you find that frustrating.
When you are at the command-line in Kubuntu (and Ubuntu proper), by
default you are using a shell called "bash". A "shell" is simply a
program that accepts commands from the user, and outputs results of
those commands to the screen. The shell is used most often to launch
other programs, and to move around the system and examine things. It's
way more complicated than that, but let's keep it simple.
One of the best time savers when in a bash shell is it's feature called
"tab completion". Basically, in many cases, if you press the tab key
(<tab>) after typing the start of a command, you will be able to
complete that command without having to type the rest of it in. This
turns moving about the command-line and running complex tasks into a
much less frustrating experience.
Say you want to use "ls -l" to see how large the file "hello.txt" is.
If you have no other files in your current directory that start with a
lower-case h, you can type "ls -l h<tab>" which will then show "ls -l
hello.txt" on the prompt. You can then press <enter> to see the output.
If you have another file in your directory that starts with "h", then
the tab completion will show as much of the filename as it can before it
runs into a conflict. You can then supply the correct choice of the
next letter and hit <tab> again to continue with the tab completion.
Say you have two files that start with "h" in your current directory,
"hello.txt" and "help.txt", and you want to do an "ls -l" on hello.txt.
You can type "ls -l h<tab>", which will show "ls -l hel" on the screen,
because that's as far as it could go until it needed the user to choose
another letter. In this case, you would type the letter "l", because
that's the letter that differentiates it from help.txt. So, even in
this case you've gone from typing "ls -l hello.txt" (15 keystrokes) to
typing "ls -l h<tab>l<tab>" (10 keystrokes). Of course, this is much
more important when dealing with longer filenames.
Now, let's say that you don't remember that help.txt is also in the
directory, and you're wondering why the tab completion stopped at "ls -l
hel" in the second example. You can press tab again to see a list of
files that match the current tab completion to that point. In this
case, you'd see "hello.txt" and "help.txt". Of course, you could just
type the letter "l" because you don't need to know which file is
conflicting, but this can be useful when you've forgotten exactly how
you spelled something when you created the file.
I'm grouping everything else I know that tab completion works on here,
because the basics are exactly the same as for filenames.
Commands - if you forget what a program is called, and it is in your
path, you can type the first few letters that you do know and press
tab. For example, typing "ch<tab>" will not put any more letters on the
command from tab-completion because there are too many choices, but
pressing the tab key again ("ch<tab><tab>") will show all commands that
start with "ch".
Command-line switches - if you forget which switches ("--something") a
particular command takes, you can type the command followed by a space
and "--" and then press the tab key twice to get a list of possible
choices (for example "ls --<tab><tab>" will show you all of the switches
that ls will accept. This will likely work on most every non-graphical
program in the Ubuntu repositories and some graphical ones, but
third-party software might not use the right libraries to make this work.
Environment variables - an environment variable is used by the shell for
various tasks. One example is "PATH", which shows which directories
your systems looks in in what order to find things. You can echo the
contents of a variable to the screen by using the echo command and
putting a "$" before the variable name. So to see what your path is,
you can type "echo $PATH". If you forget the name of the variable you
are looking for, you can type "echo $<tab><tab>" to see a list of all
variables currently defined by your shell.
There are lots of others I keep finding. Many commands that have a
choice of arguments work to some degree. For example, typing "alias
<tab><tab>" will show you which aliases you have currently defined.
The basic rule of thumb is: if you are typing a command at the
command-line and you aren't sure of the syntax, try using tab-completion
to narrow down your choices.
I hope you find this helpful, and if you know of any cute tab-completion
tricks please let us know. If you use the command-line a lot, you'll
find that pressing the tab key becomes instinctive and you'll really
miss it when you are in a program that doesn't use it.
More information about the kubuntu-users