Thursday, January 3, 2008

Delphi Tips and Tricks: Single Line Comments

A useful feature of the latest versions of Delphi and C++Builder is CTRL + /. This will place a single line comment, //, in front of every line that is selected or that the cursor is on. It will also remove the comment. Very quick and easy.


Anonymous said...

That's ctrl-' in my keyboard layout (Finnish).

Anonymous said...

Don't forget keymappings. I use the Visual Studio keymapping so that tab and shift-tab indent and outdent selected text. In this mode the auto comment key is Ctrl-K-C (which is kind of a pain.)

bprasetio said...

hmm, in Delphi, if I don't miss it, this feature exists since Delphi 2005, or maybe Delphi8?

thomas pfister said...

I thought this is included since the new IDE (> Delphi7) ?!
On the german keybord-layout I use STRG (CTRL)-# for commenting code-lines.

Daniele Teti said...

In my italian keyboard is Ctrl+ù

Fernando Madruga said...

Typing // is SO easy, especially if using a "full" layout keyboard, that is, one that has the +-/* keys on the keypad to the right, that having a shortcut for it is almost pointless.

If you want to do it to many lines, you can simply select them, then press the menu key and choose Toggle Comment from there.

This is just to say that, regardless of how cryptic that key combo is on some keyboards/mappings, it's not a big deal... BTW, that's CTRL+~ on my Portuguese keyboard which is simple enough but is also one more thing to memorize, so in practice, something I don't use!

Also, don't forget that you can use the open source GExperts to change IDE Menu shortcuts, although I could not find a way to change the edit popup menu shortcuts as well, but it may help on remapping other "cryptic" keys... You can even add a few buttons to your editing window, among which you'll find Comment Code and Uncomment Code which do exactly that using your pre-configured choice of commenting. And you can re-map the keys used for those two options as well, should you like to do it from the keyboard instead of using your mouse to click buttons...

Sylvain said...

In french: Ctrl+É ;)

Chris Bensen said...

I didn't even think of other keyboard mappings when posting this quick tip. I would have referred to it by name, but by my understanding it doesn't have an official name. I like the term "Auto Comment".

"Auto Comment" was introduced in Delphi 2005.

Loïs Bégué said...

GExperts provides the same functionality since years.

On my PC:
SHIFT+CTRL+"." => format selected text as comment
SHIFT+CTRL+"," => remove comment formating

You may redefine the shortcut as well.
You may define alternatives for the way GExperts will comment your code, using your own "syntax":
- comment separator like "(*" or "{"
- leading signs
and so on...

Post a Comment