On Sublime 3 there's this command called Paste and Indent
and the keyboard command for that is Super + shift + V
where Super
is the main command key for your OS. This seems to do the trick.
I found it here: https://gist.github.com/eteanga/1736542
Edit: Original poster adding further information, so people don't have to try to parse the comments.
Going back to the second step of the original problem:
int firstVar, secondVar; int myVar = 0; int result = someThing(myVar); if (result == GOOD_RESULT) { firstVar = 4; secondVar = 11; } else { }
I select the two lines between the first set of braces (the assignments to firstVar
and secondVar
, copy them (Ctrl+C), then add an empty line between the second set of braces and place my cursor at the beginning of the blank line (indicated by the |
, below):
int firstVar, secondVar; int myVar = 0; int result = someThing(myVar); if (result == GOOD_RESULT) { firstVar = 4; secondVar = 11; } else { | }
Then I 'Paste and Indent' (Ctrl+Shift+V), and the code is pasted with the default indentation for the file type, ready for me to change. (Also, if I'd pasted more lines, with different indent levels, they would all be adjusted accordingly.)
int firstVar, secondVar; int myVar = 0; int result = someThing(myVar); if (result == GOOD_RESULT) { firstVar = 4; secondVar = 11; } else { firstVar = 4; secondVar = 11; | }
Unfortunately, it's also left an extra blank line (because if I hadn't put the extra line, it wouldn't have indented at all), but that's easy enough to delete.
(So, sadly, it's still not as good as having the multi-line indentation work correctly, since I have to first create and then destroy a blank line. Just two extra key presses, you say? Sure, but it's two key presses I wouldn't need to do if multi-line indentation worked correctly, and it interrupts my natural flow, so I have to think about that rather than about the code changes I'm trying to make.
(Alternatively, I can de-select the newline character at the end of the last line, by pressing ← while continuing to hold down Shift, and then I only have to create a new line, but not delete one after pasting. But that's an even less natural thing to do or think about than adding and deleting and extra blank line, and it still involves two extra key presses.
(So either way, this feature is a workaround, not a fix, even if it is an interesting, even cool, feature.)
However, in situations where you already have lines of text between your braces (and you don't need to paste the new code just before the last brace), it works quite well: you don't have to add a blank line, just position your cursor at the beginning of the line where you want to insert the new code and press Ctrl+Shift+V. The lines will be inserted with indentation that matches its surroundings.
Also, you don't necessarily have to use the default indenting. If you place your cursor on some arbitrary column (with only whitespace to the left of it), the 'Paste and Indent' command will indent everything you paste to that column. (If you do this on a line with text to the right of the cursor, that line's indenting will be changed to match that of whatever you pasted.)
As I say, an interesting and fairly cool feature, worth playing with and getting to know, for sure. And worth being The Accepted Answer for this question. But still not quite as good as just having made the multi-line indentation work properly. : )