79

When using VSCode, most of my files are set to be indented using spaces. However I sometimes wish to insert a literal tab. When I was using vim I'd use <Ctrl>+v <Tab> but that doesn't work with VSCode.

I've been searching and searching and cannot find anything. Please help!

1

7 Answers 7

102

Quick-and-dirty solution: Find a tab somewhere else, then copy-paste.

Chances are that you already have a tab character in the file you are editing, but if not you can generate one in another application or text editor.

You can also generate a tab programmatically in a bash shell with the following command (the brackets are optional):

echo -e [\\t]

For your more immediate needs, I have inserted a tab character below...

    There is a tab character between these brackets: [	]

Another approach is to change the tab mode temporarily, as shown here.

Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't work to me: pasted code gets auto-formatted and the tabs converted to a space. (VSCode 1.26.0)
@lapo, It sounds like you're trying to insert a tab character at a location where it is not allowed by your formatting rules.
Yes @nobar unfortunately VSCode has no way of supporting "mixed tabs/spaces" (as defined by Java and GNU standard indentation) and I was looking for a way to achieve that manually. (so far, I'm using 8-spaces and then find/replace… a bit messy)
echo -e "[\\t]" Worker for me (MacOS)
This works for me, I have to "intentionally" introduce a tab in a file.
78

I'm not sure if there is a generic solution, but you can setup a keybinding for this:

{
    "key": "ctrl+v tab",
    "command": "type",
    "args": { "text": "\t" },
    "when": "editorTextFocus"
}

This keybinding will insert an tab character even when the current mode is spaces.

5 Comments

I suggest to use the shortcut "key": "ctrl+k tab" that is the default "prefix" for combined keys shortcut.
ctrl+v tab is inline with VI/VIM. ctrl+q tab is inline with Emacs keybindings. That there is a suggested standard for VSCode keybindings is good to know too!
In case anyone else is looking: Add this to the keybindings.json file, which is accessible via the Command Palette (⇧⌘P) Preferences: Open Keyboard Shortcuts (JSON)
ctrl+q tab works in vim, so might consider using that key chord in vscode keybindings so as to keep the default vscode behavior for ctrl+v
note this breaks the paste behavior since ctrl+v doesn't work anymore
47

<Alt> <Numpad: 0 0 9>

Still works great!

6 Comments

But this fancy-pants mechanical keyboard I bought has no numpad! :(
@tony19 That's because it's an alt code. It's windows specific.
For Linux Ctrl-Shift-u 0-0-9-space-<ENTER>
@Chris you can just enter the numbers any way your keyboard supports -- it doesn't have to be entered on the numpad. :-)
@MrMas As far as I can recall, a number pad was always required for this. Indeed, the "number strip" on SteelSeries Apex Pro does not work for this. YMMV
|
3

When working on Makefiles, I use Find/Replace using regexes.

Replace ^ (4 spaces - change as appropriate) with \t.

Comments

2

Strange, Visual Studio Code gives you a clue, but you have to hunt for it.

Turn off "Editor: Detect Indentation" which is ON by default.

After which, it will not assume that a tab is 4 spaces.

Of course, all you have to do is select your spaces, and then you will see "ghost dots" in your selection. If you delete those spaces and then type tab, you will now see 1 "ghost right arrow" when you select it which means now you really have a tab character.

1 Comment

Thanks! I had configured my editor to insert tabs whenever I press tab and I didn't even think that VS Code might have a feature that automatically overrides without a warning both what my tab key does and how wide a tab is rendered.
1

All of the previous solutions presented here require one of the following fairly brute-force solutions: a global RegEx replacement of space sequences, a copy-'n-paste of a hard-coded Tab character, or toggling from space- to tab-indentation and (hopefully remembering to change) back again once you're done.

While these are all, obviously, valid solutions, in my case I just need to insert the occasional single Tab character without jumping through hoops or Googling for a \t on the Internet (or, at least, through Stack Overflow).

For my solution, I have remembered that you can override the VSCode and VSCodium user key-bindings to allow the insertion of a hard-coded Tab character.

As a bit of background, the vast majority of my code is space character indented, but every now-and-then I will write a "Here Document" in a Shell script, and then I would like the "EOF" string to be indented to the same level as the text. For this to work, I need to manually insert a one-off \t character, not spaces.

Here is my solution:

Go to "Settings" → "Keyboard Shortcuts" → "Open Keyboard Shortcuts (JSON)" → Click on the right panel, which should be "User > keybindings.json".

Insert (or update) the following JSON:

[
    {
        "key": "ctrl+v tab",
        "command": "type",
        "args": { "text": "\t" },
        "when": "editorTextFocus"
    },
    {   "key": "ctrl+i",
        "command": "type",
        "args": { "text": "\t" },
        "when": "editorTextFocus"
    },
]

This allows you to use either Ctrl-V followed with a Tab or Ctrl-I to insert the Tab character.

Comments

-2

You can turn off editor.insertSpaces.

1 Comment

I think this actually works when combined with the answer from @World Python Developer (stackoverflow.com/a/72766544/1666926)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.