E_NO_SUCH_BLOG

Programming, philosophy, pedaling.


Mini-post: More Power User Tools

Apr 23, 2015

Tags: programming, workflow

At the request of a few friends and readers who enjoyed the post on compulsive configuration, I’m going to list and examine a few more “power user” tools that I’ve come to use. Some are purely textual, others are graphical, but all are intended to be invoked from the shell or shell scripts.

rename

When it comes to utilities, rename is about as simple as it gets. Written by Larry Wall, rename has three flags (all optional) and two arguments: a perl regular expression, and a list of files.

Let’s say you have a directory full of text files with crappy names:

Your directory

They were all made on the same date (“113”) and they’re all “datadumps” of some sort, so all you really want is the trailing number at the end. Used with shell wildcards and globs, rename makes this a breeze:

$ rename 's/datadump-113-(\d+)/$1/' *.txt

Of course, this regular expression could be made more robust:

s/(?:\S+)-(?:\d+)-(\d+)/$1/

The result:

Your directory

Most Linux distributions ship with rename in some form or another. If yours doesn’t, it can probably be found in either the util-linux or util-linux-ng package.

tee

tee is cat’s oft-forgotten sibling. Where cat reads a provided file or standard input and writes it directly to standard output, tee reads from standard input and writes to both standard output and a provided file.

This is invaluable for debugging and logging, especially when you want to send information to both the user and a log file without doing something ugly like this in your scripts:

function debug {
	echo "DEBUG: $@"
	echo "DEBUG: $@" >> ./debug.log
}

and with tee:

function debug {
	echo "DEBUG: $@" | tee -a ./debug.log # we need the -a flag to append
}

tee is included in the POSIX standard, so it should be available immediately on your system.

convert (and the rest of ImageMagick)

convert is an image format conversion tool, part of the ImageMagick suite. Conversion tools are commonplace nowadays, but convert (via ImageMagick) stands out because of its insane level of support for every image format under the sun.

Want to convert a PDF to the ancient X Bitmap format? No sweat:

$ convert yossarian.pdf yossarian.xbm

The result (viewed with display, another ImageMagick tool):

PDF to XBM

Like most ImageMagick utilities, convert takes a massive number of flags. For basic conversion purposes, however, only two arguments are required: the input file, and the output filename (with extension).

A full list of the formats supported by your ImageMagick installation can be found via identify -list format. Format support may vary depending on how your distribution builds and bundles ImageMagick, but it’s generally very good (and simple enough to augment and build on your own).

There plenty of other useful tools in the ImageMagick suite, including the general-purpose viewer display, the import screenshot tool, and mogrify for in-place image manipulation.

jump and family

This last command isn’t as much a tool as it is a few lines of bash, but it has been of great use to me. There seem to be a number of implementations of the concept, but my personal favorite is the one by Jeroen Janssens.

Unlike the others, Janssens’ jump only requires 4 (5 with tab-completion) tiny bash functions and a single environment variable, making its footprint comparatively tiny.

My version makes the commands slightly more terse, but preserves the same functionality when placed in ~/.bashrc or ~/.bash_profile:

export MARKPATH="${HOME}/.marks"

function jmp()
{
  cd -P "${MARKPATH}/${1}" 2> /dev/null || echo "No such mark: ${1}"
}

function mrk()
{
  mkdir -p "${MARKPATH}"; ln -s "${PWD}" "${MARKPATH}/${1}"
}

function umrk()
{
  rm "${MARKPATH}/${1}"
}

function mrks()
{
  ls -l "${MARKPATH}" | tail -n +2 | sed 's/  / /g' | cut -d' ' -f9- | awk -F ' -> ' '{printf "%-10s -> %s\n", $1, $2}'
}

function _completemarks()
{
  local curw=${COMP_WORDS[COMP_CWORD]}
  local wordlist=$(ls ${MARKPATH} 2> /dev/null)
  COMPREPLY=($(compgen -W '${wordlist[@]}' -- "${curw}"))
}

complete -F _completemarks jmp umrk

With these functions, you can “mrk” directories and “jmp” to them nearly instantly, skipping the tedium of typing cd /long/path/to/my/project/blah/etc. Keeping symlinks in a hidden directory isn’t exactly the cleanest thing out there, but it isn’t terrible for such useful but tiny commands.

Concluding Remarks

There are lots of “power user” tools out there, and this is only a small sampling of a small sampling of the ones that came to mind when writing this post. If you think that I’ve missed an important or especially useful one, please tell me - I’ll do my best to cover it in the future.

Happy hacking!

- William

Postnotes:

There are lots of really neat and niche utilities by the suckless community and in Joey Hess’s moreutils. In particular, I really like slock (a simple screen locker) and sponge, which soaks up its input such that a file may be both read from and written to in a pipeline without clobbering it.