I am trying to map ctrl+shift+i to insert an italics command in LaTeX files, and here's my attempt:

autocmd BufNewFile,BufRead,BufWrite *.tex inoremap <buffer> <c-I> \textit{}^[i

I am using these three Buf event versions, because Filetype tex simply doesn't seem to recognize LaTeX files (I am sure I am doing something wrong).

In any case, the mapping above causes the Tab key to have the effect which I seemingly have mapped ctrl+shft+i to have, and I have no idea why.

  • 1
    So, it seems that ctrl+shft+i and Tab are the same thing in Insert mode...? Jun 6, 2022 at 0:10
  • 1
    <C-i> is the tab character; on my terminal; see the text on bestasciitable.com for a bit of an explanation. On my terminal, <C-i> and <C-S-i> both generate a tab, but <S-i> generates <Esc>[Z. You can check by pressing <C-v> and then your keybinding (works in Vim insert mode, but also most shells) which inserts the "raw" characters the terminal sends. Mapping letters with both Ctrl+Shift can be tricky in general; see Can I map a Ctrl + upper-case letter separately from Ctrl + lower-case letter? Jun 6, 2022 at 7:54
  • Note that "Ctrl + upper-case letter" may be a bit outdated as it's 7 years old; I haven't looked in to it as I don't like to use multiple modifier keys, but make sure to follow up on Christian Brabandt's comment on my answer. Jun 6, 2022 at 7:56
  • 1
    @MartinTournoij thanks for the explanation. I did try both <c-s-i> and <c-I>, I edited my question to reflect the latter because I guessed wrongly that it was the more "sophisticated" approach. Getting into mapping upper case letters doesn't seem worth the added complexity, from what I saw in the link you gave. Jun 6, 2022 at 13:54

1 Answer 1

  1. If you don't define g:tex_flavor, it will search for keywords in your file in order to select one of the available flavors. Therefore, in small .tex files, it will guess it incorrectly as plaintex. The solution is to add let g:tex_flavor = 'latex' in after/ftplugin/tex.vim.

  2. Be careful with autocommands. It is easy to duplicate autocommands and have performance issues, particularly since you're not wrapping them in groups (e.g. augroup MyAutoCommand).

  3. As for the initial question, you can use <C-S-i> or <C-S-Tab> if your terminal supports it. As far as I know, most terminals don't support this mode. This is called "modifyOtherKeys" in case you want to know more about it. I would say inoremap <leader>i \textit{}^[ is a better alternative (see :h leader). Additional points if you also define this in after/ftplugin/tex.vim.

  • 1
    I think you actually want g:tex_flavor defined in your vimrc file, since it's a global variable. Defining it in after/ftplugin/tex.vim might be already too late, since by then filetype definition will have already happened.
    – filbranden
    Jun 6, 2022 at 4:46
  • It works both ways. In fact, you want it to be defined later so that it can override any other option set by previous scripts. It will also be loaded only when it is needed (in tex files).
    – r_31415
    Jun 6, 2022 at 5:25
  • Thanks for your reply. Just so I avoid any confusion, that directory is typically ~/.vim/after/ftplugin/ in Linux, right? Your explanation to adding it there instead of vimrc seems legit. I'll try @filbranden approach if this one does not. Jun 6, 2022 at 13:58
  • I went with the popularly suggested , for the leader key, which makes it awkward to imap. The solution I chose was to map the behaviour to <c-g>i, but I am thinking that <c-s>i is even handier. Jun 6, 2022 at 14:01
  • @AlvaroNeto Yes, that's the right directory in Linux. You can check that file is being read after most other scripts by typing :scriptnames in a tex file. Another popular option for leader key is <space>, not sure if you find that more comfortable.
    – r_31415
    Jun 7, 2022 at 2:00

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.