I have these lines in my ~/.inputrc:
set editing-mode vi
set keymap vi
This allows me to use vi keybindings in every program that uses GNU readlines for text input. Examples: python, irb, sftp, bash, sqlite3, and so on. It makes working with a command line a breeze. Matlab doesn’t use readlines, but vi keybindings would be amazing to have when debugging or working interactively. Is there an existing solution?
I tend to use matlab -nosplash -nodesktop from the command line and that got me thinking: would it be possible to write a wrapper that does use readlines and pass the input to matlab? (If I have to implement this, I’d probably prefer to do so in Ruby.)
Update:
Thanks for the help. This almost works:
# See also: http://bogojoker.com/readline/
require 'readline'
puts 'Starting Matlab...'
io = IO.popen('matlab -nosplash -nodesktop 2>&1', 'w+')
while input_line = Readline.readline('>> ', true)
io.puts input_line
puts io.gets
end
But it only reads a single line from Matlab at a time (because I’m using gets). Any ideas on how to get everything until the next time it’s waiting for input? Here’s what’s happening (I’m entering stuff at the >> prompt):
Starting Matlab...
>> 1
>> 2
< M A T L A B (R) >
>> 3
Copyright 1984-2009 The MathWorks, Inc.
>> 4
Version 7.8.0.347 (R2009a) 32-bit (glnx86)
>> 5
February 12, 2009
>> 6
>> 7
>> 8
To get started, type one of these: helpwin, helpdesk, or demo.
>> 9
For product information, visit www.mathworks.com.
>> 0
>> 1
>>
>> 2
ans =
>> 3
>> 4
1
>> 5
>> 6
>>
>> 7
ans =
>> 8
>> 9
2
>> 0
>> 1
>>
>> 2
ans =
>> 3
>> 4
3
Yes, that should be easy enough. It’s just a special case of the general “open a process and bind to its stdin and stdout” problem, and that’s not difficult.
A bit of Google searching finds that
IO.popen()is the right piece of Ruby for that, and there’s a little more detail in the replies here: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/0bbf0a3f1668184c. Hopefully, that’s enough to get you started!Update: Looks like you’re almost there with your wrapper. What you need to get finished is recognize when Matlab is asking for input, and only ask the user for input then. I’d suggest trying this pseudocode:
That’s not quite right, as you need to do the inner loop once before you ask for the first input line, but it should give you the idea. You might need to adjust the prompt text that it’s looking for, too.
Update 2: Okay, so we also need to account for the fact that there’s no EOL after a prompt and so io.gets will hang. Here’s a revised version that uses the fact that you can give a blank line to a Matlab prompt and it will just give you another prompt without doing anything. I’ve rearranged the loop to make things a little clearer, though this means you now have to add logic to figure out when you’re done.