DEV Community

Jason C. McDonald
Jason C. McDonald

Posted on

Type Literal Tabs Anywhere

I often work with tab-indented code, but there's one place that can be a royal pain: the web browser. You can't just press a field on GitHub and get a literal tab character. You move to the next field. That leaves you with two options: either indent with spaces or copy/paste your tab character manually. Blah.

I decided those were both annoying to me, so I decided to leverage the Compose key feature offered by Linux.

My guide will only work for Linux. Check out macos-compose to get compose key behavior on macOS, and wincompose for Windows. I've tested neither, but I can imagine you could pull off something similar to my approach there.

Step 1: Enable Compose Key

The Compose key is an awesome feature that allows you to type many common characters with a few keystrokes. You configure a Compose key on your computer — I usually use the right Alt key. Then, you tap the Compose key (don't hold it down), and tap the correct combination of keys for the desired character, one by one.

For example:

  • Compose n ~: ñ
  • Compose C O: ©
  • Compose t m: ™
  • Compose e ': é

To enable this snazzy feature on Gnome (the default for Ubuntu, Pop!_OS, and Fedora), go to Settings -> Keyboard -> Type Special Characters -> Compose Key, and select a key.

If you're using another desktop environment, it's pretty easy to find the instructions. Just search "enable compose key xcfe", "enable compose key mate", or whatever your desktop environment is.

Step 2: Add TAB to Compose combinations.

In your home directory, create a new file called .XCompose. (If it already exists, you can just add to it instead.) Here's mine:

include "%L"

<Multi_key> <backslash> <t>     : " "   # TAB
<Multi_key> <backslash> <T>     : " "   # TAB
Enter fullscreen mode Exit fullscreen mode

The include line just brings in all the default compose combinations from the operating system. Don't forget this, or else you'll override the tens of thousands of defaults!

The next two lines add the new combination: Compose \ t or Compose \ T will insert a literal tab character, which is what's in the double-quotes. (Everything after the # is just a comment.)

Save, close, and restart your computer.

To test this out, open up anything you can type in. Test that compose is still working by tapping your Compose key, followed by n and ~. You should get ñ.

Now test out the new combination: Compose, \, t should produce a literal tab: ().

Top comments (1)

Collapse
 
jimmont profile image
Jim Montgomery

\t rules, find it hard we don't all use it for in/outdenting--thanks Jason. I'll check out macos-compose.