1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
## What is this?
`cdlog` is a small amount of GNU Bash code that provides a better
interactive experience for directory changes than
Bash's built-in directory stack mechanism (commands `pushd`, `popd`
and `dirs`, plus `~-` expansion).
## How to use
Install the file `cdlog.sh` somewhere, and source it from your
`.bashrc` script.
The user interface consists of three commands:
* `cd` is now an alias which calls the function `cdlog.chdir`.
Every time you change directory, it pushes the previous directory
into a FIFO log. The log maintains nine entries. The ninth entry
is erased. The entries are stored in the variables `c1` through `c9`.
The first four entries are also copied into the variables `x`, `y`,
`z` and `w` for shorter access.
* `cs` (cd swap) is an alias for a command which exchanges
the current directory with a selected `cslog` entry selected
by a numeric argument, which defaults to 1. The current directory
overwrites that entry in the log, and then the shell changes to the directory
which was previously in that entry. If `cs` is given two or more
arguments, it rotates among those. The directory is changed to the
`cdlog` item indicated by the first argument. That item is overwritten
by the second item, the second by the third and so on. The last item
receives the previously current directory.
* `pd` (pop dir) is an alias for a command which changes to the most recent
directory in the log, and removes that entry from the log. The second
most recent directory becomes most recent and so on.
* `cdlog` function shows a listing of the four most recent entries in the
log. The `cl` command is an alias for this.
* The `mcd` and `mcs` are menu-selection-based analogs of `cd` and `mcs`.
They print the contents of the FIFO (all nine entries), and you can
pick which directory to change to or swap with. The terminal cursor is then
retraced back to the top of the menu, and the screen is erased from
that point to the bottom.
In addition, the `cdlog.sh` script sets the bash `direxpand` option.
With the `direxpand` option, Tab completion on a directory coming
from a variable name will expand that variable into the command line.
The directory changing commands do nothing if you attempt to change into the
same directory you are already in. There is no effect on the FIFO.
## How is this better?
* It's not hard-coded C code inside the shell; it consists of a small amount of
code whose behavior that can be customized by the end-user.
* Uses simple variables, which are better supported for completion
and require fewer keystrokes.
* The `dirs` command for accessing the directory stack in Bash is too
clumsy and won't be discussed here any more.
* Bash's shorthand notation for accessing the nth element, namely `~-n`, takes
more keystrokes than `cdlog`'s aliases for the first four elements, `$x`
through `$w`.
* Bash's shorthand notation requires an explicit slash to be typed
in order for completion to work. `~-n` followed by tab is not sufficient.
You must type a slash and then Tab. Whereas completing on the variable `$x`
just requires Tab. One tab will automatically add the slash and then additional
tabs engage further completion.
* Bash's `direxpand` option does not work for `~-n` notation. Completion
works, but the notation remains unexpanded.
* `cslog` has intuitive, simpler commands. The actions of pushing entries into
the log, and rotating among them aren't conflated into a single command.
The rotating command `cs` is usefully different from `pushd [+/-]n`,
taking only positive integer arguments with no sign, and rotating among
specified places, rather than a contiguous block of top entries.
## License
This is under distributed under a modified two-clause BSD license.
See the license block at the bottom of the `cdlog.sh` file.
|