On Unix systems – Linux and OS X included – file names can be arbitrary binary data with very few limitations. This means that in order to make sense of the name a character encoding must be used. Recently UTF-8 has become the default encoding on many systems, but sometimes you have to deal with files originating from older systems with names in other encodings. These files are a problem for Java programs because java.io treats file names as strings of unicode characters rather than bytes, and is unable to open files with with incorrectly encoded names.
Fixing Doubly UTF-8 Encoded Text in MySQL
Earlier I wrote about fixing UTF-8 encoded text stored in a latin1 column by converting it back to latin1. What if you want to convert the column to UTF-8 instead? Changing the storage encoding is easy to do, it takes just one ALTER TABLE statement and MySQL converts the column to UTF-8. But afterwards you notice that the text that was encoded in UTF-8 now looks very weird: á has been turned into á, ä into ä… and “you’re” into “you’re”.
K-means Clustering in MySQL
Clustering is about finding data points that are grouped together. K-means clustering a fairly simple clustering algorithm. Here we’ll show how to do it in MySQL.
Continue reading “K-means Clustering in MySQL”An Interesting Golden Ratio Identity
Lately I’ve been intrigued by the Fibonacci number sequence, i.e. the sequence where each number is the sum of the two previous.
Continue reading “An Interesting Golden Ratio Identity”Your Bash Prompt Needs This
Have you ever had this happen to you? You start a program and then interrupt it with Ctrl-C, and Bash prints its prompt after the ^C you have typed:
prompt$ a very long command
^Cprompt$ █
Then you hit the Up key to retrieve very long command from the history and try to edit it, only to discover that the text you see on the screen is shifted by two characters from where it appears to be, and you can’t see where you’re typing anymore. A major annoyance!
Here’s how you can fix it: make the prompt go to the first column always. To do this, include \033[G in your $PS1:
PS1="\[\033[G\]$PS1"
This code happens to be the ANSI escape code for moving the cursor to the first column. Your prompt will now start from the first column and write over the ^C you typed. The \[ and \] on the other hand are needed so that Bash does not count these movement codes when calculating the length of the prompt.
The Bash Prompt HOWTO is a great resource if you want to learn more about how and why to customize your prompt.
Update: Wow, I never expected this would spark such a lively discussion on Hackernews! People there suggest many alternative solutions to this problems, such as simply hitting Ctrl-C or Enter to get a clean line whenever this occurs, configuring a two-line prompt, or ditching Bash for Zsh.