Sunday, 22 July 2012

Delphi IDE productivity keystrokes

The other day I posted about a talk I gave at a User Group meeting that focused on Delphi IDE keystroke shortcuts (mostly code editor features and shortcuts). After processing my simple notes list and adding in comments and links to the documentation where appropriate I’ve come up with what we see in this post.

This represents the list of my favourite keyboard shortcuts (and related features), some of which have been available since Delphi1, some added in the most recent versions and many more added at various points in between. I hope you can find some useful additions to your Delphi coding arsenal in this collection. Note that the list is mostly keystrokes, but the mouse is mentioned here and there. I do find having a wealth of keystrokes available to call upon helps me be efficient in use of the Delphi IDE.

Without further ado, here’s the list:

Code Insight, mostly added in Delphi 3. Some of these can be enabled or disabled as you choose in the Code Insight page of the Tools, Options dialog:

Code Completion. The (resizable) popup list we often see automatically, but which can be forced up when required. Ctrl+Space
Code Parameters. The tooltip that shows the names and types of the parameters required in a parameter list, with the current parameter shown in bold. Again, this often comes up as required but can be forced up when the cursor is in a parameter list using this keystroke. Ctrl+Shift+Space
Code Templates (now called Live Templates). Very under-used mechanism for auto-entering a block of often-entered text. The IDE ships with a bunch of predefined Code Templates and you can define your own also (which is where the real benefit comes in). Originally the Code Template definition was quite trivial, but since the introduction of Live Templates in Delphi 2006, the definition is now in XML and a little cumbersome, but is covered in the documentation. Ctrl+J
Tooltip Help Insight. This pops up when you hover over a symbol giving you information on it. If that symbol has XML documentation comments (XMLDOC comments) defined for the item, or its parameters, these will be included in the help popup. XMLDOC comments can be readily entered in Delphi XE2 by the included product Documentation Insight Express. Ctrl+Shift+H
Tooltip Symbol Insight. This is enabled by default, but so is Tooltip Help Insight, which takes precedence. Disabling Help Insight allows this to work – it shows some declaration and type information for the symbol under the mouse cursor hover mouse over a symbol
Tooltip Expression Evaluation. In debug mode, hovering the mouse over an identifier or a selected expression will evaluate it. If the expression yields an object then the tooltip will allow you to drill down into its constituent members. hover mouse over a symbol or highlighted expression in debug mode
Class Completion. If a class has a method declaration, this will enter the implementation if pressed with the cursor within the class. If there is a method implementation this will enter a corresponding declaration. Given a minimal property declaration (e.g. property Foo: Integer;) this will declare a private backing variable and a setter that assigns its passed value to it and extend the property declaration accordingly. Ctrl+Shift+C
Block Completion. This typing saver finishes code blocks for you. Enter
Error Insight. This is the name for the squiggly red lines in the code editor indicating where a background compilation has detected errors in the source. The errors are also listed in the Structure window in recent Delphi versions. As we have all seen over many recent versions, after a few compilations, Error Insight leaves various perfectly valid and compilable expressions marked with red squigglies and most people get quite irked by it, essentially paying no attention because of its lack of reliability. The tip here is to go to the Code Insight options and turn Error Insight off.  
Code Browsing. When you hold the Ctrl key down and move your mouse around the code editor, any item whose declaration can be located turns into a hyperlink and the mouse cursor indicates clicking it will take you somewhere. In short, Ctrl+click will take you to a symbol’s declaration or definition. However that involves the mouse, so there is another keystroke that is less commonly mentioned for this. Alt+Up
Browsing History. Once you start code browsing, these keystrokes allow you to go back and forth in the browsing history. Alt+Left and Alt+Right

Method hopping, i.e. jumping from one method to another within a source file:

Scroll through methods Ctrl+Alt+mouse scroll wheel
Toggle class lock. Normally method hopping goes through all methods in a file. This toggles a lock on just hopping through the methods of just the current class. Ctrl+Q, L
Move to top of current method or to previous method (unless your graphics driver software takes the keystroke to orient your monitor display to the natural orientation) Ctrl+Alt+Up
Move to next method (unless your graphics driver software takes the keystroke to orient your monitor display upside down) Ctrl+Alt+Down
Move to first method in file Ctrl+Alt+Home
Move to last method in file Ctrl+Alt+End

Regions and code folding. Regions are great for hiding away code sections that you don’t want to see when scrolling around in your code. They are a special case of compiler directives that each adds in a new code folding area.

Collapse current foldable code block or region Ctrl+Shift+K, Ctrl+Shift+E
Expand current foldable code block or region Ctrl+Shift+K, Ctrl+Shift+U
Toggle code block collapsed state Ctrl+Shift+K, Ctrl+Shift+T
Collapse all regions Ctrl+Shift+K, Ctrl+Shift+R
Collapse all nested procedures Ctrl+Shift+K, Ctrl+Shift+P
Collapse all methods Ctrl+Shift+K, Ctrl+Shift+M
Collapse all classes Ctrl+Shift+K, Ctrl+Shift+C
Expand all code Ctrl+Shift+K, Ctrl+Shift+A
Toggle code folding support Ctrl+Shift+K, Ctrl+Shift+O

Miscellaneous editor keystrokes:

Drop (or remove) bookmark – seems to be a bug that one keystroke option doesn’t handle bookmark 0 Ctrl+Shift+1-9 or Ctrl+K, 0-9
Go to bookmark Ctrl+0-9 or Ctrl+Q, 0-9
Module navigation (a name which appears to have been dropped from the documentation over recent releases). The keystrokes take you from a method declaration to a method implementation or vice versa (either keystroke does both jobs) Ctrl+Shift+Up or Ctrl+Shift+Down
Code formatting Ctrl+D
Indent marked block Tab or Ctrl+Shift+I or Ctrl+K, I
Unindent marked block Shift+Tab or Ctrl+Shift+U or Ctrl+K, U
Insert new GUID Ctrl+Shift+G
Go to line number Alt+G
SyncEdit. This is an awesome mechanism for intelligently updating an identifier name in a marked block. Also available via an icon in the editor gutter that looks like 2 pencils joined together. Ctrl+Shift+J
Open unit under cursor. Handy for looking at the VCL source when in a uses clause. Ctrl+Enter
Find references Ctrl+Shift+Enter
Insert compiler options at top of current file Ctrl+O, O
Delimiter match – pretty redundant in recent versions as the highlighting does this for you Alt+[ or Alt+]
Toggle case of marked block. For those of us who aren’t touch typists it’s very common to accidentally type quite a bit of text while Caps Lock is on. When you notice this just mark the block and use this keystroke. Ctrl+O, U
Delete word to left of cursor Ctrl+BackSpace
Delete word to right of cursor Ctrl+T
Delete current line Ctrl+Y
Start and stop recording keystroke macro Ctrl+Shift+R
Play back keystroke macro Ctrl+Shift+P
Comment current line or marked block Ctrl+/
Show list of open files Ctrl+Alt+F12
Incremental search. General search is no longer modal in recent IDEs Ctrl+E
Add to do item. I find this keystroke often gets mis-interpreted for some unknown reason, so I have to resort to a right-click in the editor followed by pressing E. To do list items (and the to do list itself) can be a handy way of keeping on top of notes regarding code you are building up. Ctrl+Shift+T
Move marked block mouse drag
Copy marked block Ctrl+mouse drag
Set focus to Tool Palette Ctrl+Alt+P
IDE Insight. The mother of all searching keystrokes – offers an incremental search through a whole bunch of parts of the IDE. Ctrl+. or F6

Form designer:

Select selected control’s parent Esc
Move selected component(s) 1 grid unit Ctrl+Shift+cursors or mouse drag
Move selected component(s) 1 pixel Ctrl+cursors or Alt+mouse drag
Resize selected component(s) by 1 pixel Shift+cursors
Lasso controls on a container (e.g. a TPanel) Ctrl+mouse drag

Object Inspector. Note that it’s quite possible to set a given property on a bunch of neighbouring components with very little mouse use. Select the first of the components (let’s say a bunch of buttons), click the target property (say the Caption property) and then click the first component on the form designer. If you simply type you will enter a value for the selected button’s Caption. Pressing Enter after having entered the value should take you back to the button on the form designer. You can use the cursor keys to then select the next button whose caption you want to set, then you can rinse and repeat as required. Additionally you can select the initial property by selecting the Object Inspector (F11), selecting the property name column (Tab) and then start typing the property name – it uses an incremental search. Tab then switches back to the property value column.

Select Object Inspector or go back to form designer or code editor F11
Switch columns Tab
Drop down component list Ctrl+Down
Switch between Properties and Events tabs Ctrl+Tab or Ctrl+Shift+Tab

5 comments:

  1. Thanks for the list. The most unproductive bug of the Object Inspector in RAD studio is that, unlike Delphi 7, you have to click twice the object inspector in order to key in a string in a string property...

    ReplyDelete
  2. Another nice one is that you can use the Column select and Ctrl-K U to line strings up... in nice column format... very useful for readability...

    ReplyDelete
  3. Thanks for list sir!

    ReplyDelete