2

When typing LaTeX one often needs to type a \ to invoke a command such as \omega. One of the nicer features of LaTeX is the ability to define your own commands, which can then be detected by Vim if 'define' is properly set. This will allow completion of these commands with <c-x><c-d>. I would like to avoid typing the \ too often as it is located rather awkwardly on my keyboard. Therefore I would like to be able to type ome<c-x><c-d> and have vim complete that to \omega. In other words, I want to be able to complete a word of which I never typed the first character. Is this even possible using the vim completion function or do I need something more capable?

Just to be clear, I will of course be remapping <c-x><c-d> in this case, as this is still an awkward combination.

  • 1
    Maybe you could try something like this: inoremap <c-x><c-d> <esc>bi\<esc>ea<c-x><c-d> This is not tested but the idea is to remap <c-x><c-d> in insert mode so that it will escape insert mode, add the missing `\` before the word, go back to the end of the word and start autocompletion. I didn't tested it so it might not work. – statox Feb 6 '17 at 8:11
  • Autocomplete trigger naturaly with ctrl+n no ? – MisterBanal Feb 6 '17 at 12:34
  • Yes it will, but I won't add the \ at the beginning of the command. Therefore it does not really complete the command. As I do not want to type that backslash ever, simply typing <c-n> does not work for me. – Octaviour Feb 6 '17 at 13:17
1

Mapping

As recommended by @statox in the comments of the question, this mapping seems to solve your problem

inoremap <c-x><c-d> <esc>bi\<esc>ea<c-x><c-d>

I have tested this mapping. It adds the backslash to the beginning of the word, and then shows you recommended completions (you would then need to select the desired completion manually).

As a frequent LaTeX user myself, I would opt for the following instead:

inoremap <c-x><c-d> <esc>bi\<esc>ea<C-n>

This does the same thing as the original mapping but automatically selects the first completion. I like this solution the best and have added this particular mapping to my own ftplugin/tex.vim file. (I have, however, replaced <c-x><c-d> with <C-l>.)

UltiSnips

I would recommend you check out UltiSnips. This plugin is overkill if you are only looking to use one or two mappings, but if you're open to a small time investment in exchange for a much more powerful solution, this is probably what your looking for.

You'll have to refer to the official documentation for all of the nitty-gritty details.

Basically, after you install the plugin, create a file called ~/.vim/my-snippets/UltiSnips/tex.snippet containing the following lines:

snippet ome "\omega"
\omega
endsnippet

Now, when editing a .tex file, you should be able to type ome followed by a <tab> and it will expand to \omega.

Given that it only saves you two keystrokes, this is a waste of a mapping if you ask me. But to each his own I guess.

Alternative Snippet: (as mentioned in the comments)

snippet cmd "\<command>"
\\${1:command}
endsnippet
  • Maybe it would be more interesting to create a snippet command for example with a place holder. This way you have a generic mapping where you can type command, <tab>, your command, and ultisnips would replace command by your place holder and add the antislash in front of it. – statox Feb 6 '17 at 10:15
  • That is more interesting, but then we would be losing keystrokes. For the sake of giving the OP an idea of how UltiSnips can be used, however, I have edited my post to include that alternative snippet. – Bryan Bugyi Feb 6 '17 at 10:37
  • I don't think the number of keystrokes is the important component here: With your first method, one has to create one snippet for each latex command used whereas the general snippet will work with all of the existing commands which fits better what OP was asking: a keymap to add an antislash in front of the current command. – statox Feb 6 '17 at 12:17
  • The thing I don't like about using UltiSnips in this case is that commands can (and will) be defined while typing up the document. This means that it will never be possible to autocomplete in this way without either defining each command in LaTeX AND UltiSnips or to use the general snippet (both cases presented in this answer). The first seems to be too much typing for me, the latter is basically just replacing \ with cmd. Since Vim already knows the allowed commands, I figured there should be an easier way to do this. (easier meaning less keystrokes) – Octaviour Feb 6 '17 at 13:12
  • @statox's mapping may be what you are looking for then. I have updated my solution to include this mapping as I think it is the better solution to this particular problem. – Bryan Bugyi Feb 6 '17 at 23:44

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.