14

I'm getting started with a Laravel 5 project and trying to run some experiments using 'php artisan tinker' (psy shell), but I'm running into some weirdness. In the tinker/psy shell, pressing any of the arrow keys is printing character literals to the screen rather than performing the intended behaviour (move character for left and right, cycle recent commands for up and down).

Up is outputting ^[[A. Down is outputting ^[[B. Right is outputting ^[[C. Left is outputting ^[[D.

This is probably an issue with my terminal and not a Laravel bug. I am getting the same buggy behaviour when running php -a.

I am running terminal on OSX, with an xterm emulation.

Brian Alex
  • 125
  • 1
  • 10
Sean Fraser
  • 342
  • 3
  • 14

4 Answers4

37

The PHP REPL does not implement readline's line editing and history capabilities. I don't know if there's a PHP module that implements it, but you can do:

rlwrap php artisan tinker

You may have to install rlwrap for your OS.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    Thanks! that did appear to be my problem. There does appear to be a readline php library: http://php.net/manual/en/book.readline.php, though rlwrap is just too easy. – Sean Fraser Feb 27 '15 at 15:11
  • It works! I've installed rlwrap and started Tinker with `rlwrap php artisan tinker`. I can now use my directional arrows. – Charles Aug 26 '15 at 19:38
  • 1
    Thanks for the tip! For those wondering how to install rlwrap on OSX easily, just install Homebrew pasting the one liner in their homepage (http://brew.sh) and then type in the console: `brew install rlwrap` – OMA Feb 19 '16 at 17:46
6

This is due to PHP not being built with readline support. You can enable this when you compile and build PHP with the --with-readline argument.

http://php.net/manual/en/features.commandline.interactive.php

arleslie
  • 460
  • 1
  • 4
  • 16
John
  • 123
  • 1
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/12397988) – Mike Rockétt May 19 '16 at 04:37
  • 1
    Hi Mike. This solution is exactly how I fixed the arrow and backspace problem in Linux and OSX. The link was is to documentation supporting my answer. The problem is not psysh, it's lack of readline support in PHP. If you think this needs clarification, then what was unclear to you? – John Jun 03 '16 at 05:23
  • Thank you, this was actually what I was looking for. – HeySora Dec 17 '21 at 23:18
2

MY os: centos I solved the problem by :

sudo yum install rlwrap
alias tinker='rlwrap php artisan tinker'

hook zou
  • 581
  • 5
  • 6
0

In my case, I need install php7-readline

sudo zypper in php7-readline                                                       
[sudo] password for root: 
Loading repository data...
Reading installed packages...
Resolving package dependencies...

The following NEW package is going to be installed:
  php7-readline

1 new package to install.
Overall download size: 64.0 KiB. Already cached: 0 B. After the operation, additional 30.9 KiB will be used.
Continue? [y/n/...? shows all options] (y): 
Retrieving package php7-readline-7.2.1-1.1.x86_64                                                                 (1/1),  64.0 KiB ( 30.9 KiB unpacked)
Retrieving: php7-readline-7.2.1-1.1.x86_64.rpm ......................................................................................[done (8.4 KiB/s)]
Checking for file conflicts: ....................................................................................................................[done]
(1/1) Installing: php7-readline-7.2.1-1.1.x86_64 ................................................................................................[done]
Sucipto
  • 817
  • 1
  • 8
  • 19