Running External Commands

Our OpenMPC function gets the current track by running mpc current, with the --format argument. This is a shell command, and in VimL we can use the system function to call shell commands.

system works like Vim’s bang command (:!). You might be familiar with using that command to execute a shell command from inside Vim:

 
:!date
 
Sat Oct 18 19:35:53 CDT 2014
 
Press ENTER or type command to continue

In like manner, system takes a String command to run and executes it. It then returns the command’s output to us as another String.

Now notice how we display the command’s output. In the previous chapter we made a lot of use of :echo to echo messages to the user. In OpenMPC, we’re using another echoing command: :echomsg ...

Get The VimL Primer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.