I would like to have something in .vimrc where when I type (( in insert mode, it automatically converts that into \left( | \right) with the cursor position at | in insert mode. Any ideas?

I also would like this feature to be enabled when Filetype is tex.

  • Running macros always starts with the @ key. Do you mean a key mapping?
    – B Layer
    Nov 11 '21 at 0:20
  • Something I can define in .vimrc and only applied when the filetype is tex, that if I type (( in insert mode, it converts it into \left( | \right) with cursor at |.
    – Tom
    Nov 11 '21 at 0:21
  • Sounds like a key mapping.
    – B Layer
    Nov 11 '21 at 0:22
  • Yeah keymapping it is then!
    – Tom
    Nov 11 '21 at 0:22
  • I would take a look at the Ultisnips plugin.
    – mattb
    Nov 11 '21 at 10:03

In ~/after/ftplugin/tex.vim, put

inoremap <buffer> (( \left(  \right)<C-o>T(<space>

Don't forget to adjust b:undo_ftplugin (:help undo_ftplugin). I use a helper function for the task.

  • why can't I just use autocmd FileType tex inoremap (( \left( \right)<C-o>T(<space> ?
    – Tom
    Nov 11 '21 at 14:06
  • 1
    @Tom you can. I don't recommend it. The reasons are detailed, but the short versions are: maintainability, readability, flexibility, performance, and not re-inventing the wheel. Plus, as I said, b:undo_ftplugin.
    – D. Ben Knoble
    Nov 11 '21 at 14:26
  • What is b:undo_ftplugin? Where do I have to put it? Before or after?
    – Tom
    Nov 15 '21 at 14:13
  • @Tom cf :help undo_ftplugin. I usually put it at the end of my ftplugin file, though I have a helper to get the right value, since I usually want to extend what may already be there.
    – D. Ben Knoble
    Nov 15 '21 at 16:53
  • 1
    I think you want to use <buffer> argument for the inoremap Nov 16 '21 at 7:30

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.