Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vi input mode in command line Matlab?

Tags:

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
like image 366
Benjamin Oakes Avatar asked Mar 04 '10 15:03

Benjamin Oakes


People also ask

What is set vi?

SETVI EnableSales enablement via up-to-date product data, inventory, pricing and promotions info, bolstered with digital showrooms and intelligent customer solutions.

How do I run a command in Matlab?

Go to the Editor tab and click Run . MATLAB® displays the list of commands available for running the function. Click the last item in the list and replace the text type code to run with a call to the function including the required input arguments.

Where is command window in Matlab?

Description. The Command Window enables you to enter individual statements at the command line, indicated by the prompt (>>). As you enter statements, the Command Window displays the results. For example, to create the variable a , type a = 1 at the command line and press Enter.


1 Answers

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
like image 60
Brooks Moses Avatar answered Oct 21 '22 10:10

Brooks Moses