Change the color of a <hr> in all browsers

The following Sass will do it:

hr
  color: #ddd
  background-color: #ddd
  border: none
  height: 1px

Order for SELECT ... IN (5,100,23) queries

When doing a query like this:
SELECT id FROM users WHERE (users.id IN (899,1084,1095,100,2424,2429,2420))

the order of the returned records is undefined. To force the query to return the records in a given order, you have to add ORDER BY FIELD(id, 899, 1084, ...)

So the query looks like this:
SELECT id FROM users WHERE (users.id IN (899,1084,1095,100,2424,2429,2420)) ORDER BY FIELD(id,899,1084,1095,100,2424,2429,2420);

Automagical ?-methods for boolean attributes in ActiveRecord

This method is defined automatically and only given for boolean attributes

create_table "users", :force => true do |t|
  t.boolean  "email_confirmed"
end

# you may now say:
User.email_confirmed?

Only allow pictures as Paperclip attachments

validates_attachment_content_type :image, :content_type => /^image\/(jpg|jpeg|pjpeg|png|x-png|gif)$/, :message => 'file type is not allowed (only jpeg/png/gif images)'

Forward HTTP through an intermediary server (Local Port Forwarding)

This will tunnel HTTP requests to one given domain and port through an intermediary SSH server:

ssh -L 8080:targethost:80 tunnelhost

http://localhost:8080 will now connect you to http://targethost:80, tunnelling all data through tunnelhost via SSH.

Note that the connection between tunnelhost and targethost will still be unencrypted in this example.

Change default size of Gnome terminal

Open the configuration file:

gksudo gedit /usr/share/vte/termcap/xterm

Find a line like this:

:co#80:it#8:li#24:\

Change the first and last number to your desired columns and rows:

:co#160:it#8:li#40:\

Save your changes and close all open terminals. New terminals should now open with the new size.

Copy a file over SSH

Ubuntu lets you mount an SSH shell into Nautilus from Places -> Connect to server (select "SSH" as server type).

In order to copy a file over SSH from a shell:

scp filename username@remotehost:

The trailing ":" directs the file to username's home directory on the remote host.

You can also copy a file from the remote host to your local machine:

scp remotehost:remotepath localpath

Using screen for long running tasks

Screen is great for long running tasks, where you want to log in every few hours and days and keep your session alive indefinitely.

To create a new screen, do

screen -Rd screenname

This will also reattach an existing screen with that name.

Once you're in the screen, CTRL+A will bring up the menu, d detaches the screen. You can close your shell, screen and all applications running inside of it will remain alive.

Scrolling inside a screen

  • Bring up the menu with CTRL+A
  • Press Esc
  • You can now scr...

Dumping and importing from/to MySQL in an UTF-8 safe way

In a nutshell: to avoid your shell character set from messing with imports, use -r to export and SOURCE when importing.

Dumping safely

# Do not do this, since it might screw up encoding
mysqldump -uroot -p database > utf8.dump # this is bad

Better do:

mysqldump -uroot -p database -r utf8.dump

Note that when your MySQL server is not set to UTF-8 you need to do mysqldump --default-character-set=latin1 (!) to get a correctly e...

UTF-8ify an existing MySQL database

First do

ALTER DATABASE database_name CHARACTER SET "utf8";
ALTER DATABASE database_name COLLATE "utf8_unicode_ci";

After that, for each table:

ALTER TABLE table_name DEFAULT CHARACTER SET "utf8" COLLATE "utf8_unicode_ci";

This just changes the default character set / collation for each table. To convert them, you need:

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

Count files in a folder

From the shell:

ls -l | wc -l

Error installing the raspell gem

When you get this while installing the raspell gem:

ERROR:  Error installing raspell:  
ERROR: Failed to build gem native extension.

You need some libraries:

sudo apt-get install libaspell-dev

Aspell Error - No word lists can be found for the language XY

When you get this error:

No word lists can be found for the language "de".

An aspell dictionary is missing. Install it with

sudo apt-get install aspell-de

Get TinyMCE editor content as HTML

tinyMCE.activeEditor.getContent()