'How can I enter a literal <TAB> character in IntelliJ/IDEA/PyCharm?

My configuration indents with four spaces, and I want to keep that. Occasionally (e.g. in a Makefile) I want to input a literal TAB character.

How can I force the IntelliJ-IDEs to input a tab or space, when it would not do so when I hit tab or space in that instance?



Solution 1:[1]

You seem to be asking two questions here:

1. How do I force IntelliJ IDE to input a tab, when it would not do so when I hit tab?

and

2. How do I force IntelliJ IDE to input a space, when it would not do so when I hit space?

I don't understand how the second case can arise. However, I have provided a solution to it as well.

Case 1. Insert a tab character when an IntelliJ IDE wants to replace it with spaces due to configuration

Solution

Use search and replace.

Details

  1. Place the cursor where you want the tab to be
  2. Press the X key
  3. Select the X you just typed
  4. From the main menu, choose Edit | Find | Replace to bring up the search and replace pane
  5. Make sure there is an X in the search field
  6. Enter \t in the replace field
  7. Be sure the option Regex is checked
  8. Be sure the option In Selection is checked
  9. Click the Replace button

Case 2. Insert a space character when an IntelliJ IDE won't just let you type one (???)

Solution

Use search and replace.

Details

  1. Place the cursor where you want the space to be
  2. Press the X key
  3. Select the X you just typed
  4. From the main menu, choose Edit | Find | Replace to bring up the search and replace pane
  5. Make sure there is an X in the search field
  6. Enter a single space into the replace field
  7. Be sure the option In Selection is checked
  8. Click the Replace button

Solution 2:[2]

Install the plugin for Makefile support: https://plugins.jetbrains.com/plugin/9333-makefile-support

When I tried it today, this automatically use hard tabs in the editors for Makefile files.

Solution 3:[3]

Open another text editor, type a tab, and then copy and paste into the PyCharm editor. In MacOSX this worked for me using both Sublime Text 2 and TextEdit.

Solution 4:[4]

I can't think of any "direct" way. Probably the easiest way that I can think of is to write a Live Template to do it. See the help page on Live Templates for more information. When you write it, you may need to copy and paste a tab character in from another application.

However, when I tried it, IDEA saw it as just empty text and would not save it. So I used a variable with the "capitalize" function to capitalize a tab character.

Screenshot of Live Template

Here's the template I created that you can paste into your templates. Now I type tab, hit the Tab key and I get a tab character. Of course you can change the abbreviation.

<template name="tab" value="$TAB$" description="Enter a tab Character" toReformat="false" toShortenFQNames="true">
  <variable name="TAB" expression="capitalize(&quot;&#9;&quot;)" defaultValue="&#9;" alwaysStopAt="false" />
  <context>
    <option name="OTHER" value="true" />
  </context>
</template>

You could extend the idea to have multiple ones that enter multiple tabs. For example tab to enter 1 tab, tab2 for 2 tabs, etc.

Screenshot of it after use: Screenshot of tabs used in code

There may also be a way to hack a macro to do it. You could then assign the macro to a keyboard shortcut. I'll see if I can figure something out and update this if I do.

Solution 5:[5]

Use the menu option: Edit -> Convert Indents -> To Tabs.

Whenever I edit a makefile I do the "to tabs" conversion before I save the file.

screenshot

Solution 6:[6]

UPDATE: Really sadly, I think the generated character still gets converted to spaces... Am checking...


There is an Action in JetBrains IDEs to enter a Tab character.

Here are the steps to use the Tab character action: (discovered in PyCharm 2020.3)

  1. Double-tap the Shift key OR Help -> Find Action...

enter image description here

  1. Type the 3 characters tab

  2. Click on the action that is called Tab and shows the icon for the Tab character...

enter image description here


It remembers the last action you did, so if you have several tabs to add, you can get into a quick process by doing ShiftShift then Return each time.

Solution 7:[7]

Answer based on @Morfic's comment to the question. I think this is the cleanest and most sensible reply here so I figured it deserves a place as an answer (and needless to say, it worked for me).

If they're different types of files you could configure File -> Settings -> Editor -> Code style -> Tabs and Indents for each one to use either space or tab depending on how you want it.

Solution 8:[8]

One way to do this is to copy a TAB character from another text editor, then right click in your PyCharm file and use Copy / Paste Special -> Paste as Plain Text (Ctrl+Alt+Maj+V)

I guess this was not available in older PyCharm versions, because no other answer proposed it. Regular paste (Ctrl+V) doesn't work (replaced by spaces), but this one works. The function will also bypass other automatic formatting.

Solution 9:[9]

Inspired of Clare's suggestion, assign the left tab key after finding 'tab' action.

At Actions tab, search by 'tab' and move your up/down arrow key and place there. Then type Ctrl+1, you will see a popup. Select as follows and click OK.

enter image description here

Then, you might be asked "Do you want to remove other assignments?" if Tab key was already assigned. Click Leave because your usage won't conflict with the existing setup.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arno Dverjging
Solution 2 Weiming
Solution 3 safay
Solution 4
Solution 5 codeape
Solution 6
Solution 7 Avision
Solution 8
Solution 9 Brendan Kim