Mapping <C-j> in Vim for non-tex files, with Latex-Suite installed

109 views Asked by At

I've found some older posts here that discuss the difficulty with mapping the <C-j> shortcut in vim, i.e.

map <C-j> <C-w>j

due to latex-suite's usage of <C-j> to jump forward in the text. I'm in a similar situation, but I would like to keep latex-suite's normal shortcuts for when editing tex files, and only use the new mapping when editing non-tex files. I'm a little confused by why this isn't the default behavior - latex-suite's <C-j> shortcut shows up as a shortcut in the :map output even when I don't have a tex file loaded and other shortcuts (like `a mapping to \alpha) don't work. Am I understanding correctly that unlike most of latex-suite's shortcuts, this <C-j> shortcut from latex-suite gets loaded regardless of filetype for some reason? And if so, how do I make it so that it only gets loaded for tex files, so that I can use other <C-j> mappings for non-tex files?

1

There are 1 answers

1
romainl On

The only sane solution would be for the maintainers of that plugin to move all their filetype-specific mappings to an ftplugin, where they belong. The way they currently implement them is a silly mix of good practices (<Plug>) and bad practices (global filetype-specific mappings), all in a single script under plugin/. This is very bad.

What they currently do:

" in plugin/imaps.vim

inoremap <silent> <Plug>IMAP_JumpForward    <C-\><C-N>:call IMAP_Jumpfunc('', 0)<CR>

if !hasmapto('<Plug>IMAP_JumpForward', 'i')
    imap <C-J> <Plug>IMAP_JumpForward
endif
  • The first mapping is okay-ish: plugin authors should use virtual :help <Plug> mappings as much as possible in order to allow users to write their own mappings easily. IMAP_JumpForward should be in parentheses, though.
  • The :help hasmapto() guard is pointless.
  • plugin/imaps.vim is a "global plugin". As such, whatever it does that is not explicitly scoped to a buffer or a window is done for every buffer and window. That <C-j> mapping only makes sense…
    • in the context of tex files, when using that latex-suite plugin,
    • if you actually use that imaps.vim plugin elsewhere.

What they should do:

" in plugin/imaps.vim

inoremap <silent> <Plug>(IMAP_JumpForward)    <C-\><C-N>:call IMAP_Jumpfunc('', 0)<CR>

" in ftplugin/**/<somefile>.vim

imap <buffer> <C-J> <Plug>(IMAP_JumpForward)

Frankly, the way that thing is designed should raise all kinds of red flags. I don't do (la)tex at all but https://github.com/lervag/vimtex seems more competently done.