4

How do you open up vim from a CLI like svn and git do when you drop -m from commit commands?

I'm getting the follow error: Vim: Warning: Output is not to a terminal

`echo "Please edit this file" > file.name`;
`vim file.name`;

4 Answers 4

8

PHP Doesn't automatically pass thru the STDIN/STDOUT streams, you need to do it manually:

`echo "Please edit this file" > file.name`;
system("vim file.name > `tty`");

(Note: I don't really understand what I'm talking about, I just know the above works.)

Sign up to request clarification or add additional context in comments.

2 Comments

This worked great. Had some issues with it though, since you were changing the output buffer system returns automatically to the PHP script before you exit vim. I changed it to exec(); which is the same as ``. And that prevented the hanging.
Is it supposed to work on Windows? I got the warning Logan mentioned.
1

Hi this is a example using proc_open in PHP, only runs in systems with /dev/tty (Linux/OSX)

<?php

$descriptors = array(
        array('file', '/dev/tty', 'r'),
        array('file', '/dev/tty', 'w'),
        array('file', '/dev/tty', 'w')
);

$process = proc_open('vim', $descriptors, $pipes);

Comments

0

EDIT:

I just realized it sounds like you already have this set up for svn/git... what are you trying to open it from?


for bash: export SVN_EDITOR=vim although EDITOR will work as well, though this will influence other things as well. IF for some reason vim isnt on your path youll need to use the full path as well. Place this in your .profile or .bash_profile.

1 Comment

Trying to write a php script to open vim in a similar manner.
0

accepted answer no longer works for me, so assuming you have some file you need to open with Vim while running your php script:

system("vim {$file_name} > /dev/tty", $error);

here is why this works:

> /dev/tty is the key to making Vim interactive in a CLI environment.

> is the shell redirection operator. It redirects the standard output (stdout) of the preceding command.

/dev/tty is a special file in Unix-like systems (including Linux and macOS) that represents the controlling terminal of the current process. In simpler terms, it's a direct connection to the terminal window where you're running the script.

some options that didnt work for me:

the obvious but wrong:
command: system("vim {$file_name}", $error); result: Vim: Warning: Output is not to a terminal and Vim appears a moment later in read only.

the accepted answer that worked for me for a while until god knows what broke it:
command: system("vim {$file_name} > `tty`", $error);
result: Vim: Warning: Output is not to a terminal , terminal freezes until Ctrl+c, and created file not a tty in cwd with file contents.

side note: i see a worrying trend of ignoring error handling. notice the $error at the end, it returns the exit value of the command you ran so you can handle errors. please at the very least handle general failures. if something unexpected happens its easier to debug and user reports will be more helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.