Doubtless
Doubtless

Reputation: 21

Open vim from PHP like git

How do i open vim from a PHP file to edit some other file like git when you run "git commit" without the -m flag?

I've tried this solution Open Vim From PHP CLI from-php-cli but it gets me some IO error with vim :

Vim: Warning: Output is not to a terminal Vim: Warning: Input is not from a termin?

is there other way to do this? probably with bash or something else?

Upvotes: 1

Views: 499

Answers (3)

Doubtless
Doubtless

Reputation: 21

I was able to do it using the proc_open method with one of the solutions from Open Vim From PHP CLI and a cycle that constantly checks if the process is still running. Once you finish editing it continues running the script.

$descriptors = array(
        array('file', '/dev/tty', 'r'),
        array('file', '/dev/tty', 'w'),
        array('file', '/dev/tty', 'w')
    );        
    $process = proc_open($_SERVER['EDITOR']. $filename, $descriptors, $pipes);
    //if(is_resource($process))
    while(true){
        if (proc_get_status($process)['running']==FALSE){
            break;
        }
    }

It's not very elegant but it does the job :)

PS:I'm sorry for the bad English >.<

Upvotes: 1

APSy
APSy

Reputation: 21

So you want to execute a bash script with PHP?

Take a look at:

shell_exec()
exec()

http://php.net/manual/de/function.shell-exec.php

http://php.net/manual/de/function.exec.php

The first one will return data from executed command, if you do a cat file.txt you will end up with a string which contains the content of file.txt

If you use exec it will not return std output, but will return true/false if the given command was successful.

You can now write a simple bash script which does manipulate text and then execute it via PHP.

Upvotes: 0

Ondřej Mirtes
Ondřej Mirtes

Reputation: 5626

Vim is an interactive editor. The error you are getting ("Output is not to a terminal") is a precise description of the problem. You can't control Vim from a PHP script.

If you need to pass text, you should use alternative approaches like the aforementioned -m flag or STDIN.

Upvotes: 0

Related Questions