Programmatically create a keyboard shortcut to "Align Center" a Text Cell without losing cursor position
The key option is Scope -> SelectionCell
. So, you can either modify the menu item in MenuSetup.tr to:
MenuItem[
"Align Center",
TextAlignment -> Center,
Scope -> SelectionCell,
MenuKey["c", Modifiers -> {"Command", "Option"}]
]
or you can add a KeyEvent
to KeyEventTranslations.tr:
Item[
KeyEvent["c", Modifiers->{Command, Option}],
TextAlignment->Center,
Scope->SelectionCell
]
In both cases, I would add a modified version of the file to the appropriate place in $UserBaseDirectory
.
You may use the standard menu short-cuts. By pressing the following keys in sequence you will navigate the menu using the keyboard to perform the text centering.
Alt,r,a,c
In general, once you press Alt the items in the menu will have one character underlined. Press that character to select that menu item.
Update: I think for Mac you press Ctrl+F2 to enter the menu bar from the keyboard.
Hope this helps.
So it turns out all you need to do is use the token TextAlignment->Center
. Here's a way to do this taken directly from the menu itself:
Style[
"asdasdasd",
ContextMenu ->
Menu["Text Alignment", {LinkedItems[{MenuItem["Align Left",
TextAlignment -> Left, Scope -> SelectionCell],
MenuItem["Align at 25%", TextAlignment -> -0.5,
Scope -> SelectionCell],
MenuItem["Align Center", TextAlignment -> Center,
Scope -> SelectionCell],
MenuItem["Align at 75%", TextAlignment -> 0.5,
Scope -> SelectionCell],
MenuItem["Align Right", TextAlignment -> Right,
Scope -> SelectionCell],
MenuItem["On AlignmentMarker", TextAlignment -> AlignmentMarker,
Scope -> SelectionCell]}]}]
]
Right/Option click on that text:
then picking center:
Just to confirm that the MenuSetup
and KeyEventTranslations
file are effectively operating in the same way I added this to my events file:
Item[KeyEvent["Up", Modifiers -> {Shift}], TextAlignment -> Center]
and then selected a cell bracket, pressed Shift-Up and it centered the cell.
Presumably this will work for any style option.