{"id":1253,"date":"2012-10-08T06:04:00","date_gmt":"2012-10-08T10:04:00","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=1253"},"modified":"2012-10-08T06:05:33","modified_gmt":"2012-10-08T10:05:33","slug":"editing-a-column-of-numbers","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=1253","title":{"rendered":"Editing A Column of Numbers"},"content":{"rendered":"<p>Alex Schroeder has an interesting post on <a href=\"http:\/\/www.emacswiki.org\/alex\/2012-10-03_Search_and_Replace\">editing a column of numbers<\/a>. The problem is that given some text such as<\/p>\n<pre class=\"example\">|[[...]]     |   1 |          6563|     3796|   |[[...]] | \u2014 |\r\n|[[...]]     | 1\/3 |          2315|     1259|   |[[...]] | \u2014 |\r\n|[[...]]     | 1\/3 |           159|      607|   |[[...]] | \u2014 |\r\n|[[...]]     | 1\/3 |           159|      597|   |[[...]] | \u2014 |<\/pre>\n<p>add 56 to the numbers in the third column. Schroeder&#8217;s solution is to use <code>query-replace-regexp<\/code> with a complicated regular expression that selects the numbers in the third column and replaces them with<\/p>\n<pre class=\"src src-Emacs-lisp\">\\,(+ 56 \\#2)<\/pre>\n<p>That&#8217;s a nice solution and has the virtue of using only core Emacs functionality so that it will work with any Emacs. The only problem is that nasty regular expression. It&#8217;s not really that hard but has lots of room for error and will probably take most people a couple of tries to get right.<\/p>\n<p>Later, I stumbled upon a <a href=\"http:\/\/www.masteringemacs.org\/articles\/2012\/10\/02\/iedit-interactive-multi-occurrence-editing-in-your-buffer\/\">post<\/a> from the invaluable Mickey over at <a href=\"http:\/\/www.masteringemacs.org\/\">Mastering Emacs<\/a> on <code>iedit<\/code> that looked like it might do the job in an easier way. <code>iedit-mode<\/code> is much like Magnar Sveen&#8217;s <code>mark-multiple<\/code> package. They both allow you to mark identical pieces of text and then modify them all at once. The difficulty for the problem at hand is that the numbers are not the same and neither <code>iedit<\/code> nor <code>mark-multiple<\/code> work with regular expressions.<\/p>\n<p>Sveen, however, does have a wonderful solution with his spectacular <code>multiple-cursors<\/code> package. It has a mode that allows you to select a rectangle and then operate on each line of the rectangle together. You can see a similar example in operation by watching Sveen&#8217;s <a href=\"http:\/\/emacsrocks.com\/e13.html\">Emacs Rocks! Episode 13<\/a> (it&#8217;s at the end of the video). The idea is to select the numbers in the third column and edit them into a sexpr that adds 56. For example, the <code>6563<\/code> in the first row becomes <code>(+ 56 6563)<\/code>. With <code>multiple-cursors<\/code> all the rows are changed in parallel.<\/p>\n<p>Finally, we call <code>eval-and-replace<\/code> to replace the sexprs with their values. This is a truly beautiful solution: quick, easy, and hard to get wrong. Of course, you <em>do<\/em> have to load extra packages. First you need <code>multiple-cursors<\/code>. That&#8217;s an easy ELPA load from Marmalade and something every Emacs expert should have in their toolkit. You&#8217;ll also need <code>eval-and-replace<\/code>. That function is available in many Emacs predefined configurations such as the <a href=\"http:\/\/eschulte.github.com\/emacs-starter-kit\/\">the Emacs starter kit<\/a> but it&#8217;s definition is simple. <a href=\"http:\/\/irreal.org\/blog\/?p=297\">Here&#8217;s<\/a> my version: it&#8217;s only 5 lines so you can just grab it and plop it into your <code>.emacs<\/code> or <code>init.el<\/code>.<\/p>\n<p>The bottom line is that you can always solve problems like this using Schroeder&#8217;s solution in any Emacs installation but if you have a little bit of extra help loaded, the solution is trivial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Alex Schroeder has an interesting post on editing a column of numbers. The problem is that given some text such as |[[&#8230;]] | 1 | 6563| 3796| |[[&#8230;]] | \u2014 | |[[&#8230;]] | 1\/3 | 2315| 1259| |[[&#8230;]] | \u2014 &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=1253\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-1253","post","type-post","status-publish","format-standard","hentry","category-general","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1253"}],"version-history":[{"count":3,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1253\/revisions"}],"predecessor-version":[{"id":1262,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1253\/revisions\/1262"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}