emacs tip: getting lines numbered

the other day i was coding a scala apply(array: Array[String}) method to instantiate an object from a CSV file. i ended up with something like this:

apply(uuid = uuid,
source = source,
hostname = rewired(0),
type = rewired(0),
state = rewired(0),
category = rewired(0),
id = rewired(0),
lifecycle = rewired(0),
classification = rewired(0),
flag = rewired(0))

there were about a 100 parameters to use. i wasn’t really too excited about having to change the rewired(0) to use the proper index by hand. so, here’s what i did:

  • in emacs mark the region
  • then invoke shell-command-on-region and
  • use perl -pi -e 's{rewired\(0\)}{sprintf("rewired(%d)", $. - 1)}e;' as command to invoke

emacs will then show you the result of the command in a temporary buffer, you can either copy and paste from there, or just repeat the shell-command-on-region and prefix it with ctrl-u — emacs with then replace the region with the output of the command directly.


Comments !