Why don't all the readline shortcuts work in psql (or IRB, or the Rails console)? - readline

In bash and zsh, you can use handy readline shortcuts like these to navigate between words, delete from the middle of the line, etc.
However, only a couple of these (Ctrl-A and Ctrl-E and a couple of others) work in psql, IRB, and the Rails console and probably others.
Why is this, and how hard would it be to fix it? It sure would be handy to be able to quickly navigate between words in a long SQL query.

Are your copies of psql &c built with readline?
From the sound of it, yours might be built with an alternate editing library like libedit. To get all the features of readline, build them using it.

Related

/usr/bin/ruby file just contains gibberish and the word 'ELF'; safe to delete this/is this the file I really need?

I'm trying to install the Ruby gem foreverb, to keep my server process running when SSH disconnects, and also for improved understanding of the people visiting my site.
On following this guide:
https://github.com/DAddYE/foreverb
It mentioned to write some code in /usr/bin/ruby so I dutifully opened the file and looked inside. Instead of what I expected (either a blank file or something that looks like Ruby code) I was instead greeted with this:
^?ELF^B^A^A^#^#^#^#^#^#^#^#^#^C^#>^#^A^#^#^#à^H^#^#^#^#^#^##^#^#^#^#^#^#^#^P^Q^#^#^#^#^#^#^#^#^#^##^#8^# ^##^#^[^#^Z^#^F^#^#^#^E^#^#^##^#^#^#^#^#^#^##^#^#^#^#^#^#^##^#^#^#^#^#^#^#ø^A^#^#^#^#^#^#ø^A^#^#^#^#^#^#^H^#^#^#^#^#^#^#^C^#^#^#^D^#^#^#8^B^#^#^#^#^#^#8^B^#^#^#^#^#^#8^B^#^#^#^#^#^#^\^#^#^#^#^#^#^#^\^#^#^#^#^#^#^#^A^#^#^#^#^#^#^#^A^#^#^#^E^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#¼^K^#^#^#^#^#^#¼^K^#^#^#^#^#^#^#^# ^#^#^#^#^#^A^#^#^#^F^#^#^# ^M^#^#^#^#^#^# ^M ^#^#^#^#^# ^M ^#^#^#^#^#p^B^#^#^#^#^#^#x^B^#^#^#^#^#^#^#^# ^#^#^#^#^#^B^#^#^#^F^#^#^#¸^M^#^#^#^#^#^#¸^M ^#^#^#^#^#¸^M ^#^#^#^#^#Ð^A^#^#^#^#^#^#Ð^A^#^#^#^#^#^#^H^#^#^#^#^#^#^#^D^#^#^#^D^#^#^#T^B^#^#^#^#^#^#T^B^#^#^#^#^#^#T^B^#^#^#^#^#^#D^#^#^#^#^#^#^#D^#^#^#^#^#^#^#^D^#^#^#^#^#^#^#Påtd^D^#^#^#<98>
^#^#^#^#^#^#<98>
^#^#^#^#^#^#<98>
Not entirely sure what it is, but pretty sure it shouldn't be there. Is it file corruption, as it doesn't look like what I'd generally consider to be file corruption? While I assume it's probably not doing anything useful at the moment, does the presence of semi-arbirary strings in this file represent a problem with my Ruby installation which I need to fix? Is there perhaps another location or set of locations where the right file might be, and perhaps this one isn't being used?
I notice I have a lot of problems with my server, and often find I have to randomly reinstall gems which seem to disappear from my system, usually after the SSH disconnects. Could this file be something to do with that?
It seems odd to me that if this is the location of an important Ruby system file that my Ruby installation should work at all, but it does seem to... Might it be left over from a previously broken installation? I'm only just progressing out of the beginner stages with Ruby, so any insight into what the file does, why it's important and, particularly, how I should progress would be extremely helpful. Also, any suggestions for further reading on Ruby framework eco system which might help me better understand the inner working of stuff like this would hopefully save me from having to take up people's time here with similar questions in the future, though I understand that's a little outside the usual remit for the site.

Find all uses of a method in vim

Is there a way to find all uses of a method in vim? I'm using vim as an IDE for Rails with rails.vim. ctags helps to jump to a method definition from usage but not the other way round AFAIK. I'd like to be able to find everywhere (controllers, views etc) that a method has been used.
There aren't any perfect solutions for this in Vim, but you can get close with cscope and grep or ack.
cscope will help you find all references to a symbol. It's made for C and C-like languages, but it does a decent job of matching symbols in Ruby code as well. It's not going to get the context right all the time.
Here's a vim cscope tutorial and a blog post about Ruby/Vim/cscope, and another blog post, both of which include additional tips about navigating Ruby/Rails code in Vim.
Using grep or ack from Vim with quickfix integration is another great way to find symbols. They've got no notion of scope/context, but often a simple search is enough. Using just the built-in :grep command, you can do:
:grep some_method app/controllers
:cwindow
And get the results of the search in the quickfix window, which will allow you to quickly navigate to the matching files and line numbers.
A much better option is the ack.vim plugin, which integrates ack with Vim and makes use of the quickfix window.
If you're not already using a plugin for navigating CTAGS, I recommend Tagbar.
I wrote a gem to do just that: https://rubygems.org/gems/starscope
It parses ruby code properly and exports to ctags and cscope file formats.

Ruby on Rails command line looks weird

I just began learning RoR using the materials from railstutorial.org. When the author uses the command line, the resulting text are always nicely organized and colored. When I do the same thing, I often get "weird" characters and unorganized text as result.
http://i.imgur.com/2Q0kzwf.jpg
Example
http://i.imgur.com/mZP4SI9.jpg
My attempt to do the same
I'm not quite sure what to do to make my command line more organized like the one shown in the tutorial. Any help would be appreciated. In case you need to know, I'm using Windows 7.
It is because by default Windows command prompt doesn't know about ANSI color Sequences. You can try https://github.com/adoxa/ansicon which is supposed to make it aware. The reason the tutorial authors look like that is they are using a *nix shell that understands the color codes (probably bash or zsh on either linux or osx if I had to guess)
As Doon pointed out, the Windows console doesn't recognize ANSI sequences. As suggested your best bet right now is to use external tools.
However, since there are some pain points using most of the external tools, Ruby 2.0 will provide support for ANSI escape codes in Ruby out-of-the-box without having to depend on external gems or tools.

How can I clear the rails console history

When I run rails c and press the up key when irb starts up, I can see the last commands I entered when my app dropped to irb after encountering a debugger command for the ruby-debug gem. I would not only like to clear these commands out, but I would like it if rails c would pull the last commands I issued during my last rails console session. I think it used to do this but I'm not sure what has changed. I'm on ruby 1.8.7 and rails 3.0.3 on Mac OS 10.6.5 if that helps.
Update
Ray's answer helped me out in the interim. Recently I did a bit more digging to find out more and realized that there are a number of conflicting issues.
IRB checks if you have a ~/.irbrc and if not falls back to /etc/irbrc as Ray mentioned. However, if you are using rvm there is another file to consider ~/.rvm/scripts/irbrc which just loads up ~/.rvm/scripts/irbrc.rb (note the .rb) if you have rvm_path set in your ENV (you should if using rvm).
Interestingly while ~/.rvm/scripts/irbrc.rb was based off of /etc/irbrc they are not the same and differ in a few ways. The most obvious way and easiest way to detect which one is being used on your system is their history file's name. If /etc/irbrc is being used your history file will be ~/.irb_history where as rvm's is ~/.irb-history (Note: _ vs -).
Hopefully this additional information will help you determine what you need to setup your system as you would like.
Pry Concerns
I've since stopped using debugger and have moved to pry-byebug which includes the pry gem. Pry is an alternative to IRB but can also be used along side and within it. The reason I was able to provide the above update is because I was trying to figure out how to keep their respective histories separate. For more information please see my answer to the SO question on "why does pry history keep cloberring irb history?". I've included links there to the known Github issue for Pry as well as my attempt to fix it.
I interpret you question as asking how to turn history on in the Rails Console and off in the Ruby debugger. If this isn't true, please clarify.
IRB, and by extension, the Rails Console, read from ~/.irbrc, or if that doesn't exist, /etc/irbrc, to startup and configure irb. Your history is typically written to ~/.irb_history, but that is dictated by the contents of your irbrc file. The /etc/irbrc on my Mac OS X is set up to write the history from irb, so perhaps you've created a local .irbrc that doesn't have history, or perhaps you have a syntax error in that file.
The debugger reads a file called .rdebugrc on startup. You can turn off history in debug by adding this line to ~/.rdebugrc:
set history save off
Turn it back on with:
set history save on
You could also set your debug output to go to a different file than irb reads from with the command:
set history filename
These also work from the debug prompt, but aren't persistent.
There are a number of tools to help improve the irb experience. Bond and hirb are promising.
Here is Comprehensive list of Irb Tools and some tips on directly editing the .irbrc file.
Hope this help!
Although a very old question I got here by google.
Turns out RVM slightly changed over time.
Currently my IRB history (using rvm) is stored here:
user#host:~$ ls ~/.rvm/rubies/ruby-2.4.2/.irbrc*
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc_history

Ruby command line MVC framework?

I'm looking to write an app for the shell, *nix mostly. And I'm currently in love with Ruby, especially the 'rails way'.
So if there was a framework that applied rails like concepts to the commandline in ruby then that would be really fantastic.
I'v allready looked into SimpleCommand and Hirb, nothing quite what I was looking for.
To elaborate:
What I'm really looking for is a way to use a rails like (directory and application) structure to create a MVC command line application. So basically something like rails that doesn't respond to http, but instead reads and writes to the console.
Its not a shell, if i wanted that then irb works fine. It would be more like your options are A,B,C and they would work a bit like http links.
You should take a look at boson and hirb [2d] menus. First one for creation of commands, second one - for A,B,C options and custom/dynamic views for outputting data.
Maybe you should try script/console in your rails app. Is that what you wanted?

Resources