It seem to me that for a task like writing homework, it would be complete overkill to try anything fancy or low-level. So I would suggest instead to go with something like this, which just applies in the current notebook and also just for the particular vector you use most commonly:
SetOptions[EvaluationNotebook[],
InputAutoReplacements -> {"aa" -> ToBoxes[
\!\(\*OverscriptBox[\(a\), \(\[RightVector]\)]\)]}]
Now if you type aa
followed by a space, it will immediately change to $\vec{a}$.
Also be aware that you should use such constructs primarily for typesetting. Don't try using things like $\vec{a}$ in the same way you would use variables, unless you know what you're doing. See this tutorialthis tutorial and search for issues related to subscripted symbols. The same warnings apply to vectors.
Edit: general vectors
The above is the fastest method to input specific vectors. In practice, all you have to do is type the symbol you want to create, and paste it in the ToBoxes
that wraps $\vec{a}$ in the above example. To add more individual characters, just add the analogous definitions, such as "bb" -> ToBoxes[...]
, to the list {...}
.
To do something similar for arbitrary vectors, you will need more keystrokes. First enter
SetOptions[EvaluationNotebook[],
InputAutoReplacements ->
{"vv" -> OverscriptBox["\[NegativeMediumSpace]\[SelectionPlaceholder]",
"\[RightVector]"]}]
Now you can type vv
followed by a space to get a placeholder into which you type the desired letter(s). Then you can press the arrow navigation keys or Ctrl-. repeatedly to get out of the placeholder when done.
The above use of InputAutoReplacements
adds a space under the vector symbol, which I then have to undo by adding \[NegativeMediumSpace]
. To avoid this, you could also do this:
SetOptions[EvaluationNotebook[],
InputAliases -> {"vv" ->
OverscriptBox["\[SelectionPlaceholder]", "\[RightVector]"]}]
With this method, you would have to type Escvv
Esc to get the vector template, so it takes one additional keystroke. But it doesn't require me to add the \[NegativeMediumSpace]
in the output.