Fun with terminal and Vim macros

April 19, 2015, [MD]

I always enjoy finding new ways of automating boring tasks, and today I had a few different small tasks that I was able to solve using some terminal-fu, and Vim macros. I really enjoyed how well it came together, so I thought I'd put together a short screencast to demonstrate.

I had generated a number of discussion forum graphs for different courses. The processed data is in a json file in a directory named after the course. My task was to put an identical index.html file into each directory, create an index of the courses, with links, and modify all the landing pages to reflect the names of the courses. Not a hugely complicated task, but something you could run into day to day, and which would be quite cumbersome to do manually.

The video should be quite self-explanatory, but I've included some "show-notes" below.

As far as I know, ls doesn't have a built-in way to list only directories, so I found this snippet somewhere and added it to my .zshrc:

alias ld="ls -lht | grep '^d'"

awk is a very powerful string manipulation tool, but in this case we're just using it to get the 9th column of each line (awk '{ print $9 }')

xargs is very useful to run commands on input arguments. We need -n 1, otherwise it would put use all the arguments in the same command. With -n 1, it executes the command once for each input argument. If we didn't want to put the arguments last, we could do something like echo 1 2 3 | xargs -n 1 - J % echo % hi.

Stian HĂ„klev April 19, 2015 Lijiang, Yunnan, China
comments powered by Disqus