命令行Matlab中的vi输入模式? [英] vi input mode in command line Matlab?

查看:183
本文介绍了命令行Matlab中的vi输入模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的~/.inputrc中有以下几行:

set editing-mode vi 
set keymap vi

这使我可以在每个使用GNU readlines进行文本输入的程序中使用vi键盘绑定.示例:pythonirbsftpbashsqlite3等.它使使用命令行变得轻而易举. Matlab 使用读行,但是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?

我倾向于从命令行使用matlab -nosplash -nodesktop,这让我开始思考:是否有可能编写一个确实使用读取行并将其输入传递给matlab的包装器? (如果我必须实现这一点,我可能更愿意在Ruby中实现.)

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.)

更新:

感谢您的帮助.这几乎可行:

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

但是它一次只能从Matlab中读取一行(因为我正在使用gets).关于如何在下次等待输入之前获取所有内容的任何想法?这是正在发生的事情(我正在>>提示符下输入内容):

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

推荐答案

是的,这应该很容易.这只是一般的打开进程并绑定到其stdin和stdout"问题的特例,这并不困难.

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.

一些Google搜索发现IO.popen()是正确的Ruby,这里的答复中有更多细节:

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!

更新:看来您的包装已经快到了.您需要完成的工作是在Matlab要求输入时识别出,然后仅要求用户输入.我建议尝试使用此伪代码:

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.

更新2:好的,所以我们还需要考虑以下事实:提示后没有EOL,因此io.gets将挂起.这是一个修改后的版本,它使用了以下事实:您可以在Matlab提示符后添加空白行,而它只会给您另一个提示而无需执行任何操作.我重新安排了循环以使事情更清楚一些,尽管这意味着您现在必须添加逻辑来确定何时完成.

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

这篇关于命令行Matlab中的vi输入模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆