Krasiejów (Poland)

(neo)Vim, Codi & PHP

Lukasz
5 min readNov 18, 2020

--

Codi is a vim plugin to run your code in the editor. Who knows Quokka (which in my case was the first such a tool I’ve been using) knows what I mean. I can’t say, at least for the time being, whether it increases productivity or not — in my case, it is just for fun, but definitely — there is potential in it!

Installation is straight forward; however, if you have not been working with the vim plugins before, it can look a bit complex as there is a lot (at least a few :)) plugins managers for vim. So (1) you have to find your-plugin-manager, (2) install the Codi plugin and (3) you are there! Nothing more left to do — you can start experimenting with the code!

Ok, not exactly — in case of PHP you will have to install PsySH yet (globally) as PsySH is used by default by Codi (not the PHP interpreter directly).

$ composer g require psy/psysh:@stable

When PsySH works, then you can go back to the Vim and Codi. BTW — I’m using nvim — Neovim — it is a rewritten, still-maintain version of the old-good-vim. However, in this particular case, there is a little difference. In Vim Codi’s results will be probably shown in a separate buffer (pane) at the right side of the code window — in Neovim there is used a virtual text feature which presents the results directly in the same pane where you type code.

So, let’s do it!

$ nvim test.php 

Next start Codi: :Codi php and you can start typing your code, let say:

screen 1

And here is the first little problem — what is that open square bracket !?

Hmm… let’s do a quick check how it looks in python (screen 2).

screen 2

There are differences — yep. Python interpreter will not show us the value in the place of declaration, like PsySH. BTW, in PHP we can, of course, use the notation like $a; to print particular variable anywhere (like I did with the “l” in screen 2)— and the result will be similar (please note the line numbers — it is vim! :)).

Ok, but what about that open bracket at line 3 in the above PHP script? To find the answer, you will have to start psysh:

➜  ~ psysh
Psy Shell v0.10.4 (PHP 7.4.11 — cli) by Justin Hileman
>>> $a = [1, 2, 3]
=> [
1,
2,
3,
]
>>>

So the PsySH is used by the Codi, but its multiline formatting doesn’t precisely follow the Codi expectations and only the very first line of the result is shown.

I’m sure there is a way to “filter out” the white characters from the PsySH’s result by appropriate Codi configuration (each interpreter — like the PsySH for PHP — can be configured in Codi). Unfortunately — I have failed with that. More about Codi configuration can be found here.

When I had failed with fixing that by Codi configuration, I focused on PsySH. I found the line of code which adds these white characters and… commented it out.

screen 3

From that point, the output looked like that:

➜  ~ psysh
Psy Shell v0.10.4 (PHP 7.4.11 — cli) by Justin Hileman
>>> $a = [1, 2, 3]
=> [ 1, 2, 3,]
>>>

Not ideal — but it is in one line! Now, in Vim:

screen 4

Ok, I have also added a few more lines of code — can you see this string: > 123 in line 7 (one line before the cursor which is in line 8)? It is the for-loop result (so the output of the echo command).

Now let’s do something nasty — let’s remove one element from the array — in PHP it is a really nasty thing!

screen 5

Please ignore — for the time being — what you can see at line 7 in screen 5 (so the line next to the number 5) — we will go back to that in a minute. Check the echo result in line 11. It is… nothing, null, empty result — but why? When you run that script from the command line you can notice something strange (at first, as for not a PHP developer):

1PHP Notice:  Undefined offset: 1 in test.php on line 10

Yep… so we are receiving an error (a Notice you may say, but the script execution is terminated). But! The first echo has been executed! Please draw your attention on that first character — that “1” — at the above PHP output. However, Codi ignores it (what shouldn’t surprise in fact).

If you are interested what causes this error — why this offset “1” is “undefined” — please check the below PsySH session:

➜  ~ psysh
Psy Shell v0.10.4 (PHP 7.4.11 — cli) by Justin Hileman
>>> $a = [1, 2, 3]
=> [ 1, 2, 3,]
>>> unset($a[1])
>>> $a
=> [ 0 => 1, 2 => 3,]
>>>

Can you see that gap in the second $a indices? [ 0 => 1, 2 => 3,] — so at index 0 there is value “1”, and at index 2 there is value “3” — but there is no index “1” in that array (as we have removed it with the unset command)! It’s strange, but true — such a PHP flavour :) And now you probably know what that strange array in line 7 (in the last screenshot) is (line 7, so the line next to the number 5 — remember, the 5th line before the cursor which is in the line 12). It looks like: [ 0 1, 2 3,] — yep, it is the same that [ 0 => 1, 2 => 3,] but without => (and I didn’t figured out why these particular characters are stripped out from the Codi output).

And that‘s it. :)

If you are interested in arrays in PHP there is a nice article you can find in the PHP Architect magazine called Never* Use Arrays. But here — in fact — I wanted to focus more on Codi — isn’t it a good-to-know’s tool? I hope that Codi and also such a way of executing/working with the code — if you haven’t tried it yet — interested you a bit!

--

--