{"id":11511,"date":"2023-07-22T11:57:01","date_gmt":"2023-07-22T15:57:01","guid":{"rendered":"https:\/\/irreal.org\/blog\/?p=11511"},"modified":"2023-07-22T11:57:01","modified_gmt":"2023-07-22T15:57:01","slug":"moving-text-in-emacs","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=11511","title":{"rendered":"Moving Text in Emacs"},"content":{"rendered":"<p>Charles Choi has another great Emacs post. It\u2019s about <a href=\"http:\/\/yummymelon.com\/devnull\/moving-text-elegantly-in-emacs.html\">Moving Text Elegantly in Emacs<\/a>. The TL;DR is that Choi considers some elegant ways of moving various text objects forward and backwards. It\u2019s really a generalization of the <code>transpose-*<\/code> functions. We all know about <code>transpose-chars<\/code> and <code>transpose-words<\/code> and some of us even remember that you can do the same with sentences. On my system, at least, <code>transpose-sentences<\/code> isn\u2019t bound to any keys but I use it rarely enough that I don\u2019t mind typing in the command. It\u2019s easy to remember the name so I consider it part of my toolkit.<\/p>\n<p>There\u2019s also a <code>transpose-sexps<\/code> but it\u2019s action is a bit more mysterious. When you\u2019re in a Lisp buffer, it does just what you\u2019d think it would but for other languages or text based buffers the situation is more complicated. In those cases the idea of an s-expression is replaced by the more general \u201cbalanced expression\u201d. A balanced expression is something between matching delimiters like \u201c\u201d, [], &lt;&gt; and so on. Part of what makes things so complicated is that what those delimiters are depends on the buffer mode.<\/p>\n<p>Choi\u2019s idea is to use a combination of moving forward or backward over a text object and the transpose functions to create a series of functions to move those objects forward or backward over objects of the same type. He motivates this with the example<\/p>\n<pre class=\"example\" id=\"org241e194\">[\"a\", \"b\", \"c\", \"d\"]\n<\/pre>\n<p>Suppose, he says, you want to move the <code>\"a\"<\/code> forward into the third position so that the list becomes<\/p>\n<pre class=\"example\" id=\"org488ca74\">[\"b\", \"c\", \"a\", \"d\"]\n<\/pre>\n<p>It\u2019s easy to brute force that, of course, but if you have to do it repeatedly, it quickly becomes tedious. Choi creates a simple function using just <code>forward-sexp<\/code> and <code>transpose-sexps<\/code> that does this easily. He has several functions for moving various objects forward and backward.<sup><a id=\"fnr.1\" class=\"footref\" href=\"#fn.1\" role=\"doc-backlink\">1<\/a><\/sup><\/p>\n<p>He also observes that these are seldom used routines and not worth remembering let alone binding a key sequence to. His solution is to add them to the <code>Edit<\/code> menu. That\u2019s perfect because all you have to do is remember that they exist and can otherwise forget about them until you need them.<\/p>\n<p>It\u2019s a good post so take a look when you have a few minutes.<\/p>\n<div id=\"footnotes\">\n<h2 class=\"footnotes\">Footnotes:<\/h2>\n<div id=\"text-footnotes\">\n<div class=\"footdef\"><sup><a id=\"fn.1\" class=\"footnum\" href=\"#fnr.1\" role=\"doc-backlink\">1<\/a><\/sup><\/p>\n<div class=\"footpara\" role=\"doc-footnote\">\n<p class=\"footpara\">\nIf you want to experiment with the above example, be sure to do it in some sort of text buffer. In particular, it won\u2019t work in the <code>*scratch*<\/code> buffer because that\u2019s a Lisp buffer and its definition of \u201cbalanced expression\u201d isn\u2019t right for this example.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Charles Choi has another great Emacs post. It\u2019s about Moving Text Elegantly in Emacs. The TL;DR is that Choi considers some elegant ways of moving various text objects forward and backwards. It\u2019s really a generalization of the transpose-* functions. We &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=11511\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-11511","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\/11511","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=11511"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11511\/revisions"}],"predecessor-version":[{"id":11512,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11511\/revisions\/11512"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}