user1017860
user1017860

Reputation: 320

vim and unix pipe

By mistake I ran a funny command today that looks like vi filename | vi - . It made my terminal stuck even Ctrl-C was of no use. I had to close the terminal only. I tried it a couple of times and tried on my friend machine too. Just wondering why Ctrl-C was also not able to help.

Upvotes: 0

Views: 499

Answers (3)

mbde
mbde

Reputation: 171

vi is reading from stdin.

When you edit in vi Ctrl+c does not work either.

To quit vi use :q or :q! will work like in a normal vi session.

Upvotes: 3

skeept
skeept

Reputation: 12413

Vi intercepts ctrl-c (it is almost equivalent to esc) so ctrl-c would not work to quit the application in that setting.

I could escape from that trap by using ctrl-z and then using kill %

Upvotes: 0

oojeiph
oojeiph

Reputation: 21

Using the POSIX function signal() a C program can choose what to do if there is a keyboard interrupt.

Here is an example (copied from this site):

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

FILE *temp_file;
void leave(int sig);

main() {
    (void) signal(SIGINT, leave);
    temp_file = fopen("tmp", "w");

    for(;;) {
        /*
        * Do things....
        */
        printf("Ready...\n");
        (void)getchar();
    }

    /* cant get here ... */
    exit(EXIT_SUCCESS);
}

/*
 * on receipt of SIGINT, close tmp file
 */
void leave(int sig) {
    fprintf(temp_file,"\nInterrupted..\n");
    fclose(temp_file);
    exit(sig);
}

But as you can see, vi doesn't use the keyboard interrupt to exit. It doesn't matter whether you are using it in a pipe or not.

Upvotes: 1

Related Questions