Learning Vim – Part 3

Wow – it’s been a long time since my last post on my journey into the world of learning Vim. Since then I’ve started a new job at the wonderful Mozilla as a mobile software engineer and they have a really messed up complicated build system, so even though I’m working on an Android project, I haven’t got the pleasure of being able to use any of the IDEs that I’ve grown to love. The majority of people I’m working with use Emacs or Vi(m), so it’s been a pretty good chance for me to get my learn on!

The first thing I want to go over is tabs – if you’re going to use Vim to develop in then learning your way around the tab commands are essential.

First things first – to open several files from the command line in tabs:
vim -p file1 file2 file3

Next are a few commands to open files in tabs and switch tabs:

  • :tabe <filename> → open file in new tab
  • :tabp → switch to previous tab
  • :tabn → switch to next tab

That’s pretty good, but it makes it a bit of a job to navigate quickly, so below is how to setup a mapping so we can quickly access these keys. I’ve setup these up as two key mappings to reduce the chances of anything conflicting with the new setup. Open up the file ~/.vimrc and at the bottom paste these lines:

map <c-m><c-n> :tabp<CR>
map <c-m><c-p> :tabn<CR>

The <c-m> is the access modifier, it means press the ‘Ctrl’ and the ‘m’ key and yes the ‘m’ is for Martyn :) . After that, with the ‘Ctrl’ key still held, press either ‘p’ or ‘n’ for previous and next tabs. The <CR> at the end is required to mark the end of that command.

Next up is better navigation:

  • w → go to start of next work
  • e → go to end of current/next word
  • b → go to start of current/previous word
  • b → go to start of current/previous word
  • 0 → go to start of the current line
  • ^ → go to start of the content in the current line
  • $ → go to the end of the current line

Sticking a number before each of these will repeat the command, so 2w will move you to the start of the next word twice, 2b will move you to the beginning of the previous word.

Searching is quite important, and fairly easy:

  • /<SEARCH TERM> → find the search term in the current file
  • n → after you have searched, this will go to the next occurrence

Search and replace is also essential and again pretty easy:

  • :s/SEARCH TERM/REPLACE TERM/ → replace the next occurrence of the SEARCH TERM in the current line with the REPLACE TERM
  • :s/SEARCH TERM/REPLACE TERM/g → replace every occurrence of the SEARCH TERM in the current line with the REPLACE TERM
  • :%s/SEARCH TERM/REPLACE TERM/g → replace every occurrence of the SEARCH TERM with the REPLACE TERM

Guest blog on Safari books dev blog

I’ve been published!!!

A few months ago I was approached to write a few blog articles for the Safari Books dev blog. Today the first of my articles has gone live : Android 4.1 Jelly Bean Notifications

If you’ve come from there, hello! If you haven’t, go and have a read – let me know your thoughts.


Accepting incoming tcp ip connections in (K)ubuntu

I’ve recently spent a bit of time trying to work out why I could accept incoming connections on my Kubuntu laptop. Initially I tried to get Charles to work but couldn’t get it to see any traffic coming from my Android phone – I put it down as a bug in Charles and left it. Today I tried to get Synergy working and again nothing, which prompted me to investigate further. I know Synergy was working on my laptop as a client, but I was trying to make it a server today, and it wasn’t showing anything being able to connect. This sounded very much like I was behind a firewall – but to the best of my knowledge I don’t have a firewall on my laptop. Anyway, turns out the ports were closed and the following command allowed me to open them (24800 for Synergy, Charles works on port 8888):

sudo iptables -I INPUT -p tcp --dport 24800 -j ACCEPT


Messages from the cloud with GCM

In May 2010 Google released a monumental update to it’s Android OS with the release of Android 2.2 (Froyo). Alongside the quite ridiculous improvement in performance over the previous version AND a host of amazing extra features (JIT compilation, USB tethering, Wi-Fi hotspot, an improved browser to name but a few) was the announcement of the Cloud to Device Messaging (C2DM) service, or to use it’s more commonly known name – push notifications. Push notifications systems have fast become an industry standard in the world of advanced mobile computing, allowing a central server to push data to individual devices, rather than the power hungry alternative of continually polling a server for updates.

Announced at Google I/O 2012 was the next iteration of C2DM, now called Google Cloud Messaging (GCM). Along with the moniker change, there are a few key changes between C2DM abnd GCM which add functionality and make it easier to use. Of note:

  • Multicast messages: you can broadcast messages of up to 1000 registration IDs at the same time from a single request
  • Multiple senders: messages to the same app can be sent by multiple parties thanks to a change in the way registration is handled
  • Time-to-live (TTL) messages: messages can have a TTL value of between 0 to 4 weeks helping to ensure messages about events that expire don’t reach your users after the fact. The GCM servers will throttle the messages to prevent notification spam. Messages that can’t be instantly delivered will be stored by the Google servers until they are delievered or expire, messages with a TTL of 0 aren’t stored anywhere and also aren’t subject to any throttling.
  • Payload: messages can bundle up to 4Kb (up from 1024 bytes with C2DM) of data to be delivered with the message, up to 100 of these messages can be stored on the GCM servers if they can’t instantly be delivered, after which you’ll receive an error code which your server will have to deal with.

Google states that C2DM will be maintained for moment but won’t accept any new users nor grant new quotas. They also point out that ‘C2DM and GCM are not interoperable. For example, you cannot post notifications from GCM to C2DM registration IDs, nor can you use C2DM registration IDs as GCM registration IDs. Your server-side application must keep keep track of whether a registration ID is from C2DM or GCM and use the proper endpoint.’ fortunately they provide easy to follow instructions as to how to migrate your apps from C2DM to GCM and google has even provided sample GCM apps (both client and server side) for you to look at (something which was sorely missing with C2DM)

If you haven’t yet used Google’s cloud messaging service, it’s now easier than ever – full instructions on getting started with GCM are given on the developer website. Just remember that it requires devices running android 2.2 (sdk version 8) or later.


Android styling tips

One area that has always been a bit lacking for Android developers is the online documentation at developer.android.com. It’s getting better with each new release but is still far from perfect – one area which has left many developers lost is styling. There are now plenty of guides on the internet discussing styling and theming but below are the two tips I wish someone had told me when I’d started Android development:

1, ‘?’
The question mark in a style refers to a dynamically defined theme value. In the following example, taken directly from the Android styles.xml file, the value of ‘android:textColor’ refers to the value of the dynamically defined “textColorPrimary” value:

<style name="TextAppearance">
<item name="android:textColor">?textColorPrimary</item>
<item name="android:textColorHighlight">?textColorHighlight</item>
<item name="android:textColorHint">?textColorHint</item>
<item name="android:textColorLink">?textColorLink</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
</style>

As can be seen from the following excerpt from the android-16 themes.xml file, the exact value of the ‘textColorPrimary’ within ‘TextAppearance’, and hence the default text colour throughout the app, will vary depending on the theme applied on your application or activity in the AndroidManifest.xml file:

<style name="Theme.Light">
...
<item name="textColorPrimary">@android:color/primary_text_light</item>
...
</style>
<style name="Theme.Holo">
...
<item name="textColorPrimary">@android:color/primary_text_holo_dark</item>
...
</style>

So if we were to define our own theme and wanted to change the default text colour – we can easily override the above value in order to achieve that throughout all parts of our application which used our custom theme:

<style name="CustomTheme">
...
<item name="android:textColorPrimary">@color/custom_primary_colour</item>
...
</style>

2, Read the source code
This is a general tip really, but enough emphasis can’t be placed on how much you will learn by reading the Android source code, which can be downloaded via the SDK Manager. To find the style and theme code, find where you installed the Android SDK and have a look at styles.xml and themes.xml in the <android-sdk>/platforms/android-XX/data/res/values/ directory.

Really! You’ll learn more by looking at, studying, and playing about with the Android style and theme source code than any guide can ever teach you! For example, there’s no comprehensive documentation of what styles are available to change, but have a look at the default theme (search for ‘<style name=”Theme”>’ in the theme.xml file) and you’ll find out how to style every single widget in Android and lots more. This is the best form of guide and documentation rolled in to one – it’s always going to accurately reflect the live code and every single bit of style information is in there. Only the experience of using it will help you get a deeper knowledge of how it works. So…what are you waiting for?


Time to update your support library

Fragmentation is an often cited issue of the Android ecosystem, but the people who really bear the brunt of it are not so much the end users, but the developers who have to create applications to support multiple versions of the platform. It’s for this reason Google created the Android Support Library – used to provide backwards compatibility for newer features of Android.

Originally released in March 2011, just after the introduction of fragments in Honeycomb (Android 3.0), the support library (originally called the “Android Compatibility package”) provided developers the means to use Fragments, LoaderManagers and a few other classes across mostly all versions of the platform, going back to Donut, version 1.6. This library, combined with the excellent ActionBarSherlock , gives developers the means to write a single codebase which can support multiple versions of the platform without having to write platform specific code – the only exception that comes to mind being the ActionBar action view widgets which need specific pre-ICS implementations.

Fast forward a couple of years to Google I/O 2012 and the, now named, Support Library is on revision 9 and has brought with it a lot of updates. Google is slowly implementing functionality which has, up until now, been developed by other people. For example, revision 9 includes a lot of bug fixes and new functionality to the ViewPager class, however note currently the ViewPageIndicator library still offers better functionality. The Notification Builder has also been updated, but again note that a third party library, in the form of the NotificationCompat2 library, is still the recommended way to provide complete support for notifications across all platform versions without sacrificing the newer functionality where available.

Of particular interest, and the reason for the title of this blog post, are the ‘many bug fixes’ for the Fragment class, which by now should be core building blocks used by all Android developers (if you aren’t using them yet – what are you doing?!) – this in itself should be reason enough to update.

Google seems to update Android everytime it release a new Nexus device, and if the rumour-mill is correct then we should be seeing a new Nexus handset at some point towards the end of the year. If past updates are anything to go by, Google should be releasing another updated version of the Support Library sometime in August 2012 or shortly after. It’s strongly adviced to keep an eye on the Support Library updates and implement new versions as soon as they’re released in order to take advantage of the new features and bug fixes.


Android UI: CoinKeeper

https://play.google.com/store/apps/details?id=com.ifree.coinkeeper


Good Android UI

I’m going to start keeping a record of nice Android UI that I find.

First one up is Safe In Cloud

https://play.google.com/store/apps/details?id=com.safeincloud


How to get ADB to work with the Lenovo Thinkpad Tablet in Windows 7

Lenovo has a dedicated driver for the Lenovo Thinkpad Tablet, so it won’t work out of the box.

Here’s the steps to get it working:

  1. Download the ADB Interface Driver – ThinkPad Tablet from http://support.lenovo.com/en_US/downloads/detail.page?DocID=DS022366
  2. Turn on the ThinkPad Tablet.
  3. Press the slider bar icon to display the Settings property page.
  4. Press Applications on the left side of the page.
  5. Press Development and check USB debugging.
  6. Press the OK button on the dialog.
  7. Connect the Tablet to the computer.
  8. Open Device Manager.
  9. Expand the Other devices node.
  10. Right click the ThinkPad Tablet node and select Update Driver Software.
  11. Select Browse my computer for software.
  12. Browse to the folder that contains the driver files.
  13. Select the driver folder and continue the driver installation

Learning Vim – Part 2

After a few days of using Vim, I’m starting to feel more comfortable navigating around and editing files. I’m missing the ability to manipulate areas of text, and to repeatedly find patterns once I’ve searched for them, but I assume that’ll come later.

The commands I’ve been using recently are:

  • o → insert after the cursor
  • 0 → go to first column
  • ^ → go to first non-blank character of the line
  • $ → go to the end of line
  • /pattern → search for pattern
  • <ctrl + r> → redo
  • :e → open
  • :w → save
  • :bn or :bp → show next or previous file

I’ve had a little look ahead at a few of the other things that vim can do, and it looks really powerful, but it’s not at all intuitive, so there’s going to be a fair amount of learning to be done before it’s my main text editor!


Switch to our mobile site