Today’s Delphi-macro power tip: getting to the start of a line — for several different definitions of “start”.
This is most useful for macros that operate on a single line at a time, but it can sometimes be helpful in other sorts of macros as well.
The Fields to Properties macro recipe starts with Home, followed by Ctrl+Right arrow.
What “start of line” means will depend on what your macro does. Sometimes you just want to get your cursor to the beginning of the line. For this, plain old Home is what you want.
First alphanumeric character
Sometimes you want to get the cursor to the first non-blank character on the line. Unfortunately, I don’t know an easy way to do this in all cases.
But often, it’s good enough to get to the first alphanumeric character on the line. This worked well enough for the Fields to Properties macro recipe.
To do this, just do Home followed by Ctrl+Right arrow.
The downside is, if the line starts with punctuation — an open parenthesis, for example — Ctrl+Right arrow will skip right past it. This isn’t always what you want, and I don’t know of a clean and reliable way to get to the first non-whitespace character. But there are several things that can get you close.
Never underestimate the simplest thing. If, for every line you’ll be operating on, the interesting stuff will always start in column 5, don’t be afraid to do Home followed by pressing Right arrow four times. It sounds silly, but sometimes it’s just what you need.
Correcting after Ctrl+Right arrow
If you know that every line is going to start with an open parenthesis, followed immediately by an alphanumeric character, then simply do Home, Ctrl+Right arrow, and then correct for it with Left arrow to put the cursor before the parenthesis.
If you don’t know that every line will have that open parenthesis, then do whatever you think will get you closest most of the time — probably Home followed by Ctrl+Right arrow — but do it at the end of the macro, not the beginning. Then you can adjust the cursor position as necessary before you hit Ctrl+Shift+P.
Deleting leading whitespace
Okay, I do know of one way to put the cursor just before the first non-space character on the line, even if it’s punctuation. You don’t do it by moving the cursor — instead, you do it by moving the text, by deleting the leading whitespace! If you’re radically rewriting the line, sometimes you can get away with this.
If you know the line has leading whitespace, do Home followed by Ctrl+T.
I love Ctrl+T. It’s called “delete to end of word”, and it deletes everything from the current cursor position, through the end of the current word, plus any whitespace following it. It’s kind of like Ctrl+Shift+Right arrow followed by Delete, with one very interesting exception: unlike Ctrl+Right arrow, Ctrl+T stops at punctuation. So Ctrl+T, at the beginning of a line, will delete only the whitespace.
One catch is that, if you end up with a line that doesn’t have leading whitespace, you’ll delete the first word in the line! To avoid that, add whitespace first (yes, it’s cheesy, but it works): Home, Space bar, Home again (or Left arrow), then Ctrl+T.