(A comment up-front: I do not recommend pursuing the solution shown here. I think it's far, far better to switch in and out of inline math mode explicitly.)
Here's a LuaLaTeX-based solution. It can handle two types of cases:
math-mode macros that take one argument that must be (or at least should be) encased in curly braces, e.g., \vec{x} and \hat{y}); and
math-mode macros that do not take an argument, e.g., \alpha and \beta.
I don't think it makes sense to try to extend this setup to macros which take an argument that need not be encased by any kind of delimiter, e.g., \ln2, \ln 2, \sin \theta, or \sin\theta. There are just too many complications to contemplate. I don't think it's reasonable to expect users to write ln{2} or \sin{\theta}; after all, it would take just as many keystrokes to write $\ln2$ and $\sin\theta$ in the first place.
Do note that while this code can handle sentence fragments such as the letters \alpha\ and \beta, it's not meant to handle things like the expression \alpha+\beta is well defined. It also can't handle expressions such as a^2+b^2=c^2 properly; the spacing around the + and = symbols will not be the same as if the math material were were rendered math mode to begin with.
It's the user's job to populate the Lua tables called Table_A and Table_B with suitable math macros. Observe that it's necessary to double up on the backslash characters, i.e., one must provide inputs of the form \\vec and \\alpha. This is because the backslash character serves entirely different purposes in TeX and Lua. In particular, in order to get Lua to search for a single backslash character (\), it's necessary to input the character as \\.

% !TEX TS-program = lualatex \RequirePackage{filecontents} %% Store the Lua code in an external file called, say, "mymath.lua". \begin{filecontents*}{mymath.lua}
-- List of "functions" (macros, really) that should processed in math mode. -- 'Table_A' is for macros that take one argument enclosed in curly braces. Table_A = { "\\vec" , "\\abs" , "\\hat" , "\\widehat" , "\\dot" , "\\tilde" , "\\widetilde" , "\\bar" , "\\overline" } -- as many items as needed -- 'Table_B' is for macros that do not take an argument. Table_B = { "\\alpha" , "\\beta" , "\\omega" } -- as many items as needed -- The function 'mymath' does most of the work. function mymath ( s ) -- cycle over all items in the 2 Lua tables for i,j in ipairs ( Table_A ) do s = s:gsub ( Table_A[i] .. "%s-%b{}" , "\\ensuremath{%0}" ) end for k,l in ipairs ( Table_B ) do s = s:gsub ( Table_B[k] , "\\ensuremath{%0}" ) end return s end -- Assign the function 'mymath' to the 'process_input_buffer' callback. luatexbase.add_to_callback ( "process_input_buffer", mymath , "mymath" )
\end{filecontents*} \documentclass{article} \usepackage{mathtools} % loads the 'amsmath' package automatically \DeclarePairedDelimiter{\abs}{\lvert}{\rvert} % create a new math-mode macro % Load the Lua code from the external file: \directlua{ require ("mymath.lua") } \begin{document} \vec {x} or \abs{ -1} or \hat { y } or \widetilde { W}. The letters \alpha, \beta, etc through \omega. \bigskip %% Verifying that the same output results if $ symbols are used: $\vec {x}$ or $\abs{ -1}$ or $\hat { y }$ or $\widetilde { W}$. The letters $\alpha$, $\beta$, etc through $\omega$. \end{document}
\ensuremath{\alpha}+\ensuremath{\beta}versus$\alpha+\beta$?