## Rectangles

I never use the rectangle commands as often as I should.

Here’s my shopping list:

• Cheese
• Milk
• Yoghurt

I want to make the above into a list. There are all sorts of ways I could so this, one way is to use the string-rectangle command. Set (or highlight) the region to zero width in front of the list and then C-x r t – to insert a dash before each of the words.

You can use the kill-rectangle command to remove unwanted bullet points (particularly useful when importing text from PDFs)

Set the region to cover the bullet symbols and then C-x r k to kill the rectangle. Don’t forget you can C-x r y yank that rectangle back.

C-x r o or open-rectangle is a quick way to indent a block of text to where you want it, especially if you can’t remember the other commands.

And lastly C-x r c will clear a rectangular space. I can’t remember ever using that one, to be honest…

If you find pressing C-x r a keystroke too far, read this post on remapping C-x r  to C-`

## Summary of Commands

```C-x r k    Kill rectangle
C-x r y     yank last killed rectangle
C-x r c     Clear rectangle
C-x r o     open rectangle and shift text right
C-x r t spoons  fill rectangle with spoons
C-x r r q   Copy rectangle to register q
C-x r i q   Insert rectangle from register q
```

## Steps to Learning Programming

There are lots of programming languages designed to make learning programming easier. In my experience they are a waste of time for most students. Many of the languages will allow students to make apparent progress and to produce what appear to be impressive applications, but if students don’t understand what they’re doing, they’ll quickly lose interest.

Here are the things that students need to learn, and the rough order in which they need to learn them.

• Imperative commands such as PRINT “hello”
• Variables and types, particularly the difference between strings and numbers
• Simple arithmetical operations e.g. a = 3, b =4, c = a+b
• Branch commands such as IF answer = “Paris” THEN PRINT “Correct”
• More complicated branch commands – IF THEN ELSE
• For loops or equivalent
• While loops or equivalent
• Nested branch commands
• Nested loop commands
• Arrays
• Traversing Arrays using for loops and while loops
• Functions and Procedures, or equivalent

And that’s it. Everything else in programming can be achieved using the above. The rest is just readability, convenience and elegance

The problem with some languages, particularly the visual ones, is that students produce results without understanding the above. If students don’t understand the above, they don’t understand programming.

Python allows you to teach all of the above. And then once the student has learned, they can build on what they know, replacing the pieces of their Python toolkit with more advanced constructs as they learn. And as programmers, they’re always learning…

## Dired Trick #1

Many people say that once you get the hang of Dired, you never use anything else. How true is this?

From a personal perspective I’d say that was true, but the key is in the first part of the sentence once you’ve got used to using Dired…

Here’s real world use for Dired.

C-x d to start Dired and navigate to a directory.

Here’s a directory into which a CD has been ripped. The track names haven’t been recognised.
``` /home/*****/Music/Ludwig van Beethoven/Beethoven- Symphonies Nos. 1 & 2: total used in directory 108088 available 32870008 drwxrwxrwx 2 ***** ***** 4096 Jun 8 2009 . drwxrwxrwx 4 ***** ***** 4096 Jan 10 2009 .. -rwxr--r-- 1 ***** ***** 19510080 Sep 21 2010 01 Track 1.mp3 -rwxr--r-- 1 ***** ***** 12268125 Sep 21 2010 02 Track 2.mp3 -rwxr--r-- 1 ***** ***** 7567075 Sep 21 2010 03 Track 3.mp3 -rwxr--r-- 1 ***** ***** 11410580 Sep 21 2010 04 Track 4.mp3 -rwxr--r-- 1 ***** ***** 20195615 Sep 21 2010 05 Track 5.mp3 -rwxr--r-- 1 ***** ***** 19836565 Sep 21 2010 06 Track 6.mp3 -rwxr--r-- 1 ***** ***** 7501945 Sep 21 2010 07 Track 7.mp3 -rwxr--r-- 1 ***** ***** 12329080 Sep 21 2010 08 Track 8.mp3 ```
I could rename each track using a GUI by right clicking on each one and changing the name. Dired mode is faster

C-x C-q to toggle read only. Now simply type the new names of the tracks directly into the buffer. C-c C-c when you’ve finished.

If you want to be more efficient, move the point to the T in Track 1 and press M-z 1 to zap-to-char 1, deleting everything up to including the 1, leaving the .mp3 intact. Now type in the new track name.

If you want to be really efficient, of course, you could use search and replace…

Dired Tricks #2

## Emacs Snooker

The key to snooker isn’t where the coloured balls go, it’s where your white ball stops. You should always be thinking about setting up your next shot. Something similar holds when using Emacs. Where will the point (cursor) be when you’ve completed your current operation?

Consider the following commands:
M-u Convert to uppercase to the end of the word
M-l Convert to lowercase to the end of the word
M-c Capitalize word

Note that the point jumps to the end of the word when each command is called. This makes it easy to jump through a string of words, changing case or capitalizing as you wish.

Now consider the following

M– M-u Convert previous word to uppercase
M– M-l Convert previous word to lowercase
M– M-c Capitalize previous word
M– means (Alt) and –

Note that the point remains where it is, at the end of the word you’ve just converted. You can’t jump back through a series of words converting as you go.

It’s like the difference between hitting C-o and hitting Enter. At first glance they both appear to do the same thing, but look at the point…
(image: www.freeimages.co.uk)