1

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...? – Alvaro Neto Jun 06 '22 at 00:10
  • 1
    <C-i> is the tab character; on my terminal; see the text on https://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? – Martin Tournoij Jun 06 '22 at 07: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. – Martin Tournoij Jun 06 '22 at 07:56
  • 1
    @MartinTournoij thanks for the explanation. I did try both and , 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. – Alvaro Neto Jun 06 '22 at 13:54

1 Answers1

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.

r_31415
  • 576
  • 4
  • 13
  • 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 06 '22 at 04: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 06 '22 at 05: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. – Alvaro Neto Jun 06 '22 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. – Alvaro Neto Jun 06 '22 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 07 '22 at 02:00
  • @RobertSmith so I checked, and it turns out the file is not being read! Explains why Filetype isn't doing anything – Alvaro Neto Jun 11 '22 at 02:36
  • @AlvaroNeto That's highly unusual. In a default installation, all such files should be read. Verify you have filetype plugin on in your .vimrc file. – r_31415 Jun 11 '22 at 18:42
  • @RobertSmith I did not. I made that addition, thank you very much. Now it is telling me Pattern not found: tex_flavor = 'latex'. I am wondering if the problem is with the file path, ~/.vim... – Alvaro Neto Jun 12 '22 at 11:13
  • @AlvaroNeto Okay, you are setting this option incorrectly. It should be added as let g:tex_flavor = 'latex', not simply as g:tex_flavor = 'latex' because the latter is using the global operator instead (:h :g) – r_31415 Jun 12 '22 at 16:28
  • @RobertSmith of course. Officially a noob. Thank you for your patience. – Alvaro Neto Jun 13 '22 at 05:58
  • No problem. I'm glad it is working now. – r_31415 Jun 13 '22 at 15:47