How can I set the tab width in a monaco editor instance?

I haven't been able to figure out how to set a global tabSize option either, however I did manage to set the option specifically for HTML:

editor.languages.html.htmlDefaults.setOptions({ tabSize: 2 });


The answer has just been discussed in a corresponding GitHub issue. The trick is not to update the options on the editor directly, but on the underlying model. To extend the snipped above:

const editor = monaco.editor.create(
  document.getElementById("editor"), {
    language: "html",
    value: "<p>Hello World!</p>",
});

editor.getModel().updateOptions({ tabSize: 2 })

This works for me (™) in the Monaco Playground.

All credit for this goes to the monaco devs — I absolutely love their editor, and this improves it even further.