4

Depending on how I launch Vim, suspending with <C-z> or with :suspend causes Vim to drop into an empty terminal (no shell) and get stuck there. I know I can unmap <C-z> like so map <C-z> <Nop> but is there a way I can prevent :suspend from running? Or redefine it to be an empty function?

I don't want to run Vim in restricted mode vim -Z because this doesn't allow things like :sh which actually do still work.

2 Answers 2

3

You can use the cmdalias.vim - Create aliases for Vim commands plugin to override the :suspend command with an empty one (e.g. :echo), but because the overriding is done via command-line mappings, it will only work when typing the command interactively. If there's a :suspend in a script / function, there's nothing that can disable that (except for disabling the command in the source code and compiling a special version of Vim yourself).

3

Looking at the source code, there doesn't seem to be a way to prevent Vim from suspending if the :suspend command is executed. See Ingo Karkat's answer for a discussion of how to prevent the command from being executed.

Unless, that is, one changes the Vim binary. Write the following code to a file override_kill.c:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int kill(pid_t pid, int sig) {
    int (*original_kill)(pid_t, int) = dlsym(RTLD_NEXT, "kill");
    if (pid == 0 && sig == SIGSTOP) {
        /*Don't suspend, initiate resuming*/
        original_kill(0, SIGCONT);
        return 0;
    } else {
        return original_kill(pid, sig);
    }
}

Compile with

gcc -O -Wall -fPIC -shared -o override_kill.so override_kill.c -dl

Instead of directly running Vim from a terminal emulator, run

env LD_PRELOAD=/path/to/override_kill.so vim

Vim will go through the motions of suspending but immediately resume execution. (Only on a terminal: the GUI version will iconify as usual.)

Only tested under Linux; this probably works under most Unix-like operating system, except that they may require different compiler options. OSX should work with DYLD_PRELOAD instead of LD_PRELOAD.

This technique cannot be generalize to overriding arbitrary commands: only library calls can be overridden this way, not calls to functions that are internal to the Vim binary. (That's why the modified program still goes through the motions and only the actual suspension is overridden.)

2
  • 1
    Huh, this is interesting. I didn't know it was so easy to override a library call. This does technically answer my question (as does the other answer) but neither are as portable as I'd like (.vimrc setting would be ideal but I hate adding plugins for such a small problem). Is it bad etiquette to leave a question without a marked answer for a while? Commented Mar 11, 2015 at 20:06
  • @ChadParadis Feel free to accept an answer or not as you see fit. The only case in which it's bad etiquette not to accept an answer is when there is a definitive, comprehensive, clearly correct, well-explained answer. My answer is absolutely not definitive or comprehensive: I can only offer a partial, cumbersome solution. Commented Mar 12, 2015 at 20:39

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.