3
votes

I have been using Emacs for a very long time and now I want to switch to something modern like VS code. One thing that I really used to in Emacs is auto indentation with a Tab key:

http://www.gnu.org/software/emacs/manual/html_node/emacs/Indentation.html

Turned on with tab-always-indent

I want VS code not to add a tab symbol when Tab is pressed, but auto indent a single line like Emacs does, how do I configure that?

Here is an example in Javascript:

function foo(){
    }  // Press Tab here

In Emacs the result will be (and I want the same in VS code):

function foo(){
}

so the bracket will go to the beginning of line, but the result in VS code will be

function foo(){
        } // second tab added

Emacs does indentation according to the current text mode (Python/JS/C++/Whatever).

PS. I know I can select a region of code and use "Format Selection" or do auto-formatting on save.

1
Alex, for which mode you want such behaviour? Most modes I'm using now: go, python, javascript - I use autoformat on save (for go - it's unswitchable, python - yapf, javascript - eslint). I suppose that you mean some of the "text" modes. Which?Alex Yu
@Ingaz Updated my questionAlex Paramonov
I suppose you can play with code.visualstudio.com/docs/getstarted/… I never tried (yet) but it looks promising - it allows custom bindings for language ids. Thank for the question!Alex Yu
Oh, no! Tried myself - it makes Tab unusable.Alex Yu

1 Answers

1
votes

This only partly answers your question --- making Tab to indent-line --- it doesn't normalize indentation as you want. But, you could set it to some other command which may do what you want.

Plus it but breaks a few cases.

{
    "key": "tab",
    "command": "editor.action.indentLines",
    "when": "editorTextFocus && !suggestWidgetVisible && !inSnippetMode"
}

Where it yields to default behavior:

  • when using Tab to confirm a intellisense dialog (autocomplete)
  • when jumping to snippet placeholders

What it breaks:

  • pressing Tab to indent to the level of current block
  • expanding snippet cannot be done right after writing the snippet prefix (you need to wait for the intellisense to open)
  • maybe something else...