2.12.2008

Making IRB work properly under Windows when using an international keyboard

Until today, I've been looking for a way to solve a problem I have running irb under Windows. The problem is that I'm using an international keyboard layout and keys like [, ], { and } (which are really useful in Ruby) could not be inputted in irb when readline is enabled.

I did some research on the web, but the solution did not worked for me (and it looks like I'm not the only one). A few weeks ago, I stopped looking for a solution as I decided to do my RoR work on a Linux Virtual machine. For some reason, I had to start working on Windows again. So today, I decided to dig a bit deeper. As the proposed solution used an .inputrc file, I investigated the format of the file and came across a post that described a problem using the Meta key shortcut (M-) in a .inputrc file. The response to the problem suggested to use "\e" instead of "M-". So I did that and guess what? it worked!

So putting everything together :

  1. Add the HOME environment variable => HOME=%USERPROFILE%
  2. Create a .inputrc file in your home folder (cd %USERPROFILE%) with the following content :
  3. "\e[": "["
    "\e]": "]"
    "\e{": "{"
    "\e}": "}"
    "\e\": "\"
    "\e|": "|"
    "\e@": "@"
    "\e~": "~" 
  4. For added completion/history management, create a .irbrc file in your home folder with the following content:
  5. require 'irb/completion'
    ARGV.concat([ "--readline", "--prompt-mode", "simple" ])
    
    module Readline
      module History
        LOG = "#{ENV['HOME']}/.irb-history"
    
        def self.write_log(line)
          File.open(LOG, 'ab') {|f| f << "#{line}\n"}
        end
    
        def self.start_session_log
          write_log("\n# session start: #{Time.now}\n\n")
          at_exit { write_log("\n# session stop: #{Time.now}\n") }
        end
      end
    
      alias :old_readline :readline
      def readline(*args)
        ln = old_readline(*args)
        begin
          History.write_log(ln)
        rescue
        end
        ln
      end
    end
    
    #Readline::History.start_session_log
    
    require 'irb/ext/save-history'
    IRB.conf[:SAVE_HISTORY] = 100
    IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
And you're done!

4 comments:

Daniel Cadenas said...

Great, it works perfectly, thank you!

Ezneh Werdna said...

Wow It works! Thank you so much. On Vista it works even if the file is called .inputrc.txt

Tom said...

Yeah, works perfectly, thank you.
By the way: Windows XP might prevent you from creating a file that starts with a '.' from the windows explorer. A way to work around that is to go to the command line and use the good old MS-Dos-Editor. Just type
edit .irbrcand save the file from within the editor.

kikito said...

Hi there!

Thanks a lot for this post, the lack of keys on irb was driving me crazy.

I extended this with the "#" and "\" keys, which are also "special" on some layouts.

"\e[": "["
"\e]": "]"
"\e{": "{"
"\e}": "}"
"\e\": "\"
"\e|": "|"
"\e@": "@"
"\e~": "~"
"\e#": "#"
"\e\\": "\\"

Note that "\" had to be escaped, this is not a typo.

AdSense Links