All software developers spend a lot of time in their respective IDEs, and Delphi developers are no different. When designing an app’s user interface using the Form Designer, I frequently switch between mouse and keyboard operations. However, when working in the Code Editor, I rarely reach for the mouse. In fact, I’m old school and I still rely on the numeric key pad with NumLock off for cursor commands (more on that in a future post).
As a result, I appreciate editing techniques that help me edit my code faster without disrupting my workflow.
Column selection has some neat capabilities, even when not selecting multiple lines. For example, consider the following code:
begin Canvas.Font.Color := clBlue; Canvas.Font.Style := [ fsBold ]; Canvas.Font.Name := 'Tahoma'; Canvas.Font.Size := 14; Canvas.Brush.Color := clYellow; end;
Now suppose I want to prefix each statement with the name of a control (eg. Panel1). Instead of typing it in for each statement (or using a with statement), I can type Panel1 on the first statement, so the code looks like this:
begin Panel1.Canvas.Font.Color := clBlue; Canvas.Font.Style := [ fsBold ]; Canvas.Font.Name := 'Tahoma'; Canvas.Font.Size := 14; Canvas.Brush.Color := clYellow; end;
Then, I put the cursor on the “P” in Panel1 and then go into Column Selection mode (hold down the Alt key) to select “Panel1.” (including the dot). Notice that the selection extends 1 character beyond the cursor position, which is different than normal text selection. Next, copy the selected text to the clipboard (Ctrl+C or Ctrl+Ins).
Next move the cursor down to the beginning of the next line, to the left of the “C” in Canvas. The press the Paste command (Ctrl+V or Shift+Ins). The “Panel1.” text is pasted at the beginning of the statement as you would expect. BUT notice where the cursor is now! The cursor is positioned on the next line (3rd line in the example) to the left of the “C” in Canvas. All you need to do is to press the Paste command again and again to insert the text into the successive lines.
To change from column selection back to line selection (the default), press Ctrl+O+K.
This is very effective and one of my favorite tricks with column selection. Interestingly enough, this capability has been in Delphi since Delphi 1.