Exit Full View

Tab and Shift+Tab

I won't re-ignite the tabs vs spaces flame war here, but I do want to comment about what should happen when you press Tab and then Shift+Tab.

IMHO, you should always get back to the original state. The only text editors that have this property (out of the box) are those that I've written.

Regardless of whether you use tab characters or spaces to indent code, I hope we agree on a few things :

  • Tab characters should never appear in the middle of a line.
  • When you hit Tab, you want to indent the selected block of code.
  • When you hit Shift+Tab you want to un-indent the selected block of code.

So what should happen when you press tab with no selection?

Personally, I think it should still indent the current line of code (regardless of the caret's column position). Every text editor (not written by me) disagrees! They think that "Tab" no longer means "indent", and instead means "insert N space (or 1 tab character) at the caret position".

Oddly enough, Shift-Tab always means "un-indent", which is why Tab followed by Shift-Tab doesn't get you back to the starting point. (An insert followed by an un-indent always ends up with a mess).

IntelliJ

By default the Tab key is assigned to the Tab action, but there is another action called Indent Line or Selection. IMHO, this action makes more sense.

To change it...

  • File->Settings...
  • Keymap
  • Find Indent Line or Selection
  • Right Click
  • Add keyboard shortcut
  • Click the + icon
  • Set Tab
  • Ok

It then gives a warning that Tab is assigned to other actions.Warning : Do NOT pick Remove, it's better to use Leave, and then manually remove the Tab shortcut. This is because the tab key is used in lots of contexts, and you want to keep those!

Mid-Line Tab Characters

I'm on team "spaces", but I understand (and agree with) the logic of team "tabs".

Using tab characters let's each viewer choose there own indentation size. Whereas using spaces imposes a particular indentation size onto all viewers.

IMHO, that's a bloody good argument for using tab-characters.

But, if you have a tab-character mid-line, then you are also imposing an indentation size (otherwise things won't line up the way you intended).

I had a colleague who did this. He would spend AGES lining up his code so :

  1. a = [ "Hello", "World" ]
  2. longer = [ "Welcome", "Everybody" ]

He used 8 columns per tab (which is why "]" is all the way to the right).

He was very proud of his neat code.

He didn't care that literally everybody else chose an indentation size of 4, and his code was the ugliest code we'd ever seen! (regardless of our tab/spaces preference). Nothing but the first non-white-space character ever lined up as he intended. <sigh>

This is how his code appeared to everybody else ( with a tab-size of 4 ):

  1. a = [ "Hello", "World" ]
  2. longer = [ "Welcome", "Everybody" ]

It annoyed me. Not because his code was ugly, but because he spent so much time making it ugly (for everybody but him)!

If memory serves me correctly, I think he also mixed and matched tabs and spaces, because he wanted an indentation size of 4, but kept to the "standard" tab size of 8. So indentation levels 1, 3, 5... needed 4 spaces after the tabs. Ahhh! Madness.