Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Monthly Archives: October 2009

vimdiff – the cool way to diff for vim users

7 Comments

Some of you using the vim editor may not know about a tool that comes with vim called vimdiff. Vimdiff is an awesome way to diff files if you are a vim nut. It gives you the power of vim + the power of diff.

How’s this different from opening 2 files in vim (with -O option) you ask ? The difference is that vim will highlight the diff for you.

Fire it up by giving the 2 filenames, say

$vimdiff old new

This will bring up a screen like this -

vimdiff

vimdiff

Now you can move around the 2 parts of the screen with your regular vim commands. For eg. use (Ctrl+w) + right arrow to move to the right half of the screen.

vimdiff with cursor on right half of screen

vimdiff with cursor on right half of screen

You can copy paste as well. Go to the 2nd line in the left half and press the y key twice to copy that line. Use (cntrl + w) + right arrow to move cursor to the 1st line in the right half of the screen. Press p to paste the copied line below the 1st line.

vimdiff with copy paste

vimdiff with copy paste

You can go to the first line and delete the two words “a new” by moving the cursor to “a” and hitting d2w key combo.

vimdiff deletes words

vimdiff deletes words

Insert “an old” there by going to insert mode(press i key) and then typing the two words. You’ll see that vimdiff does not highlight anything. This means that there is no difference between the two files.

two files with no difference in vimdiff

two files with no difference in vimdiff

Vimdiff is the same as bringing up vim with the -d option. You could do the same things you did above by using

$vim -d old new

Bonus :
You can also diff 2 URLs directly with vimdiff
Try
$vimdiff 'http://www.google.co.in/search?q=vimdiff' 'http://www.google.co.in/search?q=vim'
You’ll first see something like this coming on screen

:!curl -o '/tmp/v959288/1' 'http://www.google.co.in/search\?q=vimdiff'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
"/tmp/v959288/1" 4L, 5289C
:!curl -o '/tmp/v959288/2' 'http://www.google.co.in/search\?q=vim'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
"/tmp/v959288/2" 4L, 5253C
Press ENTER or type command to continue

Now press the ENTER key to see the diff

vimdiff also works directly with URLs

vimdiff also works directly with URLs

You can throw in ssh into the mix as well. Try something like this
$vimdiff old <(ssh [email protected] cat ~/new)

If you're a vim/vimdiff ninja and know some more tricks, do post them below :)

Happy hacking...

Filed under Linux
Oct 20, 2009

User agent GTB or GTB5 is the Google Toolbar

0 Comments

Was checking out this blog’s visitor stats recently when I came across this User Agent "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5".  I was wondering what GTB5 was. Some googling and testing later I found out that GTB is in fact the Google Toolbar. To compare, here’s the UA of a Firefox browser with Google Toolbar installed :

"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5"

And here’s another Firefox without it :

"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)"

Also, if you plan to capture the UA string in the Apache logs, make sure that feature is turned on. Your LogFormat directive must have something like \"%{User-Agent}i\" in it. For eg,

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

You can check the Apache documentation on custom log formats for more info. If you’re using XAMPP/LAMPP, you might want to comment out

CustomLog logs/access_log common

line in your httpd.conf and uncomment

CustomLog logs/access_log combined

line.

Filed under technology
Oct 5, 2009

Copy files preserving the permissions

3 Comments

You might have come across a situation where your regular usage of the cp command for copying files in your linux box was not enough because while copying you needed to preserve the permissions of the files. Fret not, for help is at hand. Take a look at the man page for cp :

-p     same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, all

So that’s it. just use the -p flag to do the copying. Here’s an example where I copy a single file while preserving permissions.

cp -p oldfile ~/new_dest/

To copy a whole directory, I use the -r flag as well.

cp -rp some_dir ~/new_dest/

You can use the –preserve flag to keep whatever attributes you need. For the record, there’s one more related flag

--no-preserve=ATTR_LIST don't preserve the specified attributes

Filed under Linux
Oct 1, 2009