Is it possible to auto-indent only the current line on Visual Studio Code? - 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 ?

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

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.

If you install the extension emacs-tab, you can do this:
{
"key": "tab",
"command": "emacs-tab.reindentCurrentLine",
"when": "editorTextFocus"
}
Which, as far as I understand you, does exactly what you want (and doesn't format the line in other ways, such as breaking it if it is too long, and so on).

This extension worked for me, and allows typing Tab with the cursor mid-line to get proper indentation similar to what I was used to on Emacs.
Note that it basically does the same as the extension recommended in this answer but at the time of this writing that extension appears to be unmaintained and has some open issues.

Related

Copy/move rest of the current line to the end of next line

I have Googled a lot but cannot find one feature in VScode that I would appreciate greatly:
Copy (or move) rest of the current line (i.e. not the entire line) to the end of next line
Example:
What I have - note the cursor
What I need - again note the cursor
So ideally, with a single keyboard shortcut, I would copy (or move) the remainder of the current line (= "ABC1" in the example) to the end of the next line. Any ideas how to do that? Is it available within VScode, or is there a relevant extension? Thanks!
Since you're asking if there's an extension for it, the vim extensions should at least be mentioned. I have no intention or desire to start an(other) editor war, though.
What you're asking would be trivial in vim (y$, yank to end of line) and there are at least two very popular vim extensions for vscode that will bring the vim keybindings as well as the power of configurable commands and bindings in vim to vscode.
Yes, it would require some effort to learn the "vim way" of doing things, but the upside is that you'll probably never have to re-learn the bindings if you move between editors/IDEs since they are available in pretty much every editor and they make editing very fast and precise.
Drag and drop by mouse
Why are you not using mouse drag and drop text?
Select the text and drag it by mouse where you need
Otherwise, if you have multiple times then you can solve it in two steps.
Step1:
Replace all Name by Name(ctrl+enter)SecretCode
Step2:
Replace all "(ctrl+enter)SecretCode by "
Done

Sublime fails to open new_file in macro

In Sublime (Version 3.2.2, Build 3211) (Ubuntu 18.04.5), I'd like to record a macro where I select-all, copy and paste the current content into another, unsaved file. And then do other commands as well, but I'm not even getting that far. The new-file step doesn't seem to work – neither new, nor new_window, newWindow, new_file, newFile seem to work; not even reopen_last_file. It should be new_file, though.
The console throws this:
Unknown macro command new_file
Which is weird. The command new_file works fine in other contexts: looking at the key-bindings settings, ctrl+n is bound to the same command and the hotkey has no issues.
Selecting all, copying – even this following bit works as intended:
{
"command": "insert",
"args": {"characters": "Hello, Friend!\n"}
}
In their forums, user "jps" writes:
With regards to the other console messages (unknown command, etc), don’t worry about them, they’re supposed to be there :slight_smile:
… but this doesn't seem to be right if it's obviously not working.
I have a project open but closing the project has no effect.
Is there maybe a package or something missing?
Macros in sublime can only capture and replay TextCommand commands; that is, commands that directly modify the content of a buffer or it's visual state. Examples including adding or removing text, changing the selection, moving the cursor around or changing view specific settings (like toggling word wrap or changing rulers, etc).
Commands like new_file, new_window or opening and closing projects are WindowCommand commands, which can't be captured via a macro.
In order to run a sequence of commands that includes a WindowCommand (or ApplicationCommand), you need to use a package. Two examples of that which are readily available on Package Control are Chain of Command and Multicommand.

Autoformat in VS Code on semicolon, enter, etc

Is there a way to make VS Code work with autoformat like other VS versions ?
Mostly I want it to autoformat the line/block when I close it( so with ; } ).
Also I would like to autotab in advance when I make empty lines or similar empty blocks while the default behaviour is to just stay at starting tab.
I program mainly in C#.

VSCode macros extension wrong sequence

I'm currently trying to get a shortcut which opens the default and user keybindings in json side by side (pretty much the same what VSCode already provides for the settings.json). Since VSCode does not support that in itself, I tried to do it with the macros extension.
My code looks the following:
"macros": {
"openKeybindings": [
"workbench.action.openDefaultKeybindingsFile",
"workbench.action.openGlobalKeybindingsFile",
"workbench.action.moveEditorToNextGroup",
],
},
I can now trigger this macro with:
{ "key": "ctrl+alt+k", "command": "macros.openKeybindings" },
What I am thinking this macro should be executing is:
Open the default keybindins.json
Open the user keybindins.json
Move the user keybindins.json to a new editor window to the right so they are side by side
My problem now is, the macros does not seem to execute these three commands in sequence. What actually happens if I press ctrl+alt+k is that the default and user keybindings.json get opened in the new editor window to the right.
Can anyone explain to me why the macro does not execute the commands in order and maybe give me a solution to my problem?
Fixed that problem with this issue on the github of this macro

VS Code: "go to next problem" in current file rather than in any file?

I starting to use VS Code with Purescript and Haskell. The VS Code function "Go to next problem" is very handy, except that it goes to any problem (including warnings) in any file. I would like it to go to the next problem in this file.
Alternatively, is there a way to see warnings or errors without needing to use the mouse to hover over them? "Go to next problem" does that with a peek window, but otherwise it's annoying to have to take my hands off the keyboard.
I'm using Code Insiders.
In Code you can use: (type in ctrl + shift + p console)
Go to next problem(Error, Warning, Info).
This loops over the errors in current file, never changes file.
When you run f8, this starts other command
Go to next problem in Files (Error, Warning, Info)
Just add some shortcut to first one
Alt+F8 is difficult for me so I use a custom binding. The command name is the rather cryptic
editor.action.marker.next
mine is assigned like so:
{
"key": "ctrl+numpad1",
"command": "editor.action.marker.next",
"when": "editorFocus"
},