Sublime Forum

Selecting all text of a given indentation level

#1

Hey guys,

Say you have indented text (prose) with several levels of indentation (say 5 levels).
Is there any quick way to select only the text of a given indentation level (say all level 3 text only, or at the very least level 3 and all children (i.e 3, 4 and 5) in this example) to copy and paste somewhere else ?

I tried to use code folding, but without success.

Thank you so much !

0 Likes

#2

you could execute something like the following in ST’s Python console:

view.sel().clear(); view.sel().add_all(line for line in view.lines(sublime.Region(0, view.size())) if view.indentation_level(line.begin()) >= 3)

(or turn it into a plugin and create a keybinding for it)

or just do a regex search for

^([ ]{4}){3}.*$

(assuming you have 4 spaces indentation)

2 Likes

#3

If you are using windows, you can use Ctrl+Shift+J to select the text at same indentation level.

1 Like

#4

Ok guys, I didn’t know about regex, but you inspired me to watch a few tutorials and study a cheat sheet. Regex’s will be from now on a very welcome tool in my arsenal !! I already wonder how I could live without them !

So for those who would have a the same problem as me, I watched this


and studied this
https://github.com/dmikalova/sublime-cheat-sheets/blob/master/cheat-sheets/Regular%20Expressions.cheatsheet

and in the end, this regex did the trick for me
(?<=^\t{2})\w.+
and change the 2 for whatever number of tabs you have as indent

That means

  • look for a starting line ^ followed by 2 tab characters \t{2}
  • the fact that this group ^\t{2} is in this operator (?<= …) means that if it precedes what is described after, then there is a match (excluding the group that was used for the condition)
  • and after, I can have a non-blank character \w followed by any character . one or more +

hope this helps those with the same problem !

thanks guys
cheers

3 Likes