OCaml Development in Vim

This is a quick run-through of how I set up my development environment in vim:

Install pathogen.vim

mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \

Add the following to ~/.vimrc:

execute pathogen#infect()
syntax on
filetype plugin indent on

Install Syntastic

cd ~/.vim/bundle
git clone https://github.com/scrooloose/syntastic.git

Then quit vim and used :Helptags to check installs so far have worked.

Install Merlin

opam switch 4.01.0dev+trunk
opam update
opam upgrade
opam install merlin

Add the following to ~/.vimrc

:set rtp+=~/.opam/4.01.0dev+trunk/share/ocamlmerlin/vim
:set rtp+=~/.opam/4.01.0dev+trunk/share/ocamlmerlin/vimbufsync
let g:syntastic_ocaml_checkers=['merlin']

:SyntasticInfo will return a list of syntax checkers available to Syntastic, check that this now includes merlin

Install OCP Indent

opam install ocp-indent

Add the following to ~/.vimrc

autocmd FileType ocaml source /home/heidi-ann/.opam/4.01.0dev+trunk/share/typerex/ocp-indent/ocp-indent.vim

How do I download many pdf’s a webpage at once ?

A combo of curl and regular expression, e.g.

Ross Andersons “Security Engineering” is avalaible online under CC here but you must download each chapter as a seperate pdf, this is can the fixed using:

$ curl http://www.cl.cam.ac.uk/~rja14/Papers/SEv2-(toc|pref|acks|c[01-27]|biblio|index).pdf

Quick Guide: Remotely adding a new user on Ubuntu 12.04

In the following is a quick guide to creating a new user (we will be calling this user “username”) on a Amazon Cloud EC2 instance with Ubuntu 12.04 AMI


We will begin by adding the new user and setting the new users password

$ sudo adduser

This command will then take you though the setting up a password. You can check the users that you now have my opening the /etc/passwd file

Now we are going to make use of a unix command called “visudo”, this allow you to edit the sudoerrs file in a concurrent access safe way.  Before using visudo, we need to check that the default editor is the one that we would like and change it, if required:

$ sudo update-alternatives –config editor

Now you will be given a dialogue where you can select your favourite text editor.

$ visudo

Now add the following line to the file that has been opened:


Now exit the machine and log back in as ubuntu.
You can now switch to this newly created user with
$ su
$ cd /home/


We are now going to generate the public key for public/private key pair that will be used later to SSH directly into this new user

$ ssh-keygen -b 1024 -f mykey -t dsa

This line has generated two files, the public key mykey.pub and the private key mykey. To be able to ssh from a local machine to the new paws user on this remote machine, we need to place copy the contence of paws.pub into /home/username/.shh/authorized_keys and we need to have a copy of the private key (called mykey) on the local machine

$ mkdir .ssh
$ chmod 700 .ssh
$ cat mykey.pub > .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys

Now that we have the key we need to transfer this key on to our local machine and generate the private key

$ sudo chown :ubuntu .ssh
$ sudo cp mykey /home/ubuntu
$ sudo chown :ubuntu .ssh/authorized_keys
$ sudo chmod 777 /home/ubuntu/mykey

Logout and return to local machine
$ scp -i originalkey.pem ubuntu@:/home/ubuntu/mykey mykeyNow you can ssh into your remote machine as this new user:$ chmod 400 mykey$ ssh -i mykey @url-of-server

Producing Pretty Graphs

My aim to reproduce Figure 1: Internet users and non-users by age group (years), 2012 Q3 from the Internet Access Quarterly Update, Q3 2012 from the Office for National Statistics. For this I will be using Octave


1) Download data as xml and convert to csv
2) Read the data into Octave and check this it has been read in correctly
data = data = dlmread(“<my_file.csv>”,”,”);
The expected output will be 8 4
3) Divide the date into the correct axis
x = data(:,1);
ya = data(:,2);
yb = data(:,3);
yc = data(:,4);
4) Plot the new data

     plot(x,ya,”;Used within last 3 months;”,x,yb,”;Used more than 3 months ago;”,x,yc,”;Never used;”)

5) Label the x and y axis
xlabel(“Age Group(Years)”);
ylabel(“Percentage who have used the internet”);
6) Give the graph a title

     title(“Internet users and non-users by age group (years), 2012 Q3″);

7) Output the graph and save

VIM – The CompSci Classic

Learning to escape VIM

I have a confession, the first I launched vim I could not even work out how to quit it so I took the lazy way out and just closed the terminal. This put me right off vim for quite a while. Now its time to really try to learn it.

Vim has two modes: insert and normal, when you launch vim it will go straight into normal mode, from normal mode type i to switch to insert mode and then press ESC to return back to normal mode.

GOLDEN SECRET 1: To exit VIM, enter :q in the normal mode

Normal mode basics 
You can delete the character under the cursor using x and using :w to save. To cut the current line use dd, this will delete the text on the current line and also delete the line itself, the cursor will move to the next line, which is now where the original line was. To copy the current line use yy. Now to paste the recently cut line, use p  to insert the text onto the line below where the cursor currently is or P to insert text before cursor.

Instead of the arrow keys, you can make use of hjkl to move the cursor, but personal I prefer to use the arrow keys

Entering Insert mode
So far we have entered insert mode using i, there are some alternative ways to enter insert mode, which allow you to move to insert mode and perform a useful operation in one key stoke, some of the alternative methods to enter insert mode are:

  • a – insert mode and move cursor back one charactor
  • o – insert mode and insert a new line after the current one
  • O – insert mode and insert a new line before the current one
  • cw – insert mode and delete the current word
GOLDEN SECRET 2: Insert mode has auto complete, use Ctrl-n to activate

Moving around normal mode
As well as using the arrow keys (or hjkl) to navigate around normal mode, there are plenty of useful extra shortcuts for navigating around. 0 jumps to first character in the row and $ goes to the last character in the row. ^ jumps to the first non-blank character of a line and g_  goes to the last non_blank character of line.

To search for a particular work or character, use / and enter search term. This will move the cursor to the next occurrence of the search term after the cursor, so to search for the first occurrence of a term in a file, put the cursor at the start of the file and then use / to search for the term.

To jump to the start of the next word use w and to go to end of this word use e, when  a word is composed of letters and underscores only, if you want to word to include special characters use W and E instead

GOLDEN SECRET 3: % means go to corresponding (, { and [

Text editor basics
u is used to undo and Crtl-r to redo. To open a file use :e , :w to save, :saveas , :x to save and quit. :q! to quit without saving. . will repeat last   command and N will repeat the command N number of times. NG goes to line N and G is go to last line

GOLDEN SECRET 4: * means to next occurrence of word under cursor and # means go to previous
1 2