0

When you run a command such as :! sleep 5, then you can normally cancel it using Ctrl-c. How do I remap that combination to something else, for example Ctrl-x?

The context is that I use urxvt with option URxvt.keysym.C-c: eval:selection_to_clipboard (along with stty intr ^X.) But this means that Ctrl-c doesn't get passed to vim, so Ctrl-c does nothing.

If I set cmap <C-x> <C-c> then Ctrl-x will cancel commands that I haven't started yet, but it does nothing after I've started the command.

Alternatively, is there any way to get escape to cancel a running command?

I'm on NVIM v0.4.4. Thank you.

  • When in terminal, Ctrl+c is the standard keybinding to send an interrupt signal to the running process. You will probably have to solve similar problems for all kinds of programs (not only vim) like aborting shell scripts or won't even be able to solve them. The benefits of overriding default behaviour often are not worth the new problems that arise long-term, and Ctrl+c as Interrupt definitively is such a case. So I highly recommend using some other keybinding to copy a visual selection to clipboard, e.g. Ctrl+Shift+c. – Philipp Moers Aug 18 '20 at 20:16
  • Thank you for the comment, but this is the first time I've run into stty intr ^X not working. – C4K3 Aug 19 '20 at 22:14
1

When you issue :!sleep 5, Vim steps back and passes control to the shell. Quoting :help :!,

:!{cmd} Execute {cmd} with the shell.

With stty intr ^X you have assigned the interrupt signal to CtrlX. Therefore, CtrlX interrupts :!sleep 5. It's funny, but it's true: You already have what you asked for.

  • Thanks. Unfortunately Ctrl X doesn't work, but Ctrl C does work if I remove the urxvt config, so somehow the stty inty ^X isn't making it through? I don't really understand the fine nuances of what :! does. – C4K3 Aug 16 '20 at 3:12
  • 1
    @C4K3 Hmm... If you open a terminal, issue stty intr ^X;sleep 9 and type Ctrl-X, does it interrupt the sleep command? That would be the expected behavior. Then open a terminal again, stty intr ^X;vim, in Vim issue :!sleep 9 and Ctrl-X, doesn't it interrupt the sleep command? :!cmd simply means "execute the command in the shell, that's not a Vim internal cmd". – Quasímodo Aug 16 '20 at 13:25
  • stty intr ^X;sleep 9 does get interrupted with Ctrl-X. stty intr ^X;vim and then :!sleep 9 does not get interrupted by Ctrl-X. :!stty intr ^X; sleep 2 gives stty: 'standard input': Inappropriate ioctl for device. – C4K3 Aug 19 '20 at 22:05
  • @C4K3 Sorry, I don't know what is going on then. You are not using Gvim, are you? – Quasímodo Aug 19 '20 at 22:21
  • Thanks for looking into it though! No, I am not using Gvim. – C4K3 Aug 20 '20 at 8:34

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.