Logan Bailey

Adventures In Web Development

Blog, About, GitHub, and LinkedIn

Ever wonder how to open your CLI's text editor from php similar to SVN/GIT do when -m flag is not supplied?

$file_name = '/tmp/'.substr(md5(uniqid(rand())), 0, 5).'.tmp'; 
system ('echo "\nPlease edit this file"' . $file_name); 
system ("vim $file_name > `tty`"); 

The steps are pretty straightforward, but I'll go over them here. The first line creates a random for the temporary file that the text editor will be writing to. The second line is a system echo call. This creates the file in the file system and adds some nice starting text. This could easily be updated to use fwrite or any other type of file write. We run a system call tty to get the terminals name and use that as output for vim. If you do not use this you will get the error Vim: Warning: Output is not to a terminal

Posted In:
cli php tips-n-tricks