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.

3
  • 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. Commented Aug 18, 2020 at 20:16
  • Thank you for the comment, but this is the first time I've run into stty intr ^X not working.
    – C4K3
    Commented Aug 19, 2020 at 22:14
  • I've ended up solving this by configuring urxvt with URxvt.keysym.C-x: \003 and not setting stty intr ^X. Thanks to unix.stackexchange.com/a/303258/464554
    – C4K3
    Commented Apr 1, 2021 at 4:54

1 Answer 1

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.

8
  • 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
    Commented Aug 16, 2020 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
    Commented Aug 16, 2020 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
    Commented Aug 19, 2020 at 22:05
  • @C4K3 Sorry, I don't know what is going on then. You are not using Gvim, are you?
    – Quasímodo
    Commented Aug 19, 2020 at 22:21
  • Thanks for looking into it though! No, I am not using Gvim.
    – C4K3
    Commented Aug 20, 2020 at 8:34

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.