1

I have a macro that's essentially equivalent to the following: in insert mode, I press the following keys: Ctrl+O, :, !, t, r, u, e, Enter. This runs the command true in a shell and shows "Press ENTER or type command to continue".

If I press q, it continues past that point as well as inserting a spurious 'q' into the document I was editing. I don't want Vim to assume that it's OK to insert what I'm typing into a document when Vim isn't currently showing me the document. How do I stop this behavior?

4
  • Replace <Ctrl-O> with <Esc>, and add 'i' after <CR> at the end of your macro
    – Alex Kroll
    Jul 28, 2019 at 15:19
  • 1
    @AlexKroll That seems to hide the final output of the command. Jul 28, 2019 at 16:53
  • 1
    Yeah. It seems more complicated than i thought. But you can redirect shell output to "scratch" buffer. Just for example and start point for further gist.github.com/romainl/eae0a260ab9c135390c30cd370c20cd7 Also since vim8 you may like to use :term
    – Alex Kroll
    Jul 28, 2019 at 19:34
  • 1
    What is the task that you're trying to achieve? The question seems rather artificial, and could be indicative of a XY problem. Jul 30, 2019 at 10:22

2 Answers 2

2

There doesn't seem to be a good way around what you're looking for.

The behavior is clearly documented, in quite some detail, in :help press-enter.

In particular:

Press : or any other Normal mode command character to start that command.

In your case, since you hit that from a CTRL-O means the next keypress will actually be inserted in your text.

Note that not all keys will be recognized this way. For example:

Press k, <Up>, u, b or g to scroll back in the messages. This works the same way as at the more-prompt. (Only works when 'compatible' is off and 'more' is on.)

These keys won't leave the "Press ENTER" prompt even if your command has no output (as is the case with :!true.) If your command has long output, then once you scroll up, keys to scroll down will also be active and not leave the prompt to get inserted in your text.

There doesn't seem to be a way around it. I can see why you'd want a behavior such as "Press ANY key to continue" but that doesn't seem to be available.

You could file a new issue at the Vim tracker to request a way to tweak this behavior. (If you explain your use case invoking a command from CTRL-O and having characters inserted into the text while the text is not visible, you might make such a feature request more compelling.) Though even if you successfully get that accepted, it will only be available in a future version of Vim, so not in generally available versions if you want to be compatible.

Most workarounds on this "Press ENTER" prompt are about hiding or preventing it (which you can easily do by adding an extra <cr> to the end of your mapping.) But that doesn't seem to be what you want, since you do want the output of the command to be visible until the user presses Enter (or another key!)

Consider alternative solutions, such as saving the external command's output into a scratch buffer or preview window and displaying it there, you can better control the user experience that way.

1

Thanks to @filbranden's hint to append an extra <CR> to skip past Vim's prompt, I now have two solutions using Bash's read:

Wait for Enter

<C-o>:!true;read -sp$'\nPress ENTER to continue. '<CR><CR>

Wait for any key

<C-o>:!true;read -n1 -sp$'\nPress any key to continue. '<CR><CR>
1
  • Yes, awesome idea! Do the waiting in bash and skip Vim's.
    – filbranden
    Aug 7, 2019 at 5:38

Your Answer

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

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