From: Gregory Brown Date: 2006-01-06T02:22:27+09:00 Subject: Re: using irb On 1/5/06, Dick Davies wrote: > On 05/01/06, Gregory Brown wrote: > > > I use plain old vim and gvim and have both irb and the ruby > > interpreter bound to keyboard shortcuts. > > > > This way, I can write out whole programs and run them, or just pop > > open an irb session right inside my editor. :) > > > > Combine this with 'ri what's under my cursor' and you've got a rockin' > > development environment > > Nice, have you got any config snippets you wouldn't mind sharing? I'll paste in my .vimrc and my .irbrc file (the latter was lovingly lifted from JEG2) .vimrc --------------- " An example for a vimrc file. " set nobackup set nocompatible set backspace=indent,eol,start set history=50 " keep 50 lines of command line history set ruler " show the cursor position all the time set showcmd " display incomplete commands set incsearch " do incremental searching map Q gq set autoindent " always set autoindenting on set shiftwidth=2 set tabstop=2 set expandtab set tw=80 set nohlsearch map :se invhlsearch filetype on filetype indent on filetype plugin on syntax on if !exists( "*EndToken" ) function EndToken() let current_line = getline( '.' ) let braces_at_end = '{\s*\(|\(,\|\s\|\w\)*|\s*\)\?$' if match( current_line, braces_at_end ) >= 0 return '}' else return 'end' endif endfunction endif noremap :set invpaste paste? imap set pastetoggle= map :!ruby % map :!rake map :!ri imap :execute 'normal o' . EndToken()O imap def test_ "{{{ Ruby block delimiter conversion: do end <=> { } "Copyright (c) 2005 Mauricio Fernandez "Subject to same licensing terms as Ruby. " requires matchit and friends " since it uses the % and = bindings function! s:String_Strip(str) let s = substitute(a:str, '\v^\s*', '', '') return substitute(s, '\v\s*$', '', '') endfunction function! s:RubyBlockBraceToDoEnd(lineno) " { } => do end let oldz = getreg("z") call setreg("z", "") execute 'normal ^f{%l"zd$' let suffix = s:String_Strip(getreg("z")) call setreg("z", oldz) let orig = getline(".") let repl = substitute(orig, '\v\s*\{\s*(\|[^|]*\|)?.*', ' do \1', '') call setline(".", repl) let nextline = substitute(orig, '\v[^{]*\v\s*\{\s*(\|[^|]*\|)?', '', '') let nextline = substitute(nextline, '\}[^}]*$', '', '') let numlines = 0 " uncomment one of the following: " (1) just insert the body without splitting the lines on ; "call append(a:lineno, nextline) "call append(a:lineno+1, 'end' . suffix) " " (2) try to split on ; ... call append(a:lineno, 'end' . suffix) " this is what we would want to do: "let nextline = substitute(nextline, ';', "\n", 'g') while stridx(nextline, ";") != -1 let eom = stridx(nextline, ";") let line = s:String_Strip(strpart(nextline, 0, eom)) call append(a:lineno + numlines, line) let numlines = numlines + 1 let nextline = strpart(nextline, eom+1, strlen(nextline) - eom - 1) endwhile let nextline = s:String_Strip(nextline) if strlen(nextline) > 0 call append(a:lineno + numlines, nextline) let numlines = numlines + 1 endif " this is what it all began with... "execute 'normal :s/\v\s*\{\s*(\|.*\|)?/ do \1\r/ ' "execute 'normal g_cw end' execute 'normal V' . (1 + numlines) . 'j=' "echo "{ } => do end" endfunction function! s:RubyBlockDoEndToBrace(_firstline, _lastline) " do end => { } let linenum = a:_firstline + 1 let orig = getline(".") while linenum < a:_lastline - 1 let addline = getline(linenum) if '\v^\s*$' !~ addline let addline = substitute(addline, '\v^\s*', '', '') let addline = substitute(addline, '\s*$', '; ', '') let orig = orig . addline endif let linenum = linenum + 1 endwhile let l = substitute(getline(a:_lastline-1), '\v^\s*', '', '') let l = substitute(l, '\s*$', '', '') let orig = orig . l let l = substitute(getline(a:_lastline), '\v^\s*end(\.|\s|$)@=', ' }', '') let l = substitute(l, '\s*$', '', '') let orig = orig . l "echo orig "input(orig) let repl = substitute(orig, '\v\s*do\s*(\|[^|]*\|)?', '{\1 ', '') "execute 'normal d' . (a:_lastline - a:_firstline) . 'j' execute ':' . a:_firstline . ',' . a:_lastline . 'd' call append(a:_firstline - 1, repl) execute ':' . a:_firstline "echo "do end => { }" endfunction map xx xx let s:sid = maparg("xx") unmap xx let s:sid = substitute(s:sid, 'xx', '', '') function! RubyBlockSwitchDelimiters() range set nofoldenable if a:firstline == a:lastline let braceidx = match(getline("."), '{') let doidx = match(getline("."), '\') if braceidx != -1 && (doidx == -1 || braceidx < doidx) call s:RubyBlockBraceToDoEnd(a:firstline) elseif doidx != -1 execute 'normal /\' . "\n" . 'V%:call ' . \ s:sid . 'RubyBlockSwitchDelimiters()' . "\n" else echo "No block found" end else call s:RubyBlockDoEndToBrace(a:firstline, a:lastline) endif "execute 'normal V2k=' "execute 'normal v5j' endfunction command! -range B ,call RubyBlockSwitchDelimiters() vmap B :call RubyBlockSwitchDelimiters() ------ and .irbrc ------ require 'irb/completion' ARGV.concat [ "--readline" ] if $0 == "irb" IRB.conf[:PROMPT][:XMP][:RETURN] = "\# => %s\n" IRB.conf[:EVAL_HISTORY] = 1000 IRB.conf[:SAVE_HISTORY] = 100 HISTFILE = "~/.irb.hist" MAXHISTSIZE = 100 begin if defined? Readline::HISTORY histfile = File::expand_path( HISTFILE ) if File::exists?( histfile ) lines = IO::readlines( histfile ).collect {|line| line.chomp} puts "Read %d saved history commands from %s." % [ lines.nitems, histfile ] if $DEBUG || $VERBOSE Readline::HISTORY.push( *lines ) else puts "History file '%s' was empty or non-existant." % histfile if $DEBUG || $VERBOSE end Kernel::at_exit { lines = Readline::HISTORY.to_a.reverse.uniq.reverse lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if lines.nitems > MAXHISTSIZE $stderr.puts "Saving %d history lines to %s." % [ lines.length, histfile ] if $VERBOSE || $DEBUG File::open( histfile, File::WRONLY|File::CREAT|File::TRUNC ) {|ofh| lines.each {|line| ofh.puts line } } } end end def ri(*names) system(%{ri #{names.map {|name| name.to_s}.join(" ")}}) end end