Such a mapping is quite hard to get right, including all corner cases.
Typically, you should try to use an "operator-pending" mapping to define the a$
part, so you can use it in da$
but also in commands such as ya$
, ca$
, etc. In fact if you define other custom commands that take a "motion", then a$
will be a valid one.
A naive solution using an operator-pending mapping and assuming the $...$
expression is in a single line (for inline math in TeX/LaTeX) would be:
onoremap <silent> a$ :<C-U>normal! F$v,<CR>
But this has some shortcomings, such as it only working from inside the $...$
block (not from the cursor being right at the first $
) or if you have two $...$
blocks on the same line this mapping will select the text between the two if your cursor is on that region.
You can get Vim to use more advanced logic to select the delimiters for such a region very precisely, but that probably involves writing a Vimscript function to handle all corner cases correctly. (You can also do that to handle more complex matches such as a math environment covering multiple lines.)
Fortunately, there are plug-ins which can help you with that!
If you want to define your own, then the vim-textobj-user plug-in can help you create new operator-pending mappings (and also visual mappings) based on patterns you define. This plug-in hides a lot of the complexity of these mappings in a way that avoids many of the pitfalls. Also, in many cases, using a simple regex to define the region to use as a text object is a much simpler approach.
If you're looking specifically into TeX/LaTeX, then consider the vimtex plug-in, which already defines an a$
text object for inline math structures. Also an ae
for LaTeX environments, which you might find useful as well.
noremap
variants