66

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!

Christopher Causer
  • 1,354
  • 1
  • 11
  • 13
  • if by chance it's for editing a `Makefile` then see https://stackoverflow.com/a/56060185/191246 – ccpizza Aug 21 '23 at 10:04

6 Answers6

86

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.

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • 4
    This doesn't work to me: pasted code gets auto-formatted and the tabs converted to a space. (VSCode 1.26.0) – lapo Feb 20 '19 at 15:54
  • @lapo, It sounds like you're trying to insert a tab character at a location where it is not allowed by your formatting rules. – Brent Bradburn Feb 20 '19 at 17:26
  • 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) – lapo Feb 21 '19 at 18:45
  • perfect! i can't believe that this is actually the easiest way to do it, but i guess it just is. how could they forget about makefiles? – Julius Naeumann Mar 03 '23 at 13:46
  • `echo -e "[\\t]"` Worker for me (MacOS) – Chiel Jun 26 '23 at 12:47
61

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.

Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
  • 4
    It's incredible how hard it is to find how to do this simple thing. – RBarryYoung Apr 16 '20 at 17:09
  • 12
    I suggest to use the shortcut `"key": "ctrl+k tab"` that is the default "prefix" for combined keys shortcut. – Carlos Aug 07 '20 at 11:21
  • 2
    `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! – Greg Fenton Mar 27 '21 at 15:45
  • 6
    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) – Ed Randall Nov 09 '21 at 13:38
  • `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` – huyz Aug 05 '23 at 08:13
34

<Alt> <Numpad: 0 0 9>

Still works great!

Shawn Lord
  • 355
  • 3
  • 4
2

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

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

Keivan
  • 1,300
  • 1
  • 16
  • 29
mpmansell
  • 21
  • 1
1

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.

-2

You can turn off editor.insertSpaces.

kitt
  • 110
  • 5
  • I think this actually works when combined with the answer from @World Python Developer (https://stackoverflow.com/a/72766544/1666926) – MrMas Sep 02 '22 at 18:48