Power Shell Usage

Bash Tips & Tricks

UKUUG Linux 2003 Conference • August 2003

This view lists all the slides from the talk in a continuous format. There is a PDF version available, suitable for printing on A4 paper.

The slides are also designed to be viewed as a slideshow from this file with Mozilla or a compatible browser. JavaScript is used to make pressing N progress to the next slide. The slides fit on a 1024 × 768 pixel display. To toggle full-screen mode in Mozilla press F11.

Both of these problems stem from only writing history lines to disk
when a shell exits

The solution is to append each line to the history individually

Specify this in .bashrc

Make Bash append rather than overwrite the history on disk:

shopt -s histappend

Whenever displaying the prompt, write the previous line to disk:

PROMPT_COMMAND='history -a'

A new shell gets the history lines from all previous shells

If you’ve configured your terminal windows to run log-in shells then you
might need to use .bash_profile instead of
.bashrc. But on many systems
.bash_profile sources .bashrc
anyway. This also applies to other mentions of .bashrc
in this presentation.

zip Up goes to the most recent zip command.
Further presses of Up cycle through previous zip
commands.

gv Up goes to the most recent command starting with those
letters. Suppose that found a gvim command and you were looking for
a gv command: pressing Space Up would then go to
the most recent line starting gv␣.

Press repeatedly to cycle through the final argument from earlier
commands

Suppose these commands have been executed:

$ mount /mnt/cdrom
$ ls /var/tmp

It is then possible to type the start of the next command line (such as
ls -l) and simply press Meta+. to have
/var/tmp be ‘typed’ for you. Pressing Meta+.
again (without an intervening keystroke) will ‘untype’
/var/tmp and replace it with /mnt/cdrom.

Note that on PCs Alt typically functions as
Meta, so Alt+. is what would be pressed. This
applies to all mentions of Meta in this talk.

You can then list the files’ sizes without having to type their names
again:

$ ls -lh !*

Then you can rename the new file to the original name (deleting the
uncrushed file in the process). The new name can be ‘typed’ with
Meta+., and the original name picked out of a previous
command:

$ mv Meta+. !-2^

Before committing a file to CVS you check over which changes you’ve made.
Vim conveniently uses colour to highlight the changes, and using a separate
window means that they can be kept on-screen while typing the commit message:

$ cvs diff GBdirect/DocTools/Util.pm | gview -

Then the file can be committed, picking its name out of the previous
command line:

doc can be made to change to the specified package’s
documentation directory and display the files therein:

function doc
{
pushd "/usr/share/doc/$1" && ls
}
export -f doc

Functions can be created for performing file conversions. For example
this function takes a single XFig file, such as
network.fig, and produces a PostScript file with the
same basename, such as network.ps:

Bash’s programmable completion enables commands’ arguments to be
completed intelligently for different commands

Enable it from .bashrc:

source /etc/bash_completion

When writing a document you often end up with several files with similar
names but different extensions:

$ ls
Bash_tips.aux Bash_tips.log Bash_tips.pdf Bash_tips.tex

But with programmable completion, Bash will pick the filetype that matches
the command and ignore all the others:

$ acroread ba

Pressing Tab converts the above to:

$ acroread Bash_tips.pdf

If that was the only PDF file in the directory you don’t need to type
any of the filename: just type the command name then press
Tab and the filename will be inserted. And even if there are a
few PDF files, it still may be quicker not to type any of their names and
just use Meta+S to cycle through them.

This is useful for any application which only works with a limited group
of filetypes. It can also be used to exclude filetypes from commands. For
example image and sound files can be excluded from filename completions for
text editors.