ChatGPT解决这个技术问题 Extra ChatGPT

Visual Studio Code Tab Key does not insert a tab

I'm using Visual Studio Code as my code editor. I did a search on google but wasn't able to find anything about my issue.

The issue is simple, pressing ⇥ Tab in the editor does nothing. I'm expecting it to insert 4 spaces.

Anyone know what I can do to get ⇥ Tab working like expected?

I had a problem where tab would just tab outside of the editor (navigating between UI elements). Restarting the editor fixed it.
Not answering op questions, but for anyone else landing here after searching "visual studio tab not working" or having issue when hitting Tab in Visual Studio selects block instead of adding indentation. You can refer to this answer by Karel Tamayo
Remains an issue in Code v1.45.1 from June 2020. Solution is to delete the Toggle Tab Key Moves Focus keybinding of Ctrl + M (see the answer by @Addison). Strange 'feature'.
Thanks! Pressing ctrl+M fixed it for me. no need to restart
@AlainD The idea is probably to allow moving around in the editor without leaving the keyboard :)

A
Arad

I had accidentally enabled a different mode for the tab key. Fixed it by pressing Cmd+Shift+M (for Mac), or Ctrl+M (for Windows).

From the Visual Studio Code Keybinding docs:

| Key | Command | Command id | | Ctrl + M | Toggle Use of Tab Key for Setting Focus | editor.action.toggleTabFocusMode |

The current tab control mode should also show up in the status bar:

https://i.stack.imgur.com/RTMqF.png


Happened to me when I tried to use the code collapse (Ctrl + M, Ctrl + O). Thx.
What is Setting Focus ?
Theres a bright green "Tab Moves Focus" on the statusbar at the bottom if you have mistakenly pressed Ctrl+M.
@Jannunen you're right, sadly for us who have 25inch screens, nobody is gonna notice that teeny tiny thing in the corner...
What a truly peculiar setting. It's especially confusing because Ctrl-M is used to open up a development menu in the Android emulator (writing in React Native / Expo). I must have accidentally pressed it when VSCode was open and not the emulator. What a bewilderingly strange feature, though.
A
Addison

To fix the issue

Pressing ctrl+M causes the ⇥ Tab key to move focus instead of inserting a ⇥ Tab character. Turn it off by pressing the shortcut again.

To disable the shortcut

Open "Keyboard Shortcuts" with ctrl+K, then ctrl+S. Or go to File > Preferences > Keyboard Shortcuts. Search for toggle tab key moves focus. Right Click, Remove Keybinding.


Thank you for this! I use ctrl + m -> ctrl + k -> ctrl + 0 to collapse code and almost daily I accidentally break my tab functionally before this :)
I just installed MSVS 2019. I don't see Preferences under the File tab. ctrl-k, ctrl-s opens a list of snippets. I don't see a "toggle tab key moves focus" option. Which version of MSVS are you using?
@riderBill - This is for the latest VS Code 1.39.2 (2019-10-15). You are using Microsoft Visual Studio, which is not to be confused with Visual Studio Code.
Oh. <:^| My mistake. But not (entirely) my fault. I'll plead "Microsoft is an industry leader in poor choices for naming new products." As is Google. Ever try to search for help on Google Messages? [Sigh]
With no obvious indication as to what's happening, what an utterly stupid UI design. Thanks for the answer. :)
T
Tony L.

Click "Tab Moves Focus" at the bottom right in the status bar. That's it.

I believe I had clicked on ctrl+M. When doing this, the "Tab Moves Focus" tab/button showed up at the bottom right. Clicking on that makes it go away and starts working again.

https://i.stack.imgur.com/O0wxo.png


Saved me, I do not know how I enabled it but I installed a new keyboard and suddenly TAB was jumping into different windows.
Microsoft products have many rubbish built in. another famous is Multiple Desktop in Windows 10, or unable to Create Desktop Shortcut in Windows 10 . so when you try to add Shortcut for Skype App into Desktop, good luck
ctrl + M toggling solve the issue
i cant even....
J
JBallin

Click on the explorer or any other window that is not the editor then press Ctrl + Shift (for Mac only) + M, this is the command to "Toggle Tab Key Moves Focus" on the Keyboard Shortcuts.


Is there any way to disable this "feature" ?
@AlexanderRyanBaggett just remove the keybinding
L
Lucas Basquerotto

In, my case I followed this advice and changed this emmet setting in vscode to false:

"emmet.triggerExpansionOnTab": false

I was receinving the error Cannot read property 'value' of null. It solved the problem immediatelly.


r
rubimbura brian

Try CTR + M it will work like before.


f
flux9998

As of December 2018 on macOS Mojave 10.14.2 using VSCode 1.29.1 the default keybinding for 'Toggle Tab Key Moves Focus' is set to Command+Shift+M. If you got stuck with this, using that key combo should fix the issue.

Do Command+K Command+S to pull up the Hotkeys Settings and then search for Toggle Tab Key Moves Focus or editor.action.toggleTabFocusMode if you want to change the key combo.


On Windows it's just Ctrl+M by default to Toggle Tab Key Moves Focus. And Dan, that's a tad hyperbolic, considering the default in Visual Studio to format a document is actually Ctrl+K, Ctrl+D - visualstudioshortcuts.com/2017
D
Dharman

Had this problem. Simply press "Ctrl + M" (On Windows). Likely "CMD+M" on Mac.


S
Sagi

I am using code on xfce - did the following to fix the Tab key behavior:

File -> Preferences -> Settings

search for "keyboard.dispatch"

copy to right panel and change value from "code" to "keyCode"

Reload code


N
Nazim Kerimbekov

All the above failed for me. But I noticed shift + ⇥ Tab worked as expected (outdenting the line).

So I looked for the "Indent Line" shortcut (which was assigned to alt + ctrl + cmd + 0 ), assigned it to tab, and now I'm happy again.

Next morning edit...

I also use tab to accept snippet suggestions, so I've set the "when" of "Indent Line" to editorTextFocus && !editorReadonly && !inSnippetMode && !suggestWidgetVisible.


Perfect! Thank you for figuring out a functioning when condition.
r
riderBill

[Edit] This answer is for MSVS (the IDE, as opposed to VS Code). It seems Microsoft and Google go out of their way to choose confusing names for new products. I'll leave this answer here for now, while I (continue to) look for the equivalent stackoverflow question about MSVS. Let me know in the comments if you think I should delete it. Or better, point me to the MSVS version of this question.

I installed MSVS 2017 recently. None of the suggestions I've seen fixed the problem. The solution I figured out works for MSVS 2015 and 2017. Add a comment below if you find that it works for other versions.

https://i.stack.imgur.com/1fFou.png


S
Steven Delrue

Maybe another program is interfering? Closing Teamviewer fixed the problem for me.


Same for me!!! I never open a TeamViewer, and I accidentally left it open. To add more info: I had another instance of VS Code running, and in it the tab was working fine, while in the other tab was not working. After closing TeamViewer, tab works in both instances!
A
Arash

I've had this happen before as well, where there was TeamViewer client takes the control of the TAB key. You won't know this until you close the TV window that you have open in the background.


A
Archon 808

Make sure this is NOT checked :

[ ] Tools | Options | Text Editor | C/C++ | Formatting | Automatic Indentation On Tab

Let me know if this helped!


Is this related with Visual Studio? The question is asked for VS Code I guess.
N
Nazim Kerimbekov

For those of you not about that space bar life (- _ - )( - _ -)

Keybinding for ⇥ Tab isn't set to anything so you have to do it manually Navigate to Preferences/Environment/Keybindings and search for "tab" Click on Edit Binding at the bottom and press the tab key. Press "Apply" then "Ok" Key bound!


A
Alexandre Macedo

For some reason, after an update, my tab key was unbinded of 'tab' default behaviour. So I have to open File>Preferences>Keyboad Shortcuts, search for the simple "tab" line, and bind to the tab key again. Pretty awkward, but worked.


K
Kishorekumar Yakkala

https://i.stack.imgur.com/u0Fsu.png


M
Matt

Check your key mapping (File > Preferences > Key Mapping) [Ctrl+K Ctrl+S]. In the input line at the top write 'tab' and bind tab command with tab key.

It works for me. Some extension overwrite it.


h
hoogw

In 2022, above not working any more, Microsoft changed the key binding

On Mac, Cmd + shift + M no longer working

Instead you should use : ctrl + shift + M

https://i.stack.imgur.com/SwnGP.jpg


Ctrl+M still works for me on vscode 1.64.1 on Windows. What version of vscode are you on?
D
Devorein

None of the above worked for me. For me, the issue was that the file wasn't editable by VSCode. You'll see a small padlock on the file tab on top. Just close the tab and open the file again. Tab should be working as usual.


h
hxlnt

Not sure what operating system you're on, but there was a known issue with the tab key on one of the more recent releases of VS Code for Mac OS X. The bug has been fixed in the latest release (0.10.9).

On Mac OS X, you can check for the latest update by opening VS Code and then going to [Code > Check for Updates].

Sources and more information:

https://code.visualstudio.com/Updates


The latest update didn't do it for me - I pulled the March insiders release and that worked great.