Console Tips

Console Newb?

Maybe these links will help you:

You should at least know:

  • If you see something like this: "$" or this "$>" when referencing to a command to be run on the CLI, it means you must run it as normal user, as opposite of "#" or "#>" which means you should run as super user.
  • Changing directories:
$> cd directory
  • Creating an empty file:
$> touch /somewhere/file.rb
  • Deleting a file
$> rm file.rb
  • Moving a file:
$> mv file.rb /home/me/
  • How to identify your current directory in the console
$> pwd

Show ActiveRecord queries inside Rails console

The idea is to make ActiveRecord's logger to print output to STDOUT instead of a file. To do so we need to run this:

ActiveRecord::Base.logger =

So, a sample session would be like this:

>> ActiveRecord::Base.logger =
=> #<Logger:0x103351d40 @logdev=#<Logger::LogDevice:0x103351ca0 ...>>
>> Ticket.find_by_role_id(Role.last)
  SQL (0.1ms)   SET NAMES 'utf8'
  SQL (0.1ms)   SET SQL_AUTO_IS_NULL=0
  Role Load (0.3ms)   SELECT * FROM `roles` ORDER BY DESC LIMIT 1
  Ticket Columns (1.9ms)   SHOW FIELDS FROM `tickets`
  Role Columns (1.2ms)   SHOW FIELDS FROM `roles`
  Ticket Load (0.3ms)   SELECT * FROM `tickets` WHERE (`tickets`.`role_id` = 47) LIMIT 1
=> #<Ticket id: 1, created_at: "2010-05-03 21:09:41", updated_at: "2010-05-03 21:09:41">

Fast access to a project's directory with autocompletion

You need to define two bash functions (for example in /home/you/.bashrc, /home/you/.bash_profile or /home/you/.profile):

p() {
_p_complete() {
  local cur opts
  opts="$(ls $PROJECTS_DIR)"

  COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
  return 0
complete -F _p_complete p

Remember to change PROJECTS_DIR to your own project's directory.

You can also change the function name, for instance, instead of p() you can type project and so. Just be sure to replace the function name in both places: the definition itself (first code block, second line), or in the auto_complete declaration (second code block, last line).

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License