Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to auto-indent only the current line on Visual Studio Code?

On Atom (and many other editors), there is the auto-indent command which allows us to auto-indent the line the cursor is on. Is there an equivalent in Visual Studio Code ?

I know there is the formatter action on Visual Studio Code but from what i have seen, it can be used only to :

  • format a selection (ctrl-K ctrl-F)
  • format the hole document (ctrl + shift + I)

I would like to be able to format the line the cursor is on without reformating the whole document and without having to make a selection.

Basically, i would like to configure the [TAB] key so that when i press [TAB], it auto-indents only the line the cursor is on :

  • if there is nothing written on the line, it just put the cursor at the right place so that when i start writting, the code is correctly indented.
  • if there is already something written on the line, it audo-indents the line

Is it possible ?

like image 623
Samuel Maisonneuve Avatar asked Oct 13 '17 08:10

Samuel Maisonneuve


People also ask

How do I indent left Visual Studio?

Use either of the two ways to indentation the code: Shift + Tab , Ctrl + k + f .

How do you indent an entire block of code?

You (these are the default settings I believe) can select a block of code and press the Tab key. This will indent the entire block. So for indenting a whole file: Ctrl + A , then Tab .


Video Answer


2 Answers

So I have skimmed through the source code and seems there is no setting currently available to make this happen. There is a lot of work happening in pipeline for indentation

https://github.com/Microsoft/vscode/issues/17868

VSCode use Monaco Editor under the hood

https://github.com/Microsoft/monaco-editor/issues/612

The current python configs are located in

https://github.com/Microsoft/vscode/tree/master/extensions/python

I tried, but understanding how all this integrates and works together just to fix one indent issue was just overwhelming. So I would just for the time being open a enhancement request with VScode and let the experts take a call and do the job

like image 118
Tarun Lalwani Avatar answered Sep 25 '22 21:09

Tarun Lalwani


Allow me to humbly suggest that you are looking for the solution in the wrong place.

I would suggest the following setting:

  "editor.formatOnType": true,

You have focused on "tab" doing the correct indentation. But with this setting you need not press the tab key at all. Just type the line with a normal return at the end. Visual Studio Code will then indent (and format) that line correctly.

like image 32
Klas Mellbourn Avatar answered Sep 24 '22 21:09

Klas Mellbourn