Tab completion is awesome. It helps to quickly call commands without typing it entirely. For example in my home directory there is the directory Downloads
. If I want to change into it in a Bash-shell I simply have to enter cd Dow [tab]
. After the tab-press the directory name is completed automatically. To enable this feature you have to install the appropriate package. In Feodra it is bash-completion
, most distributions call the package similar.
However, sometimes there are more choices:
$ cd Do Dokumente/ Downloads/
In that case tab has to be pressed two times to unveil the two options. To have both options already on the first tab, create the file ~/.inputrc
and enter the lines
set show-all-if-ambiguous on
As usual, if you have other useful and short tips regarding Bash or the command line please let me know.
(Yes, I know about zsh’s great completion tool. Yes, I already used zsh quite some time. No, a tip how to set up Bash/ZSH with a fifty lines bashrc/zshrc is not suited for a comment here.)
Here are two short tips for bash:
1) Doing things like that often?
$ svn diff foo.cpp bar.h
$ svn ci foo.cpp bar.h
And do you mainly use the cursor keys to replace diff by ci in the second command?
Instead, do this:
$ svn diff foo.cpp bar.h
$ ^diff^ci^
You can even add things like “-m ‘some comment'” in the second command
2) Similarly to the one above, when doing things like
$ cp foo.cpp /a/very/long/path/here
$ ls /a/very/long/path/here
You can shorten it to
$ cp foo.cpp /a/very/long/path/here
$ ls !!:2
Some 5 years ago, this default was changed and annoyed me until started digging inspired by ur post:
set mark-symlinked-directories on
it is so good to have it back.
put this into your .bashrc:
# use a fancy prompt ๐
PS1=”\[33[01;32m\]\u@\h\[33[00m\]:\[33[01;34m\]\w\[33[00m\]”
PS1=”$PS1 \`if [ \$? = 0 ]; then echo -e ‘\[33[01;32m\]:)’;”
PS1=”$PS1 else echo -e ‘\[33[01;31m\]:(‘; fi\`\[33[00m\] \$ ”
export PS1
Now your prompt will look like this:
dh@eriador:~ ๐ $ # smily ๐ means return value of 0
dh@eriador:~ ๐ฆ $ # smily ๐ฆ means return value is != 0
I found it quite useful, thanks to Erlend for the tip.
The bash completion is the biggest reason I love Linux. Actually, bash in general. But this is one of my favorites.
Do you really need bash-completion just for file and directory completion? I thought that worked by default, and bash-completion just added things like restricting cd to directories and tar to files with suitable extensions (like the zsh completion).
Thx for the additional tips! However, I must admit that I already have my fancy command line prompt, I might post it here soon.
randomguy3, yes, the bash-completion is usually only needed for the extended completion, you’re right.
I really like this one…
cd() {
if [ “$PS1” ]
then
if [ “$1” ]
then builtin cd “$1” && ls
else builtin cd && ls
fi
else
if [ “$1” ]
then builtin cd “$1”
else builtin cd
fi
fi
}
One of the bash shortcuts I use the most, is “M-.” (meta-. or alt-.), which means “paste in the last argument of the last command”:
$ ls /some/long/path/to/file.txt
$ rm **press “M-.”**
(if you instead want the, say third argument, then press M-3, M-.)
Great tip, thanks