Go program stores directory paths
Programming Snapshot – cdbm

© Lead Image © lightwise, 123rf, 123RF.com
When you change directories at the command line, you often find yourself jumping back and forth between known paths. With a utility written in Go, Mike Schilli records the jumps and shows the way back.
While younger coworkers tend to edit their programs with clever IDEs, I still find it most natural to jump to local Git repositories with a quick cd
at the command line and fire up Vim at files with the source code residing there. Typing in the directory path each time is a pain in the ass, and there are usually only half a dozen paths back and forth – so the command line should be able to remember that.
The C shell invented the pushd
and popd
commands many years ago, but wouldn't it be much more convenient to automatically record the directories you visit, store them in a database, and even offer search queries for previously visited directories based on criteria such as frequency or the timestamp of the last visit?
In this issue, a Go program by the name of cdbm
collects the paths accessed by the user during a shell session; the command line user just uses cd,
and some magic glue in the shell's configuration then taps into the $PS1
prompt generator. If the directory changes, cdbm
gets called and stores the new path in an SQLite database on the disk, which later allows search queries whose results can be accessed directly by the user for navigation help. Bash users can modify their .bashrc
file to enable this. On typing a newly introduced command c
, users will see a selection list with the last directories visited (Figure 1). After selecting one of them with the cursor keys and pressing Enter, the shell directly jumps there (Figure 2).
[...]
Buy this article as PDF
(incl. VAT)