5

If there is some text between quotations, such as

"x = 5"

I can type ci" in normal mode to delete the text inside the quotations and switch to insert mode.

I often want to perform this same action on text between two dollar signs, such as

$x = 5$

But typing ci$ has no effect. Is there a way to configure vim for treating dollar signs like quotations?

7

If you want only $ to be a text object, then do the following

xnoremap i$ :<C-u> normal! T$vt$<CR>
onoremap i$ :normal vi$<CR>
xnoremap a$ :<C-u> normal!F$vf$<CR>
onoremap a$ :normal va$<CR>

This will add some more text objects in your vim config.

for s:char in [ '_', '.', ':', ',', ';', '<bar>', '/', '<bslash>', '*', '+', '%', '$' ]
  execute 'xnoremap i' . s:char . ' :<C-u>normal! T' . s:char . 'vt' . s:char . '<CR>'
  execute 'onoremap i' . s:char . ' :normal vi' . s:char . '<CR>'
  execute 'xnoremap a' . s:char . ' :<C-u>normal! F' . s:char . 'vf' . s:char . '<CR>'
  execute 'onoremap a' . s:char . ' :normal va' . s:char . '<CR>'
endfor

The four commands in the for loop will add operator-pending and visual mode mappings for each of the characters in the dictionary.

Put the above commands in your vimrc for consistent behavior over all sessions.

  • 1
    You can remove backticks: iirc vim already has support for them as text objects – D. Ben Knoble Aug 15 '18 at 12:50
1

If you do not mind installing a plugin, you have several options you can choose from

  1. vim-textobj-latex (depends on vim-textobj-user) defines i$ and a$
  2. vim-latex-objects defines im and am
  3. vimtex defines i$ and a$
  4. vim-sandwich defines ib and ab by default. However, vim-sandwich can easily be customized to provide i$ and a$. Add following to ~/.vim/after/ftplugin/tex.vim:

    silent! omap <unique> i$ <Plug>(textobj-sandwich-query-i)$
    silent! xmap <unique> i$ <Plug>(textobj-sandwich-query-i)$
    silent! omap <unique> a$ <Plug>(textobj-sandwich-query-a)$
    silent! xmap <unique> a$ <Plug>(textobj-sandwich-query-a)$
    

Note, the classic plugin vim-latex (github) (aka latex-suite) does not provide this text object.

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.