5

I have read this post about auto formatting code, and it worked when formatting some JSON. It seems to format the entire file at once. This could be somewhat drastic, e.g., if the indent rules used by VS Code conflicts with a project, this would lead to a major version control diff when used on a large file. How could I auto-indent my current line?

In Emacs, the cursor can be anywhere on the line and pressing Ctrl+i will auto-indent the current line. I searched through the possible Keyboard Shortcuts and did not find one that would do this. I am looking something like "Indent Line" but to auto-indent.

Steven C. Howell
  • 16,902
  • 15
  • 72
  • 97
  • There is a default keybinding of: { "key": "ctrl+k ctrl+f", "command": "editor.action.formatSelection"...} which formats the currently selected lines. Not exactly what you're asking for but close as it should not format the entire file. – alexriedl May 31 '17 at 16:17
  • Thank you @alexriedl! I find this to be an okay workaround though not quite what I was hoping for. It is annoying to have to select the entire line. More importantly, this only formats a line with respect to itself (e.g., tested with a line of JSON and it added new-line characters but did not indent the line w/ respect to the previous line). So really one should highlight multiple lines, a bit more annoying. – Steven C. Howell May 31 '17 at 16:53
  • This is a very good question. I wonder if it is very difficult to create a command that would take care of selecting the current line and formating selection. I suppose this must not be difficult to implement. – Pedro Rolo Jun 01 '17 at 10:55

1 Answers1

2

You can simulate that by pressing ctrl + i + k + f. These are two separate shortcuts: ctrl + i will select the current line under cursor and ctrl + k + f will format the selection.

You can remap the editor.action.formatSelection command to something shorter if you want to save keystrokes.

Saravana
  • 37,852
  • 18
  • 100
  • 108
  • As I mentioned in response to @alexriedl's comment, I find this to be an okay workaround though not quite what I was hoping for. This only formats a line with respect to itself (e.g., tested with a line of JSON and it added new-line characters but did not indent the line w/ respect to the previous line). So it would need to select the previous line as well (repeatedly pressing "ctrl + i" will only select subsequent lines). – Steven C. Howell Jun 01 '17 at 14:36