Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align indent to parenthesis after linebreak in Sublime Text

I like to keep my lines below 80 columns, so I often want to refactor a line that looks like this:

object.function(a_long_argument, another_long_argument, and_a_third)

to this:

object.function(a_long_argument,
                another_long_argument,
                and_a_third)

But when I press Enter after the first "," in Sublime it just linebreaks and indents the cursor a few spaces. I want it to align to the paranthesis or [] or {} that I am in, like Emacs does so beautifully.

Is there an option for this? Is there a plugin for this? Do I have to write my own?

I have tried searching for it, but I have not found anything.

EDIT:

Even better would be a shortcut or plugin or something for selecting a few rows, or the entire buffer, and let it try to auto-linebreak at good spots. Refactor comments too. If it has to be language specific, I want it primarily for Python and C++.

like image 402
Gurgeh Avatar asked Sep 12 '12 09:09

Gurgeh


People also ask

How do I fix indentations in Sublime Text?

That's quite simple in Sublime. Just Ctrl+Shift+P (or Command+Shift+P on MacOS) to open the tools pallet, type reindent , and pick Indentation: Reindent Lines . It should reindent all the file you are in, just remember to save before running the command, or it may not appear.

How do I indent multiple lines in Sublime Text?

You can use ctrl+ ] to indent a line (or highlighted block), and ctrl + [ to unindent. On OSX this is cmd + ]/[ . at least on the mac version tab & shift-tab work on whole lines and the position of the cursor has no influence on it.

How do I change the default indentation in Sublime Text 3?

Changing default indentation settings The following File Type Preferences determine the indentation settings: translateTabsToSpaces: set to either 'true' or 'false'. If true, then when tab is pressed, an equivalent number of spaces will be inserted into the buffer instead. Defaults to false.

How do I align codes in Sublime Text 4?

Simple: Select the lines you wish to align. Press Ctrl+Alt+A (Windows & Linux) or Command+Ctrl+A (Mac OS X)


1 Answers

Sublime's indent_to_bracket will wrap the cursor for you. Just add the following line to either your User/Preferences.sublime-settings or User/Python.sublime-settings file:

"indent_to_bracket": true 

Unfortunately this currently only seems to work with parentheses, curly braces and square brackets still wrap to the previous line indent.

like image 140
joemaller Avatar answered Oct 17 '22 17:10

joemaller