2

So I am pretty new to vim, but I have started using it as my Latex editor to help get more efficient. Anyways I have a recording that I would like to prompt a user for input on that will change how the recording goes. I am looking for some way of doing the following:

qa
i 
\begin{theorem}[XXX] #where XXX is the name of the theorem given by the user

\end{theorem}
ESC
<C-o>q

The idea would be that @a would wait for a prompt like "Pythagorean Theorem", and replace XXX above, and otherwise would leave [ ] blank. Is there an easy way to do this? Thanks in advance for anything you can recommend.

EDIT 1:

I have tried using functions outside of the file that take an input like:

function! GetTheorem()
   let curline = getline('.')
   call inputsave()
   let thm = input('Theorem Name: ')
   call inputrestore()
   call setline('.', curline . '[' . thm . ']')
endfunction

Which is stored in GetTheorem.vim. But when I set that as the source file for the code I end up getting only to the point where vim outputs:

\begin{theorem}[XXX]

And just ignores the rest of the macro commands even though it still says that it is "recording" as I was putting it together. Any Ideas how to fix that issue?

1 Answer 1

4

I'll give an answer, but not the one you are looking for. Hopefully, you will find it even better. Note that it does rely on you installing a plugin.

First, what you are trying to do is to simplify the process of creating a new theorem environment. You want to apply a template and to fill out some parts automatically. You are in luck, because this is the main motivation behind the concept of snippets.

There exists several snippet plugins for Vim, where the most popular ones are probably UltiSnips and neosnippet. Both of these plugins are very well documented. In addition, a lot of people use a community maintained default set of snippets: Honza's vim-snippets. This will contain a lot of LaTeX snippets as well.

With a snippet engine, you will typically define a trigger key such that when you type:

theorem<trigger-key>

It will expand to:

\begin{theorem}[|<name>]
  <next>
\end{theorem}

Here you can immediately insert the name, and a new key (which may often be set to the same key as the trigger key) can be used to go to the next input field <next> inside the environment.

Note that <next> above is not a part of the snippet, I just used it to explain/indicate that a snippet may contain several "jump targets" that may be used during expansion of the snippet.

I propose that you spend an hour or two reading the introductions for the documentation to the snippet engines and then select one of these to try out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.