1

I have the following bit of text in a vim file with cursor where the asterisk is while in insert mode:

\begin{document}
[latex]
Some text*
[\latex]
\end{document}

\begin{document}
[latex]
[\latex]
\end{document}

I want to hit ctrl-A to search for the next instance of [latex and insert a new line after that with this:

inoremap ^A <esc>execute "normal! /^[latex\<cr>o"

However, what happens instead is the first instance of [\latex] gets changed to [latex] with the cursor over the x. What has me baffled is that this works fine when I type in the keystrokes manually.

How do I get vim to do it what I want?

2

Solved with this:

inoremap <silent> <C-A> <esc>/[latex<cr>o

I guess execute doesn't work when mapping keys.

  • 1
    execute didn't work as expected in your original mapping because after pressing Esc, you are in normal mode. But for :execute to work properly you need to be in command-line mode, i.e. press a colon :. This would have worked too: inoremap <c-a> <esc>:execute "normal! /^[latex\<lt>cr>o"<cr>, but your new mapping is much simpler. – user938271 Aug 25 '18 at 0:29

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.