4

I have the following lines in my .vimrc

let g = 0
inoremap <silent> <C-g> <C-o>:let g = !g<CR>
inoremap <expr> <C-_> <C-o>(g == 0 ? :undo : :redo)<CR>

I'm trying to emulate an emacs-like undo/redo feature in insert mode. I'm not sure how to escape or use colon-commands inside a ternary expression. Any help would be greatly appreciated.

5

inoremap (g == 0 ? :undo : :redo)

You are using an expression mapping, so you must return an expression, e.g. in your case a string describing the command you want Vim to perform. That means your mapping should look like this:

inoremap <expr> <C-_> "\<C-o>".(g == 0 ? ':undo' : ':redo'). "\<CR>"

Or, even slightly simplified:

inoremap <expr> <C-_> "\<c-o>".(g ? ':red' : ':u')."\n"

One thing to note is, that I am not sure, that <C-_> will actually work. This might or might not work, depending on whether you are using a Gui version of vim or whatever type of terminal you use. See also the faq

1
  • Thank you Christian Bradbandt. <C-_> works for me. I don't use GVim very often. – god Feb 21 '18 at 13:38

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.