Lazy Hacker Babble

Just some random babblings from a lazy hacker…

Archive for the 'Programming' Category

VIM copy/paste/navigation in Insert mode.

Posted by hsin on 6th March 2010

A few months ago, I made a commitment to learn the VIM editor. I was primarily an EMACS user, but I wanted to become proficient with VIM since EMACS is not always available I found myself too slow when using VIM. To learn it, I pretty much told myself that I’m not allowed to use any other editor while programming and just immerse myself in VIM. For the most part, it worked. I’ve been using VIM daily for about 4 months now and I’ve gotten used to it… mostly.

I can see the reasoning and benefits of having a separate mode for inserting text, but for me it isn’t as intuitive to use. Maybe my fingers are just to conditioned to be able to write chunks of text and still be able to copy, paste, etc. The way I work, I edit text while jumping around a lot and that mode leads to a lot of hitting ESC to the point where I’m using more key strokes then on EMACS (where the criticism is that it requires mult-combos to do an action such as ‘C-x C-c’). So I found myself editing text, ESC, move around, ‘i’, edit text. For the most part, I was willing to live with that, but the breaking point is that I just kept making mistakes editing. I’d hit ‘i’ when I’m already in edit mode or be typing outside of edit mode leading to typos and jumping to another part of the screen. I figure that 4 months is enough and while I’ll stay with VIM, I’ll likely be in Insert mode a lot and so I want to reduce bouncing back-and-forth between modes.

I made the following changes to my .vimrc to allow me to do things like move around (without using the arrow keys) and copy/paste without leaving Insert mode:

" Key mappings in INSERT mode
" Tired of lifting hand to hit ESC or having ctrl-[
imap ;; <Esc>

" navigate without lifting hand off of keys
imap <C-h> <Left>
imap <C-j> <Down>
imap <C-k> <Up>
imap <C-l> <Right>

" paste in Insert Mode
imap <C-v> <C-o><S-p>

" undo
imap <C-u> <C-o>u

" Select text
imap <C-c> <C-o>vgG
 

Posted in Main, Programming | No Comments »

Android 2.1 SDK released

Posted by hsin on 12th January 2010

For those who has been asking for the 2.1 SDK since the Nexus One launch, the new SDK can be downloaded here: http://developer.android.com/sdk/android-2.1.html

Posted in Google, Programming | No Comments »

Google APIs

Posted by hsin on 5th January 2010

“Eat your own dog food” is not an uncommon term in Silicon Valley. It means for a company to use the product they built themselves. If a company offers an email product to customers then they should also be using that same email product. It’s not always easy to eat one’s own dog food especially when that dog food provide a critical business functionality or is fairly new. Email, for example, has become vital for companies and they depend on it to be scalable, stable and reliable, but if you’re eating your own dog food you might be using the pre-production version which might not be 100% stable or it’s missing some functionality and which you need to build yourself. Because of this, even though many tech company talk about eating their own dog food, but don’t really pull it off.

I find Google to be very serious about eating their own dog food. Everything released to the public is also used internally and this includes their APIs. Often companies release APIs but don’t use them internally because they have direct access to the underlying technologies. Google does use their own APIs and during my first two months here, I’ve been learning a lot of the APIs in order to do my work. There are some amazing stuff out there and they are all listed at code.google.com.

Specifically, I’ve been learning the Google Data APIs. There is an amazing amount of access to APIs to allow developers to build sophisticated applications so I encourage everyone to try them out!

Posted in Google, Programming | No Comments »

Programmatically authenticating to Google App Engine with Python

Posted by hsin on 11th December 2009

Even before joining Google, I was already a fan of two of its services: Google App Engine and Google Apps for Domains. Apps itself brings a lot of value and combined with app engine, it’s especially powerful for small business and start-ups. Apps provides the most essential IT pieces for a business (email, office suite, calendaring) and Google App Engine (GAE) allows the business to build their own software on Google’s infrastructure without the heavy initial investment in hardware and IT infrastructure. This post focuses on just GAE specifically how you can programmatically access app engine from a python script.

The code to authenticate against the production instance of GAE is actually fairly well documented by Google on the GAE site. The issue I ran into is that before I push my code live, I wanted to test it on a development instance. GAE makes setting up a development server easy and comes with its own web server and datastore (database), but the one area where the development environment is different from production is how authentication is handled. The development server doesn’t do real authentication. It just sets the right cookie that simulate that you’ve already logged in. I wanted to write my code with minimal differences between how it calls production and how it calls the localhost. Authentication to your application on production means that you need to get a token from the Google servers, but this doesn’t exists on the dev environment. Looking at how the developer instance handled the login screen, I was able to do the following:

#!/usr/bin/python

import cookielib
import urllib
import urllib2

# Setup to be able to get the needed cookies that GAE returns
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)

# Set ‘prod’ based on how your system determines whether to connect to prod or localhost.
if prod:
   # This is the setup to construct the login URL for authentication on prod.
   authreq_data = urllib.urlencode({‘Email’: ‘<login_email_address>’,
                                     ‘Passwd’: ‘<login_password>’,
                                     ’service’: ‘ah’,
                                     ’source’: ‘<your_app_name>’,
                                     ‘accountType’: ‘HOSTED_OR_GOOGLE’})

    # Authentication server
    token_uri = ‘https://www.google.com/accounts/ClientLogin’

    # This is where you want to go after log in.  Replace:
    target_uri = ‘http://auth_example.appspot.com/home’
    ## Get an AuthToken from Google Accounts
    auth_req = urllib2.Request(token_uri, data=authreq_data)
    auth_resp = opener.open(auth_req)
    auth_resp_body = auth_resp.read()
    auth_resp_dict = dict(x.split(‘=’)
                          for x in auth_resp_body.split(\n) if x)
    authtoken = auth_resp_dict[‘Auth’]

    authreq_data = urllib.urlencode({‘continue’: target_uri,
                                     ‘auth’: authtoken})
    login_uri = (‘http://<location_of_prod_GAE>/_ah/login?%s’
                 % authreq_data)
else:  # authenticate against the local dev server
    target_uri = ‘http://localhost:8080/home’
    authreq_data = urllib.urlencode({‘email’: ‘test@example.com’,
                                     ‘continue’: target_uri,
                                     ‘action’: ‘Login’})
    login_uri = (‘http://localhost:8080/_ah/login?%s’ % authreq_data)

# Okay, we’re done at this point with the difference.  From this point everything else
# should be the same for either prod or dev.

# Do the actual login and getting cookies.
serv_req = urllib2.Request(login_uri)
opener.open(serv_req)

# Rest of code here.
 

That all there is to it!

Posted in Google, Programming | 2 Comments »

.vimrc

Posted by hsin on 10th December 2009

I’ve been trying to set up my VIM environment to be more comfortable for me so I can be more productive. It’s getting better although I still miss having a shell inside the editor that EMACS provide. Here is my current .vimrc:

" Enable loading filetype and indentation plugins
filetype plugin on
filetype indent on

" Turn on syntax highlighting
syntax on

" Global settings
set autowrite

" Allow backspacing over everything
set backspace=indent,eol,start

" Insert mode completion
set completeopt=menu,longest,preview

" Use UTF-8 as the default buffer encoding
set enc=utf-8

" Remember up to 100 ‘colon’ commmands and search patterns
set history=100

" Enable incremental search
set incsearch

" Always show status line, even for one window
set laststatus=2

" Jump to matching bracket for 2/10th of a second (works with showmatch)
set matchtime=2

" Don’t highlight results of a search
set nohlsearch

" Enable CTRL-A/CTRL-X to work on octal and hex numbers, as well as characters
set nrformats=octal,hex,alpha

"Show line, column number, and relative position within a file in status line
set ruler

" Show matching brackets
set showmatch

" Show line numbers
set number
" case-insensitive search
set ignorecase

" Code folding
set foldenable
set foldmethod=indent
set foldlevel=100

" Have VIM recursively search upward for tag file
set tags=tags;/

" Toggle tag list
nmap <F8> :TlistToggle<cr>
" Builds tags for current directory
nmap <F9> :!/usr/bin/ctags -R –c++-kinds=+p –fields=+iaS –extra=+q .<CR>

" Bufexplorer plugin toggle
nmap <F7> \be

" Change to directory of current working file
au BufEnter * lcd %:p:h

 

I use 2 plugins: TagList and Bufexplorer along with Exuberant-Ctags to make moving around the code easier.

Posted in Programming | No Comments »

One script with different script names.

Posted by hsin on 29th November 2009

Sometimes I want to have my bash script behave differently based on how I call it without having to set up different parameter inputs. For example, I might have a script that can sync a file from a local server to one of two remote servers. I can make my script to take in parameters so I can call it using ’syncfiles -h serverA; syncfiles -h serverB’, but sometimes I forget what my params are and it’s easier to remember ’syncToA’ or ’syncToB’.

To do this is pretty simple, yet I always have to look it up each time so I’m writing this as note to myself.

#!/bin/sh

echo $0

if echo $0 | grep "syncToA" > /dev/null
then
   # do stuff
elif echo$0 | grep "syncToB" > /dev/null
then
   # do stuff
fi
 

Next, create symlinks to the original source files with the names you chose (i.e. ’symlink syncToA syncfiles’).

Posted in Linux, OSX, Programming | No Comments »

GNU Screen and Bash

Posted by hsin on 29th November 2009

As pretty as GUIs are, sometimes it is most efficient to work in a text console which is why I find myself using GNU Screen. However, Screen’s command starts with CTRL-A which is also Bash’s move to start of line command. To have it work, use ‘ctrl-a a’.

Also, screen sets the value of TERM to be screen so if you have any settings based on the TERM value, update it appropriately.

One thing about using screen is that you lose your terminal’s scroll buffer so as line scroll past your view you can’t just use the scrollbar to move up to see what you missed. Instead, screen has it’s own scroll buffer that you can use. You can set the buffer size using ‘defscrollback #’ (where # is the number of lines) in your .screenrc or in the screen command line (ctrl-a). You can switch to the buffer with ‘ctrl-a [‘ (and exit buffer mode with ESC). Navigation follows normal VI keys.

Finally, I noticed that hitting the delete key didn’t send backspace as expected with using the OSX terminal. To correct that, add the following to your .screenrc:

termcapinfo xterm-color kD=\E[3~
 

This tip came from here.

Posted in Linux, OSX, Programming, Software | No Comments »

My VIM cheatsheet

Posted by hsin on 28th November 2009

To help me learn VIM keys, I put together a cheat sheet which I load onto my desktop much like how I handle my calendar/todo.

I’m not sure how much this cheat sheet will help others because I left out the commands that I already know well and left the commands that I’m currently learning. I expect that I’ll update the list as I get more familiar with VIM.

—-  BUFFER  —-
:e          set buffer
:b          goto buffer (next|prev)
:sp         new window/file above
:vs         new window left
:q          close current window
:qa         quit all windows
ctrl-w      move to window
ctrl+w =    autosize window
:ls         list buffer
:bd         buffer delete
:sav        save-as
u           undo
ctrl-r      redo
.           repeat
ESC         ctrl-[

—- NAVIGATION —–
gg          goto start of file
G           goto end of file
:54         goto line
80|         goto column
ctrl-g      show file info
ctrl-e      scroll up
ctrl-y      scoll down
ctrl-b      page up
ctrl-f      page down
zt          scoll line to top
w           next word (ctrl+right)
b           previous word (ctrl+left)
zi          toggle folding

—- SELECTION —-
v           visual select
shft-v      line select
ctrl-v      column select
y           copy selection
p           paste selection
=           reindent
>           indent
<           unindent
:set list!  toggle visible whitespace
xp          transpose
r           replace
s           substitue
x           delete char
 

Posted in Programming | No Comments »

Trying to switch to VIM.

Posted by hsin on 26th November 2009

I’ve always been an EMACS user on UNIX and it sometimes seems like it can do EVERYTHING. The one problem with EMACS is that a lot of production systems (and even many non-prod) won’t install it which means that when I’d have to use vi/vim and I’m just not as fluent with it. Especially since when I’m on those systems means that I’m trying to do fast debugging and navigation, being slowed down by my lack of experience with the editor is really frustrating.

I’ve decided to try to switch to VI/VIM to be the primary editor for awhile to force me to learn. There is no need to tell me that EMACS kicks butt (I agree), but unless you can get emacs installed everywhere, this is really more a decision based on my current situation.

Posted in Linux, OSX, Programming, Software | No Comments »

Nice font for terminal and code editing

Posted by hsin on 25th November 2009

Terminus is a very nice looking font that is well suited for programming editors and terminals.

terminus font

It is an open source font and many linux distributions have it available. I haven’t tried any for OSX, but you can compile for xterm on OSX/X11 from macports.

Posted in Linux, OSX, Programming | No Comments »