[Emacs] Fun with rectangles

Lets have some fun with emacs rectangles. Lets assume you have the following text:

012345678901234567890123456789
      val1 = 10;
      val2 = 20;
      val3 = 30;

Now, you need to add "(int)" in front of all the numbers. You can do it one line at a time, or you could use rectangles in emacs. Move the cursor to the start of 10, set mark, make the selection until start of 30. Hint: To set the mark, just hit Ctrl+Space and take your cursor to where you need to stop. Something like this,

012345678901234567890123456789
      val1 = 10;
      val2 = 20;
      val3 = 30;

Ok, now lets insert the string using "string-insert-rectangle" and enter the string that we want to be inserted, in our case "(int)"

M-x string-insert-rectangle
String insert rectangle (default void ): (int)

012345678901234567890123456789
      val1 = (int)10;
      val2 = (int)20;
      val3 = (int)30;

Now, say you wanted to give a space between the (int) and the number. Mark the rectangle as show below:

012345678901234567890123456789
      val1 = (int)10;
      val2 = (int)20;
      val3 = (int)30;

We will use the "open-rectangle" command now.

M-x open-rectangle

012345678901234567890123456789
      val1 = (int) 10;
      val2 = (int) 20;
      val3 = (int) 30;

Cool, now let's delete the spaces in the front using "delete-rectangle". Make the rectangle and...

012345678901234567890123456789
      val1 = (int) 10;
      val2 = (int) 20;
      val3 = (int) 30;

M-x delete-rectangle

012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;

Now, just for fun, we are going to cut a rectangle and yank it somewhere else. Lets remove 1,2,3 from the variable names and put them in the front (bad idea, I know). Lets select the rectangle and call the "kill-rectangle" command

012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;

M-x kill-rectangle

 012345678901234567890123456789
|val = (int) 10;
 val = (int) 20;
 val = (int) 30;

Move the cursor (show above as | ) to where you want to yank the rectangle now (in our case, the beginning of the word) and run "yank-rectangle"

M-x yank-rectangle

012345678901234567890123456789
1val = (int) 10;
2val = (int) 20;
3val = (int) 30;

Now, of course there are key-binding available (its Emacs for heaven's sake, even if there is no key-binding you can make your own).

From the Emacs manual
C-x r k
Kill the text of the region-rectangle, saving its contents as the “last killed rectangle” (kill-rectangle).
C-x r d
Delete the text of the region-rectangle (delete-rectangle).
C-x r y
Yank the last killed rectangle with its upper left corner at point (yank-rectangle).
C-x r o
Insert blank space to fill the space of the region-rectangle (open-rectangle). This pushes the previous contents of the region-rectangle to the right.
C-x r N
Insert line numbers along the left edge of the region-rectangle (rectangle-number-lines). This pushes the previous contents of the region-rectangle to the right.
C-x r c
Clear the region-rectangle by replacing all of its contents with spaces (clear-rectangle).
M-x delete-whitespace-rectangle
Delete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.
C-x r t string
Replace rectangle contents with string on each line (string-rectangle).
M-x string-insert-rectangle string
Insert string on each line of the rectangle.
Enjoy!

Using Shift+Arrow keys in Emacs from GNU screen

While inside screen, when using the Shift+Left, Shift-Right, etc keys in Emacs (1) to mark and select, if you see the 2A 2B 2C and 2D characters instead you are probably still using the default TERM which is set to "screen".

Simple solution, use xterm-vt220 or xterm-color instead. You can either create an alias for emacs or change your .screenrc file.

$ alias em='export TERM=xterm-vt220 && emacs -nw'

OR in your ~/.screenrc add,

term xterm-vt220

----------------------------------------------------------------
(1) You need to use Emacs 23 or above.