<p>I've read in several places that the rectangle functions in emacs are very useful. I've read a bit about them, and I can't quite figure why. I mean, when you want to kill a paragraph, you mark the first row/column and then the last one, and  that's actually a rectangle, right? But you can still use the normal kill...</p>

<p>So what kind of transformations would you do with them?</p>

[{'answer_id': 58885, 'author': 'David Webb', 'author_id': 3171, 'author_profile': 'https://Stackoverflow.com/users/3171', 'pm_score': 6, 'selected': True, 'text': '<p>If you have data in columns in a text file with <code>M-x delete-rectangle</code> or <code>M-x kill-rectangle</code> you can delete a single column of data.  Similarly, <code>M-x yank-rectangle</code> will paste in a column of text.</p>\n\n<p>For example, take the following text:</p>\n\n<pre><code>1. alligator    alphorn\n2. baboon       bugle\n3. crocodile    cornet\n4. dog          didgeridoo\n5. elephant     euphonium \n6. fish         flugelhorn   \n7. gopher       guitar\n</code></pre>\n\n<p>Select from the a of alligator to the g of guitar.  The beginning and end of the selection mark out two opposite corners of the rectangle.  Enter <code>M-x kill-rectangle</code> and you immediately have:</p>\n\n<pre><code>1. alphorn\n2. bugle\n3. cornet\n4. didgeridoo\n5. euphonium \n6. flugelhorn\n7. guitar\n</code></pre>\n\n<p>Next put the mark at the end of the top line, add a few spaces if required and enter <code>M-x yank-rectangle</code> and ta-da!  You have re-ordered the columns:</p>\n\n<pre><code>1. alphorn      alligator    \n2. bugle        baboon       \n3. cornet       crocodile    \n4. didgeridoo   dog          \n5. euphonium    elephant     \n6. flugelhorn   fish         \n7. guitar       gopher       \n</code></pre>\n'}, {'answer_id': 59523, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 4, 'selected': False, 'text': '<p>I like to use rectangle for 2 main purposes, inserting the same text on every line, or killing a column of text (similar to Dave Webb\'s answer).</p>\n\n<p>There are 2 useful shortcuts for these, <code>C-x r k</code> will kill a rectangle, and <code>C-x r t</code> to insert (there are other rectangle commands with a <code>C-x r</code> prefix, but these are the ones I use).</p>\n\n<p>So let\'s say you want to take some code and format it so that you can post it in a Stack Overflow post... you need to prefix with 4 spaces.  So, go to the beginning of the first line, <code>C-SPC</code> to mark, then go to the beginning of the last line and <code>C-x r t &lt;SPC&gt; &lt;SPC&gt; &lt;SPC&gt; &lt;SPC&gt; &lt;RET&gt;</code>, and there you have it!  Then you can just copy and paste it into Stack Overflow.  I have run into more complex situations where this is useful, where you actually have text you want to insert on every line at a particular place.</p>\n\n<p>So the other situation like Dave Webb\'s situation, if you want to kill a rectangle, use <code>C-x r k</code> though, because it\'s just a lot quicker ;-)</p>\n\n<p>Also, according to my <a href="http://refcards.com/docs/gildeas/gnu-emacs/emacs-refcard-a4.pdf" rel="nofollow noreferrer">reference card</a> that I printed out when I first started, you can do the following:</p>\n\n<ul>\n<li><code>C-x r r</code>: copy to a register</li>\n<li><code>C-x r y</code>: yank a rectangle</li>\n<li><code>C-x r o</code>: open a rectangle, shifting text right (whatever that means...)</li>\n<li><code>C-x r c</code>: blank out a rectangle (I assume that means replace it with spaces, but you\'d have to try it out to see)</li>\n<li><code>C-x r t</code>: prefix with text (as described above)</li>\n<li><code>C-x r k</code>: killing (as described above)</li>\n</ul>\n'}, {'answer_id': 23968635, 'author': 'Adobe', 'author_id': 788700, 'author_profile': 'https://Stackoverflow.com/users/788700', 'pm_score': 1, 'selected': False, 'text': "<p>In emacs24+ there's also function for numbering lines:</p>\n\n<pre><code>(rectangle-number-lines START END START-AT &amp;optional FORMAT)\n\nInsert numbers in front of the region-rectangle.\n\nSTART-AT, if non-nil, should be a number from which to begin\ncounting.  FORMAT, if non-nil, should be a format string to pass\nto `format' along with the line count.  When called interactively\nwith a prefix argument, prompt for START-AT and FORMAT.\n</code></pre>\n\n<p>It is binded to <kbd>C-x r N</kbd> by default. </p>\n"}]
['https://Stackoverflow.com/questions/58872', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3785/']