Benjamin Oakes
Benjamin Oakes

Reputation: 12782

vi input mode in command line Matlab?

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

Upvotes: 20

Views: 3585

Answers (3)

towolf
towolf

Reputation: 350

You could have used rlwrap in a straight-forward manner.

rlwrap is a wrapper that uses the GNU readline library to allow the editing
of keyboard input for any other command. 

http://utopia.knoware.nl/~hlub/rlwrap/#rlwrap

Unfortunately it will block context-sensitive tab completion in MATLAB, which is useful on its own.

Upvotes: 4

Brooks Moses
Brooks Moses

Reputation: 9527

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:

while input_line = Readline.readline('>> ', true)
  io.puts input_line
  while ((output_line = io.gets) != '>> ')  // Loop until we get a prompt.
    puts io.gets
  end
end

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.

while [not done]   // figure this out somehow
  io.puts blank_line                        // This will answer the first
                                            // prompt we get.
  while ((output_line = io.gets) != '>> ')  // Loop until we get a prompt.
    puts io.gets                            // This won't hang, since the
  end                                       // prompt will get the blank
                                            // line we just sent.

  input_line = Readline.readline('>> ', true)  // Get something, feed it
  io.puts input_line                           // to the next prompt.

  output_line = io.gets   // This will eat the prompt that corresponds to
                          // the line we just fed in.
end

Upvotes: 5

AVB
AVB

Reputation: 4024

Actually, you might be better off writing this in C - then you can call the matlab engine directly. This basically allows you to write your own front-end to matlab, if you are so inclined, using the GNU Readline library.

Upvotes: 3

Related Questions