The following operator-depending mapping works great for me,

onoremap <silent> a$ :<C-U>normal! hEF$v,<CR>

except when it is issued at the very beginning of a line. What happens is that the h raises an error despite the preceding <silent> and the whole mapping gets canceled nonetheless.

With an operator-depending mapping, how can such a trivial bordercase error be silenced or ignored?

Here is a line to test it on:

$E = mc^2$ this is a test line with inline $\LaTeX$ math $\Omega$


At the end of the day, I solved the original problem with a different mapping:

onoremap <expr> a$ col('.') >= (col('$') - 1) && getline('.')[0] == '$' ? ':<C-U>normal! Bf$v,<CR>' : ':<C-U>normal! wBf$v,<CR>'
  • 1
    PS <silent> different from :silent. The first simply prevents characters in the mapping being echo’d to the screen (ie, the command-line remains unchanged). The second actually silences errors – D. Ben Knoble May 5 at 13:47

One option would be not to include the h in the mapping if you're on the first column:

onoremap <expr> a$ col('.') == 1 ? "f$" : ":<C-U>normal! hEF$v,<CR>"

N.B. I'm not 100% clear on how you want the mapping to behave if run when the cursor is not between a pair of $ signs. Some tweaks to the above may be necessary.

| improve this answer | |
  • The weird thing is, it does not work at the beginning of the line with the almost identical mapping onoremap <expr> i$ col('.') == 1 ? "f$" : ":<C-U>normal! hEF$lv,h<CR>" Help would be much appreciated! – Serge Stroobandt May 5 at 12:03
  • 1
    @SergeStroobandt How about this? onoremap <expr> i$ col('.') == 1 ? ":<C-U>normal! lvt$<CR>" : ":<C-U>normal! hEF$lv,h<CR>" – Rich May 5 at 12:39
  • Almost there! I have got now onoremap <expr> i$ col('.') == 1 ? ":<C-U>normal! lvt$<CR>" : ":<C-U>normal! hET$v,<CR>" and I am wondering whether one can extend the test for the character under the cursor at the beginning of the line; i.e. something like ` == 1 && char(col(':')) == "$"` I find it hard to find the pertaining documentation for these tests. – Serge Stroobandt May 5 at 14:37
  • 1
    @SergeStroobandt I think you're looking for getline('.')[0] == "$" – Rich May 5 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.