Center The Text!
vim, 43 36 35 bytes
VGrx:sor
G:let &tw=col("$")
uu:%ce
Too good not to post. Note the trailing newline; it is significant.
Thanks to @Marth for saving a character!
vim-friendly format:
VGrx:sor<cr>G:let &tw=col("$")<cr>uu:%ce<cr>
Explanation:
VGrx replace every character with an "x"
:sor<cr> sort (since all chars are now same, sorts by line length)
G go to the very last line
:let &tw=col("$")<cr> set &tw to column number of last char on this line
"let &tw" is equivalent to "set tw"
tw is short for textwidth, used in :center
uu undo the sort, and the replacing-with-x too
:%ce<cr> center over entire file (%), using textwidth set earlier
Mathematica, 96 bytes
StringRiffle[#~StringPadLeft~Floor[Max@(l=StringLength)@a/2+l@#/2]&/@(a=#~StringSplit~"
"),"
"]&
Don't ask me how it worked, I just fiddled with it until it produced the correct output.
Pyth, 19 17 bytes
2 bytes thanks to Jakube
V.ztr+1.[l.T.zNd6
Demonstration
I think this is the first time the center-pad function of .[
has been useful. The length of the longest line is found using non-truncating transpose (.T
).
Trailing spaces are removed by adding a non-space character to the front, stripping spaces, then removing the added character.