FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Kubuntu User

 
 
LinkBack Thread Tools
 
Old 01-23-2009, 07:39 PM
Paul Rumelhart
 
Default Shell tab-completion and other helpful command-line tricks

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.

Some examples:

Filenames:

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.

Others:

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.

Paul


--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-23-2009, 08:46 PM
Bruce Marshall
 
Default Shell tab-completion and other helpful command-line tricks

On Friday 23 January 2009, Paul Rumelhart wrote:
> 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.

One I use a lot is cd -

Takes you back to the previous directory you were in.

--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-24-2009, 07:12 AM
Nils Kassube
 
Default Shell tab-completion and other helpful command-line tricks

Paul Rumelhart wrote:
> One of the best time savers when in a bash shell is it's feature called
> "tab completion".

Indeed.

> Some examples:
>
> Filenames:

There are two things you didn't mention.

1. If a file name contains special characters the command completion uses
backslashes as escape character in front of the special characters. E.g.
if you have a file name "isn't this funny?.txt" and you type

ls is<tab>

the result of the command completion is this:

ls isn't this funny?.txt

2. The command completion doesn't work within quotes. Using the file name
above it wouldn't work if I type this:

ls "is<tab>

So if you want to use command completion, don't use quotes.

> 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.

Did you try that? It doesn't work here. Or is there some special addon to
bash needed to make it work? If you don't know the switches, use
the --help option. Many commands answer with a usage message.

> 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.

Doesn't work either. But if you want to get a list of your aliases just
try the alias command without parameters.


Nils

--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-24-2009, 08:01 AM
Donn
 
Default Shell tab-completion and other helpful command-line tricks

On Saturday, 24 January 2009 10:12:41 Nils Kassube wrote:
> 2. The command completion doesn't work within quotes. Using the file name
> above it wouldn't work if I type this:
>
> ls "is<tab>
>
> So if you want to use command completion, don't use quotes.

Works fine for me....

d

--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-24-2009, 08:11 AM
Jonas Norlander
 
Default Shell tab-completion and other helpful command-line tricks

2009/1/24 Nils Kassube <kassube@gmx.net>:
> Paul Rumelhart wrote:
>> One of the best time savers when in a bash shell is it's feature called
>> "tab completion".
>
> Indeed.
>
>> Some examples:
>>
>> Filenames:
>
> There are two things you didn't mention.
>
> 1. If a file name contains special characters the command completion uses
> backslashes as escape character in front of the special characters. E.g.
> if you have a file name "isn't this funny?.txt" and you type
>
> ls is<tab>
>
> the result of the command completion is this:
>
> ls isn't this funny?.txt
>
> 2. The command completion doesn't work within quotes. Using the file name
> above it wouldn't work if I type this:
>
> ls "is<tab>
>
> So if you want to use command completion, don't use quotes.
>
>> 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.
>
> Did you try that? It doesn't work here. Or is there some special addon to
> bash needed to make it work? If you don't know the switches, use
> the --help option. Many commands answer with a usage message.

Works for me.

>> 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.
>
> Doesn't work either. But if you want to get a list of your aliases just
> try the alias command without parameters.
>

Works for me.

~$ bash --version
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
On Kubuntu Intrepid

/ Jonas

--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-24-2009, 08:18 AM
Nils Kassube
 
Default Shell tab-completion and other helpful command-line tricks

Donn wrote:
> On Saturday, 24 January 2009 10:12:41 Nils Kassube wrote:
> > 2. The command completion doesn't work within quotes. Using the file
> > name above it wouldn't work if I type this:
> >
> > ls "is<tab>
> >
> > So if you want to use command completion, don't use quotes.
>
> Works fine for me....

Oops, now it works here as well. I must have mistyped it when I tried it
earlier this morning, maybe it was a lack of coffee? Anyway, sorry - and
scratch that part.


Nils

--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-24-2009, 08:55 AM
Nils Kassube
 
Default Shell tab-completion and other helpful command-line tricks

Jonas Norlander wrote:
> Works for me.
>
> ~$ bash --version
> GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
> On Kubuntu Intrepid

Ah, then that is a new feature. I'm using 8.04 where it doesn't work.


Nils

--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-24-2009, 09:00 AM
Jonas Norlander
 
Default Shell tab-completion and other helpful command-line tricks

2009/1/24 Nils Kassube <kassube@gmx.net>:
> Donn wrote:
>> On Saturday, 24 January 2009 10:12:41 Nils Kassube wrote:
>> > 2. The command completion doesn't work within quotes. Using the file
>> > name above it wouldn't work if I type this:
>> >
>> > ls "is<tab>
>> >
>> > So if you want to use command completion, don't use quotes.
>>
>> Works fine for me....
>
> Oops, now it works here as well. I must have mistyped it when I tried it
> earlier this morning, maybe it was a lack of coffee? Anyway, sorry - and
> scratch that part.
>
>
> Nils
>

That on the other hand don't work for me, neither in Konsole or on a
"pure" console.

/ Jonas

--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-24-2009, 12:52 PM
Derek Broughton
 
Default Shell tab-completion and other helpful command-line tricks

Nils Kassube wrote:

> Paul Rumelhart wrote:

>> 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.
>
> Did you try that? It doesn't work here. Or is there some special addon to
> bash needed to make it work?

bash completion is fairly arcane to me, but aiui it's up to the command's
packager to add some relevant file with the completions. Everybody should
have apt-get - so you can try "apt-get -<tab>". Unfortunately, apt-get's
completions are part of the default system, so the "apt" package doesn't
have a special file in it.

> If you don't know the switches, use
> the --help option. Many commands answer with a usage message.
>
>> 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.

Works for me (doesn't need two <tab>s).

The coolest discovery I ever made about bash completion was the fact that:

> scp filename somehost::<tab>

works! It's pretty slow for the host where I usually want to use it, but so
handy.



--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 
Old 01-24-2009, 12:59 PM
Derek Broughton
 
Default Shell tab-completion and other helpful command-line tricks

Jonas Norlander wrote:

> 2009/1/24 Nils Kassube <kassube@gmx.net>:
>> Donn wrote:
>>> On Saturday, 24 January 2009 10:12:41 Nils Kassube wrote:
>>> > 2. The command completion doesn't work within quotes. Using the file
>>> > name above it wouldn't work if I type this:
>>> >
>>> > ls "is<tab>
>>> >
>>> > So if you want to use command completion, don't use quotes.
>>>
>>> Works fine for me....
>>
>> Oops, now it works here as well. I must have mistyped it when I tried it
>> earlier this morning, maybe it was a lack of coffee? Anyway, sorry - and
>> scratch that part.
>
> That on the other hand don't work for me, neither in Konsole or on a
> "pure" console.

Nor me - and I really wouldn't have expected it to. Since bash explicitly
prevents expansion of anything inside double quotes, it seems perfectly
normal that bash completion can't work.

However, that last "belief" is obviously wrong (!) as I find the completion
works in some cases at least:

> "apt-c<tab>
> apt-cache apt-cdrom apt-config




--
kubuntu-users mailing list
kubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/kubuntu-users
 

Thread Tools




All times are GMT. The time now is 11:49 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org