Sublime Forum

Automatically centering text

#1

Hello,

I frequently insert comment blocks to mark sections into my latex documents. It looks something like this:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                SECTION NAME                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Its always 80 characters in length and I currently just copy paste them. I thought about making it a snippet, but I would like the text to always be as centered as possible. In other words, if the text I want to place in there is 13 characters long, I want 33 spaces on one side, and 34 spaces on the other (80-13=67, 67/2=33.5). Can snippets do this? Do I need to write some kind of function? Or a full flegded plugin?

Thanks!

0 Likes

#2

Don’t think it’s possible to do math in regexp, but often people amaze me with what can be done in regexp.

Writing a plugin to do what you want is pretty easy, a good exercise if you never done that before.
Ask some specific question if you need help.
Good luck.

0 Likes

#3

[quote=“bizoo”]Don’t think it’s possible to do math in regexp, but often people amaze me with what can be done in regexp.

Writing a plugin to do what you want is pretty easy, a good exercise if you never done that before.
Ask some specific question if you need help.
Good luck.[/quote]

In sed, you’d do something like this:

sed  -e :a -e 's/^.\{1,78\}$/ &/;ta' -e 's/\( *\)\1/\1/'

Not sure how well this translates to sublime…

0 Likes