Archive

Posts Tagged ‘something-i-just-learned-about-emacs’

Something I just learned about Emacs part 1: deleting whitespace

September 29th, 2010 No comments

I’m very much a trainee Emacs user at the moment. I’ve been using it for about a year, and I’m much faster in it than in any other editor, but I’ve still got so much more to learn.

I’m planning to document these new things as I learn them in the hope that (a) writing it down will help me remember, (b) if (a) fails then at least I’ve got this as a references, and (c) maybe someone else is at a similar level of Emacs mastery to me, and they’ll learn something new with me.

delete-indentation

Let’s say you’ve got the following code:

function myAwesomeFunction(argumentOne,
                           argumentTwo,
                           argumentThree)

and you decide that it’s really not necessary having all those arguments on their own line – you want them all on the same line. My old technique would be to set the mark at the end of the first line with C-<SPC>, move down a line with C-n, back a word with M-b, kill the region with C-w, and insert the space with <SPC>.

The new technique uses delete-indentation, mapped to M-^. Just go anywhere in the last of the three lines, and press M-^ to remove all space between argumentTwo, and argumentThree) except for one space. Then press M-^ again to bring the line argumentTwo, argumentThree) up to the end of the previous line. Awesome!

The old technique takes 11 commands to process those three lines (assuming point is at the end of the first line). The new technique takes just 2 commands (assuming point is somewhere in the third line). That’s a 5.5x increase in productivity! Hell yeah.

just-one-space

Just one Cornetto,
give it to me,
delicious ice-cream, of Italy,
vanilla and choco dream,
Give me Cornetto,
from Wall’s ice cream.

Sorry about that. So, you’ve got the following annoying string:

"My string has a long      string of spaces in the middle"

If only there was some way of deleting all those spaces in the middle… My old technique was this: starting with point on the first space, C-<SPC> to set the mark, M-f to the end of string, M-b to the beginning of string, C-w to delete the spaces and then <SPC> to insert the missing space.

Here’s the new-and-improved-with-added-awesome technique: with point anywhere in the spaces, press M-<SPC> (just-one-space). That’s it. 5 commands down to 1. A 5x productivity increase. OOooh yeah.

delete-horizontal-space

just-one-space has a brother called delete-horizontal-space, mapped to M-\, which does exactly the same thing but removes all the spaces, leaving nothing but a trail of destruction in its path.

delete-blank-lines

One last one – it’s not as exciting as the others, but it could easily be the most useful. Imagine the following brilliant JavaScript code:

function awesome() {
    //do stuff
}






function great() {
    //do other stuff
}

The author of this code probably heard that whitespace was beneficial for readability, but they went too far. delete-blank-lines to the rescue! With point on any of those blank lines, press C-x C-o and all the blank lines go except for one. It’s flexible too. With point anywhere on the last line of awesome() (the }), press C-x C-o to delete all the blank lines.

There’s a cloud to every silver lining though: with this technique, I don’t get to go C-k C-k C-k C-k and shout “Kill Kill Kill Kill!” I think it’s worth the sacrifice though.

Further reading

GNU Emacs Manual – Deletion