Registers in Vim

Registers in Vim let you run actions or commands on text stored within them. To access a register, you type “a before a command, where a is the name of a register. If you want to copy the current line into register k, you can type

“kyy

Or you can append to a register by using a capital letter

“Kyy

You can then move through the document and paste it elsewhere using

“kp

To access all currently defined registers type

:reg

Read More

A parcel from StackExchange

I had a slightly unexpected parcel arrive today, I say slightly because I knew about it a while ago but had since forgotten. I thought I’d do a small unboxing set of pictures.

2013-07-23 12.05.30
The parcel itself

2013-07-23 12.05.53
On opening

2013-07-23 12.12.37
Goodies! A couple of stickers, a couple of pens and a tshirt.

2013-07-23 12.13.33
I’m honoured to get a letter from Joel

So – just want to say thank you to the StackExchange people, very nice to have received this gift from your guys. And thank you Joel Spolsky – you made my day (and my girlfriends – she squeed when she saw who the letter was from. really!)

Read More

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

Read More

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!

Read More

Making Gmail Tap

This morning my brother posted a message on Google+ about Gmail Tap – Google’s April Fools joke this year. I looked at it and though “I can do that” – so I did. Here’s the Google Play link (until they pull it!)

EDIT

After less than 24 hours the app got pulled from the store by Google, but still managed to get an impressive 5,646 downloads with 143 comments and a 4.7 / 5 user rating. I’ve removed all the Google branding and repackaged it – Here’s the Google Play link

Read More

Learning Vim

Today I decided to level up and start learning Vim (VI improved) – I know it’s a long road ahead but there are many benefits:

I’m going to start out using Learn Vim Progressively, but I’m going to adapt as I go. So, from that page – first lessons:

  • i → Insert mode. Type ESC to return to Normal mode.
  • x → Delete the char under the cursor
  • :wq → Save and Quit (:w save, :q quit)
  • dd → Delete (and copy) current line
  • p → Paste
  • Recommended:
  • hjkl (highly recommended but not mandatory) → basic cursor move (←↓↑→). Hint: j look like a down arrow.
  • :help → Show help about , you can start using :help without anything else.

And a few of my own:

  • u → Undo the last change
  • :q! → force quit without saving

Read More

How to install packages from the command line in (K)Ubuntu

This is more a note for myself, but recently I had an instance where I had a deb package to install but it needed me to accept a license – thing is that it wouldn’t work using the GUI – it’d just display a window saying ‘Done’. I worked out I had to install from the CLI to get it to show the license so that I could accept it.

Anyway – here’s the command :

sudo dpkg -i <location of deb file>

Read More

Easy way to reduce the size of png files

I’ve been doing some work with some hefty png files recently and was pointed to a page on my colleagues website where he explains how to compress pngs without losing any quality. I won’t go through the details of what he’s doing – you can go have a read up of his site for that, but I’ve seen reductions in size from about 400Kb to about 110Kb without any loss of image quality.

Make sure you backup your files before you do any of this – I won’t be held responsible for any issues you have if this messes up for you!

I’ve replicated the steps below but formatted them for use in bash and included a little python script I wrote to sort through and pick out the smallest files as I found that not all the steps reduced the file size of all the png files, depending on the original file.

for i in *.png; do pngquant -ext -convert1.png 256 $i; done
for i in *-convert1.png; do pngout -c3 -d8 -y -force $i ${i%%convert1.png}convert2.png; done
for i in *-convert2.png; do pngcrush -bit_depth 8 -brute -rem alla -reduce $i ${i%%convert2.png}convert3.png; done

pngcrush takes ages on my machine, so make sure you have enough time if you have lots of files to crunch.

After this you should have a list of files:

  • landscape.png <- this being your original file
  • landscape-convert1.png
  • landscape-convert2.png
  • landscape-convert3.png

Now all you need to do is pick out the smallest, which I’ve written the following Python script for. It’s not optimised, but it works. It’ll run through all your files and remove all but the smallest and rename it to the original name of the png:

#!/usr/bin/python
import os, re

class PngFile(object):
    def __init__(self, filename):
        self.name = filename
        self.path = path + filename
        self.size = os.path.getsize(self.path)

path="<INSERT PATH HERE>"  
dirList=os.listdir(path)
out = ''
convertString = '-convert'
pngString = '.png'
reobj = re.compile('([^.]+)\.png$')
reobj2 = re.compile('[^.]+convert.\.png$')
filename = ''
count = 0
sizesaved = 0
for fname in dirList:
   if reobj.match(fname) is not None:
       if reobj2.match(fname) is None:
	   files = [PngFile(fname)]
	   count+=1
           filename = reobj.match(fname).group(1)
           if(os.path.exists(path + filename + convertString + '1' + pngString)):
	       files.append(PngFile(filename + convertString + '1' + pngString))
           if(os.path.exists(path + filename + convertString + '2' + pngString)):
	       files.append(PngFile(filename + convertString + '2' + pngString))
           if(os.path.exists(path + filename + convertString + '3' + pngString)):       
	       files.append(PngFile(filename + convertString + '3' + pngString))
	   smallestFileSize = min(files, key=lambda pngFile: pngFile.size).size
	   biggestFileSize = max(files, key=lambda pngFile: pngFile.size).size
	   smallestFilePath = min(files, key=lambda pngFile: pngFile.size).path
	   saved = biggestFileSize - smallestFileSize
	   sizesaved += saved
	   print "{2} - smallest file found {0} - saving {1} - {3}".format(smallestFilePath, saved, filename, fname)
	   for file in files:
	       if file.path is not smallestFilePath:
		   print "remove {0}".format(file.name)
		   os.remove(file.path)
	   print "RENAME {0} -> {1}".format(smallestFilePath, path + fname)   
	   os.rename(smallestFilePath,path + fname)
print "files found={0} - saving {1}".format(count, sizesaved)           

Read More

Getting a YouTube clip to run inside your Android application

Recently I’ve been working on an application for a company based near to where I live – part of their application involves some video in the app. Now Android has a 50 meg limit on apk files in the market, so I was tasked with streaming the videos from YouTube, which doesn’t seem to be a massive problem up front.

How wrong I was.

Getting YouTube videos to stream inside an Android app is actually really difficult – I hunted for hours on StackOverflow.com looking for something which would work, only to be given half baked code or ill-thought-out solutions. I got something working by faking the browser agent and loading the video directly, but this would only work in Android 2.x and then only if the user has Flash installed.

So I wrote to my client – telling them that I’d already spent quite a lot of time on this, and that I wanted them to make a call on how best to proceed…and about 30 minutes later I found the answer. Open YouTube Player is a fantastic bit of code, written by Keyes Labs which you can pretty much drop in to your code to get working. It looks like it’s dependant on the way YouTube encodes their videos, so if that changes then it may break, but the project is active on and it’s the best solution I’ve found.

I hope this saves someone else a lot of hunting the internet for a solution to this problem, and a massive thanks to Keyes Labs.

Read More