Merging with vimdiff

Thu, 2008-09-11
Open the files with vimdiff:
$ vimdiff file1 file2

To jump to the next difference:
]c

To jump to the previous difference:
[c

If there are only two files you can copy differences with:
:diffget
and
:diffput

If you are doing a diff with vim between more than two files you also have to specify from which buffer to :diffget or from which buffer to :diffput
:diffget b#
where b# is the buffer number. In vertical window split, buffers are numbered from left to right starting at 1.

Advertisements

vim tip: expression register + autoincrement + recording ….

Tue, 2008-07-01

Repost from my tumblelog:

I had a line like this:
    PARTITION p_op1 VALUES LESS THAN 150,
and I wanted to generate this:
    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op3 VALUES LESS THAN 450,
    PARTITION p_op4 VALUES LESS THAN 600,
    PARTITION p_op5 VALUES LESS THAN 750,
    PARTITION p_op6 VALUES LESS THAN 900,
    PARTITION p_op7 VALUES LESS THAN 1050,
    PARTITION p_op8 VALUES LESS THAN 1200,
    PARTITION p_op9 VALUES LESS THAN 1350,
    PARTITION p_op10 VALUES LESS THAN 1500,
    PARTITION p_op11 VALUES LESS THAN 1650,
    PARTITION p_op12 VALUES LESS THAN 1800,
    PARTITION p_op13 VALUES LESS THAN 1950,
    PARTITION p_op14 VALUES LESS THAN 2100,
    PARTITION p_op15 VALUES LESS THAN 2250,
    PARTITION p_op16 VALUES LESS THAN 2400,
    PARTITION p_op17 VALUES LESS THAN 2550,
    PARTITION p_op18 VALUES LESS THAN 2700,
    PARTITION p_op19 VALUES LESS THAN 2850,
    PARTITION p_op20 VALUES LESS THAN 3000,

How to do it the vim way:
Copy the line we originally had and multiply the value by 1, so that we have this:

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op1 VALUES LESS THAN 150 * 1,

place cursor at the beginning of the line (cursor position is highlighted): _

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op1 VALUES LESS THAN 150 * 1,

start recording into register a: qa
increment partition number: ctrl+a
Now we have:

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Move to the comma: f,

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Move one position left: h

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Increment the number 1 by 1: ctrl+a

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Copy & paste line: yyp

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Move back to the end of the previous line: k$

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Delete expression into register n: “nd3b

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN ,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Enter insert mode: i
Enter expression mode: ctrl+r =
Paste deleted expression: ctrl+r n
Insert result of expression: <Enter>

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Exit insert mode and move to the beginning of the next line: <esc>2_

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Stop recording: q
Repeat what has been recorded as many times as you want, for instance, 10 times: 10@a

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op3 VALUES LESS THAN 450,
    PARTITION p_op4 VALUES LESS THAN 600,
    PARTITION p_op5 VALUES LESS THAN 750,
    PARTITION p_op6 VALUES LESS THAN 900,
    PARTITION p_op7 VALUES LESS THAN 1050,
    PARTITION p_op8 VALUES LESS THAN 1200,
    PARTITION p_op9 VALUES LESS THAN 1350,
    PARTITION p_op10 VALUES LESS THAN 1500,
    PARTITION p_op11 VALUES LESS THAN 1650,
    PARTITION p_op12 VALUES LESS THAN 1800,
    PARTITION p_op12 VALUES LESS THAN 150 * 12,

Complicated? Maybe, but a lot more fun and much faster than doing it manually ;-)


Create mp3 to practice mental addition

Sat, 2008-06-28

Repost from my tumblelog.

This script uses the festival text to speech system, SoX and lame to generate an mp3 to practice mental addition. It basically says two numbers and after a delay says the sum of those two numbers, the delay is supposed to give you time to calculate the sum mentally:


#!/bin/bash -e

# output file
output=$1

if [ -z "$output" ]; then 
        echo "usage: $0 OUTFILE"
        exit
fi

CWD=`pwd`
TMP=`mktemp -d /tmp/speed_addition.XXXX`
pushd $TMP

# pause length in seconds
pause=8

for i in `seq 32`; do
        x=$(($RANDOM % 999))
        while [ $x -lt 11 ]; do
                x=$(($RANDOM % 999))
        done
        y=$(($RANDOM % 999))
        while [ $y -lt 11 ]; do
                y=$(($RANDOM % 999))
        done
        echo "$x . + . $y" | text2wave -o add$i.wav
        echo "$(($x+$y))" | text2wave -o result$i.wav
        sox add$i.wav add_with_pause$i.wav pad 3 $pause
        sox add_with_pause$i.wav result$i.wav out$i.wav
done;

sox out*wav out.wav
lame --ta speed-math --tl speed-math --tt $output --silent out.wav $CWD/$output

popd

rm -r $TMP