Vim’s powerful navigation

I was reading about the Mac OSX Terminal app and somebody complained about vim being slow. A vim programmer shed some light on how to use vim’s amazing keyboard navigation:

Using Vim in Terminal.app, I can easily scroll about 5100 lines in 10 seconds, using ctrl-F or ctrl-B (scroll one page forward or back), if you’re trying to move a long distance in a hurry. I get the same speed in MacVim (which can update the screen much faster than Terminal.app), so I’m pretty certain that the limiting factor (at least in MacVim) is the keyboard repeat rate, not Vim’s ability to update the screen.

The only way I get roughly 120 lines in 10 seconds, is scrolling Vim (or MacVim) line-at-a-time by holding down “j” or the down arrow, and that’s clearly the default keyboard repeat rate (about 12 per second) limiting things.

You can easily adjust the keyboard repeat rate in System Preferences, in the Keyboard pane, by fiddling with “Key Repeat Rate” and “Delay Until Repeat”. But I’ve been using Vi/Vim/MacVim for a loooong time, and have never felt the need to have the keyboard repeat faster; the key (no pun intended) is to use Vim as it was intended…

If you want to move 3 lines down (or up) hit “j” (or “k”) 3 times (much faster than waiting for key repeat to kick in). If you want to move 10 or 20 lines down, then maybe hold down “j” (“k”) and let it repeat. But if you want to go further, that’s the wrong way: ctrl-D (ctrl-U) will take you half a screen Down (Up), ctrl-F (ctrl-B) will take you a full screen Forward/down (Back/up)… holding any of these down will get you somewhere fast. If you’re trying to get to somewhere in particular: “]]” (“[[“) will jump forward (backward) to the start of the next (previous) function (in C/Obj-C/Perl/Java/Javascript or any other language where “{” by itself at the start of a line indicates the start of a function), or use “/” (“?”) to search for some text and “n” to jump to subsequent occurrences until you reach the right one. Or use tags (see “:help tags”), build a tags file for your code, then you can use “:tag yourfunctionname” to jump to a function definition in any file, or sit on the function’s name in a call somewhere and hit ctrl-] to jump to the definition (and ctrl-T to jump back to the call). Or even more lazily, in Vim, if you sit on any identifier and hit “*” (“#”), Vim will select that identifier as search text and take you to the next (previous) occurrence, where you can use “n” to keep searching further — this is more than sufficient in smaller files. And turn on incremental searching and search highlighting (“:set incsearch hlsearch”, worth putting in your ~/.vimrc), then the search results will be more obvious on the screen.

Finally, nearly everything in Vim (and Vi before it) takes a repeat count argument. If what you want is really to jump 179 lines down, then type “179j” (and if you wanted to get to line 423, that’s “423G”). Use the full power of Vim as it was intended, and keyboard repeat rate will never be a limiting factor.

And if you’re using Vim on the Mac extensively, you owe it to yourself to check out MacVim — you can still start it from Terminal.app (an included helper script lets you type “mvim file1 [ file2… ]” at a command line prompt), but it’s much more Mac-like — cut’n’paste and drag’n’drop interact well with other programs, you get real scroll bars (drag those to move through a file really quickly), multiple editing windows, and you can use the mouse for selecting text, along with (practically) unlimited colors for color syntax highlighting (you do use that, right? “:help syntax” or just “:syn on” to switch it on), instead of the 16 colors you’re limited to in Terminal.app. And it’s fast.

Vim (and MacVim) is the ultimate programmer’s editor, if you take the time to really learn how to use it — it requires the smallest amount of hand movement to make changes (especially since one never has to reach for the mouse), it’s insanely customizable/programmable, and it has a very smartly laid out command set — yes, it has a steep learning curve, but for something that I use 10 hours a day, I’ll take easy/powerful to use over easy to learn any day.

Cheers,
Carl

Source: MacRumors

Titanium Mobile integrating Admob/Smaato with Android on Mac OSX

Titanium doesn’t have Admob/Smaato support, so it is really hard to monetize free applications without using Webview Onclick Trickery (even then you’re not utilizing the full potential of Admob’s seamless integration). Thankfully, David Ashwood has released source code for Admob and Smaato Integration on his (branch). Unfortunately, the instructions in his posts are clear but not working (some only work on Linux), however I’m using Mac OSX for development, so it doesn’t help me much. Although I had learned java jar files are platform independent, but they didn’t work in my project (but that’s topic for another day). So I figured I’ll give a broad overview of how to get it working:

Note: I’m not sure if each of these steps are necessary, but this is the way I went on doing it, so you’re welcome to ignore any step that you feel is redundant. But please do leave a comment specifying it, so in the future other developers may not face the same problems.

  1. Follow Titanium Documention to get Titanium built from source (for practice and setting up paths/classpaths that you need later)
  2. Install Android SDK and Google APIs (if you haven’t already)
  3. Clone dasher’s fork
  4. Checkout the branch
  5. If using scons, edit the android/build.xml file to fix the two paths: android.platform and google.apis. They point to the older way If using Eclipse, you can specify these two CLASSPATH variables: ANDROID_PLATFORM and GOOGLE_APIS (as the Titanium Documentation to build from Source (Step 1) suggests).
  6. Run scons or Eclipse to generate jars. I got a zip archive at the end of the process in distribution folder. (I don’t know if Eclipse does it, because I did it through scons. I would argue the regular eclipse build does it as well.)
  7. Unzip this archive in your Titanium application directory, where the following directory structure exists: mobilesdk/{platform}/{version}. Be careful if replacing files/folders. I created a new directory for the {version}, because if this build doesn’t work for whatever reason, you’ll have an option to fallback on the original Titanium version.

And amazingly, you have Admob support in 1.4.1.1 (the latest version for Mac OSX).

Last tidbit: I noticed that moving these jars are very easy, just be sure to carry the other jars along with you, like the admob-sdk-android.jar and SOMAAndroidSDK.jar. I’m not sure if you need these, but I got rid of some crashes that were happening earlier.

Javascript/JSON: Find Index in an Array of Objects

JSON is one of my favorite data models notation (xml = :(). A lot of times, I get arrays of objects (hash tables) that I then have to search again an again to find the index of the object inside that array. Here’s a quick function to find it:


/*
function findIndexByKeyValue: finds "key" key inside "ob" object that equals "value" value
example: findIndexByKeyValue(students, 'name', "Jim");
object: students = [
   {name: 'John', age: 100, profession: 'Programmer'},
   {name: 'Jim', age: 50, profession: 'Carpenter'}
];
would find the index of "Jim" and return 1
*/

function findIndexByKeyValue(obj, key, value)
{
	for (var i = 0; i < obj.length; i++) {
		if (obj[i][key] == value) {
			return i;
		}
	}
	return null;
}

Mplayer’s useless warnings at startup

Installed an mplayer rpm from RPMForge repository (mplayer.i386 1.0-0.40.svn20090711.el5.rf) to my CentOS 5 server. I was setting up a bash script to decode and encode several media files. Everytime mplayer was called from the script, I would get the following useless warnings:

MPlayer SVN-r29417-4.1.2 (C) 2000-2009 MPlayer Team
Can't open joystick device /dev/input/js0: No such file or directory
Can't init input joystick
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

It was building up my logs. So I put the following two lines in my mplayer configuration file at /etc/mplayer/mplayer.conf:
nolirc=yes
nojoystick=yes

And like linux magic, the errors on each load disappear.

Import error while building Eclipse Plugin in a Java Project

I was trying to build a Java project I got from somebody using the Eclipse Gallileo install. Since the workspaces were different, my Eclipse install was confused about the eclipse plugins jar files, and it threw the following error:
"The import org.eclipse cannot be resolved"
on line: import org.eclipse.core.runtime.Plugin
and other cryptic errors: "BundleContext cannot be resolved to a type"

I resolved this by adding the appropriate jar library files from my eclipse directory. Here’s how I searched which files I needed.

  1. Go to eclipse plugins: cd eclipsedir/plugins
  2. For each suspecting jar, I do jar -tvf filename.jar | grep "searchingforthisClass"
  3. In the end I found my Plugin class: jar -tvf org.eclipse.core.runtime_3.5.0.v20090525.jar | grep Plugin
  4. Right click on the project in Eclipse, choose Properties, under Libraries tab, click “Add External JARs” button, then just locate that jar that you found above.
  5. Rebuild

PS: This is more of a reference for myself than for other Java developers, because this is probably as basic as it gets.

DholCutz Bhangra Radio iPhone OS App v1

DholCutz Bhangra Radio iOS App is out now! Get it from the Apple App Store.

This is the same application as the Android App for DholCutz Bhangra Radio I blogged about a few days earlier. So check out the screenshots on that page.

Note: The apps were made on the same day, but due to the approval processes for the Android Market and iOS AppStore being vastly different (1 day as opposed to 2 weeks, respectively), they have been published online on different dates.

Mercurial/Bitbucket: How to get a Repository name with a Space

I had iPhone mobile development code on my Mac OSX and I wanted to commit it to an online repository, Bitbucket (just like Git’s GitHub, except it is for the Mercurial DVCS).

So I created a private BitBucket repository with the name “Test App”. Surprisingly, BitBucket renamed my basename to “test-app”. However my folder structure was:

Current Folder Structure:

"Test App" (notice it has spaces)
  -- File 1
  -- File 2

Now I can just as easily clone the “test-app” repository and change my folder structure to this:

Possible Solution’s Folder Structure:

test-app
  -- .hg/*
  -- "Test App"
    -- File 1
    -- File 2

But that means I have to change my application file/folder paths. (Although I will note that a sane developer would use relative paths, I unfortunately did not have that option.) Surprisingly, this is not documented in the BitBucket documentation or on any online resource.

Possible solutions I looked at:
– “Hard” Symbolic Links – Unfortunately Mac OSX does not support hard linking to directories.
– Use a script to copy/paste code from “Test App” to test-app, each time I need to check it in – Highly inefficient

Solution
I cloned “test-app” to a folder. So I got:

Output of hg clone:

test-app
  -- .hg/*

Now I took the .hg directory and just moved it to my “Test App” folder, like this:

Final Folder structure:

"Test App"
  -- .hg/*
  -- File 1
  -- File 2

Thankfully, there are no problems with hg commands. Apparently, the Mercurial coders are not hardcoding paths. 🙂

Upgrading WPMU 2.x to WordPress 3.0: Multisite Problems

When upgrading WordPress MU installation to a WordPress 3 multisite installation, I faced a lot of “Internal Server Error” problems, such as:

Premature end of script headers: *.php
an error occurred while processing this directive

A lot of people suggested disabling plugins before, or even removing them all from the /wp-content/plugins/ folder. But one crucial detail was missing from most of these suggestions. WPMU keeps a special set plugins for itself in the folder /wp-content/mu-plugins/. It is very important to disable these, because chances are these are the most “incompatible” plugins that you have (because they are not updated like the regular WordPress plugins).

Python Scraping: Scrapy and BeautifulSoup

When I search for solutions to my problems, I often search the internet for “compare and contrast” or analytical posts on the best tools for the job, which in turn help me make an informed decision.

Recently, my problem was scraping a website for data using python. I searched online and a lot of users recommended Scrapy over BeautifulSoup. Well, that was easy, I naively said. Scrapy probably is the better option for most people (it supports XPath right out the box). Like Scrapy’s docs put it:

comparing BeautifulSoup (or lxml) to Scrapy is like comparing jinja2 to Django.

But Scrapy didn’t settle well with my Cent OS platform (or Google Apps Engine). For one, there were a whole lot of problems trying to install Scrapy in my virtualenv (safe python environment system) because of it’s dependency on libxml2/libxslt and their bindings. Examples:


etree.so "undefined symbol: libiconv"
Version 2.6.26 found. You need at least libxml2 2.6.27 for this version of libxslt
ImportError: /pyenv/test/lib/python2.6/site-packages/libxml2mod.so: undefined symbol: xmlTextReaderSetup
No module named libxml2
Failed to find headers. "update includes_dir"

Note: This may look overly dramatic. And it maybe is a little dramatic, because a lot of these errors/problems do have solutions. Most of them can be searched out of Google results.

I endlessly chased solutions at trying to integrate libxml2, libxml2 python bindings, libxslt and lxml in a virtualenv (with python 2.6; note Cent OS/RHEL only have python2.4 in their repositories). I eventually grew tired of trying to find what is linking to what shared library and what seems to be the missing culprit. And I figured, let me just give BeautifulSoup a try. I thought I’d spend the extra time learning the library that BeautifulSoup is, as opposed to learning the “framework” that Scrapy is.

In the end, BeautifulSoup was not that hard. It may be missing XPath support in its default setup, but I could easily implement the XPaths that I had with ones using BeautifulSoup syntax.

Lesson: Don’t let your ego get into it. Save time by going for fairly-efficient solutions that can be implemented in fairly-optimal time (as my Algorithms professor used to say).

Convert Django MySQL Database Tables to Unicode

When I created a Django application, I hadn’t noticed that my MySQL was defaulted to latin character set (probably by Virtualmin or CentOS’s default MySQL values). So I didn’t want to delete my current project and start again. So here are the commands to convert a database to unicode:

for the database

ALTER DATABASE djangodb CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

on each table do

ALTER TABLE djangotablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci