Just another tech blog.

A blog abt GNU/Linux, programming, hacking, and my life.

Server and client mode in Vim

Posted by ajay on October 21, 2009

Here is another interesting and useful feature of vim. We can start vim as a server which will send and receive commands and execute them.

The first use-case of the above feature is to give vim a one-instance functionality, just like Firefox. We click on a link anywhere in the system, it doesn’t open a new Firefox window. Just creates a new tab in the existing Firefox instance.  To achieve similar functionality in vim, we need to start vim as a server -

[command_promot]$  vim –servername SAMPLESERVER file1.txt

Done. Now you can go to any shell in the system, and to open a file in the existing vim instance, just do -

[command_prompt]$ vim –servername SAMPLESERVER –remote-tab file2.txt file3.txt

The above command will open file2.txt and file3.txt as new tabs in the existing vim instance running :).

If the above typing is very long and cumbersome, you can just set a couple of aliases [one for command to start the server and another for opening the file in existing server]. You can also choose any server-name and you can also run multiple servers in your system [A possible use-case when you're working on multiple projects and run one vim instance for every project and while opening the file you decide which vim instance to use for opening this file]. The servers are recognized by the –servername option which you give while starting the server.

The server has additional functionality in which it can receive a command and execute it. This can be quite handy when you use multiple computers and login to a server machine for working. Example – You were using machine A and had a vim instance running.  You left this machine and started using machine B and would like to continue editing where you left on machine A. If your vim instance was started as a server (assuming server name to be SAMPLESERVER), then from machine B you can just login to machine A and execute the following command -

[command_prompt_on_machine_A]$ vim –servername SAMPLESERVER –remote-send ‘<Esc>:mksession ~/sessionFile.vim<CR>:wqa<CR>’

Essentially this command will send the existing server running by name SAMPLESERVER, to store all the progress in ~/sessionFile.vim, save all files and exit :). [If you dont know what mksession does, please read my earlier post about sessions in vim]. Now to continue with the same settings, you start your vim instance by picking up the settings from the session file -

[command_prompt_on_machine_A]$ vim –servername SAMPLESERVER -S ~/sessionFile.vim

Done. This will allow you to edit files in one single environment, even if you work on different machines :). Happy Vimming.

PS: vim needs to be compiled with +clientserver in order for you to be able to use this feature.

Posted in vim | 8 Comments »

One liner to find and remove duplicate files in Linux

Posted by ajay on October 16, 2009

I recently found a one-liner to report all duplicate files under the current directory and its subdirectories here. The command is as follows -

find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

It first compares size and then compares md5 hash in order to find duplicate files. Since this one just reports and doesnt delete the files, I’ve made slight modifications to find and DELETE duplicate files as well. Don’t worry, it’ll ask your permission before running the delete command over all files. Here it goes -

find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d |  xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate | cut -f3-100 -d ' ' | tr '\n.' '\t.' | sed 's/\t\t/\n/g' | cut -f2-100 | tr '\t' '\n' | perl -i -pe 's/([ (){}-])/\\$1/g' | perl -i -pe 's/'\''/\\'\''/g' | xargs -pr rm -v

The modifications are very boring, but that’s all I could do.  Have a better solution? Let me know. If you want to delete files without asking permission, remove the -p after last xargs in the above command.

Have Fun :).

PS: The command is primarily to be used for deleting duplicate media files (mp3, videos, images etc.). Please dont run it on any sensitive system directory.

Posted in Linux | 29 Comments »

Vim Sessions

Posted by ajay on October 15, 2009

Vim editor also supports sessions just like a browser like firefox.

If you have 8-10 tabs having > 30 files open in vim [ this should be the case if you're working on a project of reasonably big size] , and you have to reboot your PC or restart x server for that matter, it can get really irritating to close vim, and then open files one by one again :(.

Vim session solve exactly this problem. When you have to close your vim for any such thing, you can just do

:mksession /path/to/session/file.vim

This will store everything in current vim session including all open files, all visible buffers, window sizes and keyboard mappings etc.

Now when you want to restore vim from this particular session, you can just do

command_prompt$  vim -S /path/to/session/file.vim

This will restore everything just the way it was :).

You should have vim compiled with session support in order to be able to do this. I think from version 7 onwards, even the standard vim supports this by default. Also, for the setting to work, you need to add the following line to your .vimrc

set sessionoptions=blank,buffers,curdir,folds,globals,help,localoptions,options,resize,tabpages,winsize,winpos

PS: Vim session doesnt keep track of undo history in each open file.

Posted in vim | 5 Comments »

Vim Timeline

Posted by ajay on October 13, 2009

I found an interesting feature of vim today. If you edit lots of files and dont close your vim session for long time, then its a very powerful feature. It essentially allows you to jump back in time and change a file to what it looked like at a particular time.

For example, you made lots of changes, now dont remember them and want to switch back to the version of the file 10 minutes ago, you can just do

:earlier 10m

Similarly, if you want to jump 5 seconds ahead of time, you can do

:later 5s

Also, doing

:earlier 10

means doing 10 undo’s in one shot :).

Happy Vimming :).

Posted in vim | 10 Comments »

My Digital Signatures :)

Posted by ajay on May 8, 2008

One evening I didn’t have much work to do and I had a look at this page. After seeing the Quote there, I thought why not create one such signature for myself :). And here it is ->

i;double r[]={175297239055116917538816e2,591298870944180608}

Just a little note: You need to compile it using gcc ( g++ wont work ) in Linux.

PS 1: Many thanks to Maruti Borker for helping me in creating the signature.

PS 2: I’ve become foosball addict. For more details check out Maruti Borker’s recent post.

Posted in Programming | 10 Comments »

Success Stories this year: ug2k5 @ IIIT Hyderabad

Posted by ajay on May 3, 2008

Background: The last comment on my last post ( which was about 1 year ago ) encouraged me to write a post. And then after reading Aditya Agarwal’s blog and noticing that he too wrote a post after about 1 year, I thought that finally I should write one today :).

I guess the title explains what this post is about. Our batch has been doing great this year and almost all of my good friends have been able to get decent internships or other great opportunities to work this summer. I’m really happy about it and here I’m going to list the major good achievements by my batchmates this year ( 2008 ) :-

1. Felicity 08 was a great success. It involved a lot of hard work from my batch-mates. Since a lot of people have written about it already, so I guess I don’t need to put that all again.

2. Internships:

a) Amazon: Ankit Saraswat, Anupama Gali, Rahul Tenany, Shashank Agrawal, Sunil Soni.

b) GE Research: MNV Kartheek, Sashidhar G, Saurabh Sharma.

c) Google: Ajay Somani, Ankush Kalkote, Maruti Borker, Ramya Yaragani.

d) Gridstone Research: Subhashish Chand.

e) IBM: Navatha T, Sahiti P.

f) Microsoft: Aditya Agrawal, Gaurav Kharkwal, Harsh Vardhan Bansal, Himank Sharma, Manish Arora, Ojasvi Rajpal, Randeep Singh Banga, Shikha Agarwal, Vivek Prakash.

g) Microsoft Lab Banglore : Abhijeet Pagare, Abhinav Parasher, Gaurank Khatri, Karan Maroo.

h) Nvidia: Richeek Arya.

i) Philips: Akash Agrawal, Phani Deepti.

j) Sarnoff: Piyush Nigam, Raveendra Meka, Ravindra Reddy.

k) SkWorks: AK Kartheek, Nitin Jain.

Lydia has got an internship at University of Sydney ( Australlia ). special congratulations to her. [ I have only listed the people who are joining, there are other offers too ].

3. Google Summer of Code: This I consider as the biggest achievement of my batch-mates. This year, 3 of them have got selected for Google Summer of Code. The lucky ones ( or rather, the deserving ones ) are Kulbir Saini, Vamshi Velegapuri, Kiran Kumar M. Kulbir Saini’s mentor is the original developer of Yum. Quite exciting opportunity to work with such a person. Hopefully these people will make some good contribution towards open source.

4. Research Papers: Vibhav Vinet of my batch has recently got a research paper accepted in CVPR. CVPR is one of the top 3 conferences in the area of computer vision. He worked on a parallel algorithm for graph cuts which can be implemented on specialized hardware ( contact him for more details :) ). There are some other people who are sending research papers in various conferences but I wont list them now.

5. Microsoft Open Source Scholar: From this year onwards, Microsoft is also funding open source projects by students in Indian universities provided that they are built around microsoft technologies. Following people from my batch have got this scholarship – Ankush Kalkote, Randeep Singh Banga, Aditya Agarwal, Manish Arora, Vamshi Velegapuri, Kiran Kumar M., Satyanarayana Koneru, Sridhar Duggi.

6. 3 of my batch-mates ( Kulbir Saini, Vamshi Velegapuri, M. Bhargav Chowdary ) have recently got the IIIT website redesign project. They will be working on main IIIT website and also the websites of all the research centers. Good thing about this is that they competed with 5 other companies to get this project and won :).


This year, I have teamed up with Anshuman Singh (ug2), Gaurav Agarwal (ug2) and we have got decent ranks on a lot of contests conducted by various universities in India. This includes winning the annual programming contests ( some online and some onsite ) conducted by IIT Kharagpur, IIT Delhi, NIT Surathkal, Delhi College of Engineering, NIT Durgapur and Madras Institute of Technology. Anshuman Singh is also one of the top 5 candidates of India Student Mathematika competition conducted by Wolfram research. Kudos to him..


I hope these success stories list continues to grow like this in the next academic year too :). I will end the post now..

PS 1: I’m getting a lot fatter because of the good food at Google :D.

PS 2: If by mistake, I forgot to include anyone’s special achievements or anyone would like me to list them here. Please drop me an email, I will gladly update the post.

PS 3: I don’t know if someone else already posted something like this, I don’t read blogs often.

PS 4: Nightouts at Google office :), god I’m crazy..

Posted in My thoughts | 17 Comments »

I love sudoku puzzles

Posted by ajay on January 16, 2007

I am writing a post again after a long time. Didn’t write one primarily due to the fact that I was very much busy these days due to Hectic schedule at college and ofcourse some other things ;). Anyway In vecations I wrote a kind of interesting c++ program to solve sudoku puzzles given in daily news papers. The program uses backtracking and thats why It’s complexity is exponential in general. But still on most of the puzzles that I tried; It works very fast ( almost negligable run time). You can download it or have a look at it here. If you find input puzzles where it doesn’t work ( bugs ) or you have better ideas to solve a sudoku puzzle ( in terms of run time ofcourse ), then please post it as a comment.

PS 1: The program is primarily written to solve puzzles of size 9 x 9. but can be easily modified to solve a bigger puzzle.

PS 2: I have been trying to solve this problem. But my idea doesn’t seem to run in time :(. If anybody solves it, please post a comment about the idea used to solve the problem.  ).

Posted in Programming | 13 Comments »

Setting up new keywords for programmes in vim

Posted by ajay on September 8, 2006

Recently I was working on my operating systems assignment and I noticed that all the primitive system types like pid_t , mode_t , dev_t , nlink_t  etc do not come colored in vim editor like basic data types such as int float etc.  I thought it would be nice to have those words colored as we have int and stuff .

all the keywords are related to syntax, so for creating new keywords. First of all we have to create a class of words. For that we use the syntax command. For example suppose for a C program you want to highlight some words I mentioned above as datatypes, then you should  go to the vim configuration file specific to C programs ( Previously I told how to have specific configuration files for specific file types in vim here ) and add the following code -

syntax keywod cSysvar pid_t mode_t something_t x_t

highlight link cSysvar Type

now here cSysvar is the name of the class which you want to create. You can have as many of these classes as you want but the class name should begin with the file type you want to edit (the same class in a cpp file will be named cppSysvar) and the next character should be capital to distinguish the extention from the class name.  All the words written after cSysvar in the first line are the elements in the class cSysvar.  The second line tell vim to recognize all the elements of class cSysvar as they are the members of class ”Type’ (Type is used to recognize datatypes).

some other alternatives of type are as follows  -

Type -> datatype

Comment -> comment

Error -> syntax with errors.

Number -> the color used for displaying numbers.

Character -> used to represent single characters.

Some other class names include Identifier, Underlined, Ignored, Delimiter, Statement, Preproc (preprocessor directives) and more. You can change the last word in second line of the snippet to have variation in the color used to display those particular words.

I hope this will make Vim more beautiful.

Happy Viming ..

Bytheway .. I recently got through in the Qualification round of Google Code Jam 2006 and the next stage will held on 14th of september :).  looking forward for that as well as Topcoder Collegiate Challange.

Posted in Programming, vim | 10 Comments »

Some good looking codes and interesting problems

Posted by ajay on July 21, 2006

I recently came across some awesome piece of codes. I dont understand any of them, but I find it worth sharing here.

1. A c program to solve The Tower of Hanoi problem without using functions,  stack etc. and does the task in 6 lines. Moreover, the code looks like a tower as well :).

“disk %i from %i to %i.\n”/**/

2. The queen of problems – N queens problem. If you are a computer science student, you must have heard about n queens problem and probably solved that as well.  This C program solves the problem very quickly, and for the rest – just have a look at the code.

int v,i,j,k,l,s,a[99];main(){for(scanf(“%d”,&s);*a-s;v=a[j*=v]-a[i],k=i<
s,j+=(v=j<s&&(!k&&!!printf(2+”\n\n%c”-(!l<<!j),” #Q”[l^v?(l^j)&1:2])&&++

3. Prints first 15000 digits of PI.


4.  An interesting problem -

what is the “condition” such that, this snippet of code prints HelloWorld!!

if condition




my friend abhilash gave a nice solution for that ..

if ( printf(“Hello”)<0){




5. Another interesting problem -

Write a small C program, which while compiling takes another program
from input terminal, and on running gives the result for the second
program. (NOTE: The key is, think UNIX).
Suppose, the program is answer.c
Then, while compiling
$ cc -o answer answer.c
int main()
printf(“Hello World\n”);
$ ./answer

Hello World
if anybody can solve this problem please let me know by commenting over the post with the solution.

Happy coding.

Posted in Programming | 17 Comments »

Some Special Codes

Posted by ajay on July 16, 2006

I was solving problems some time ago and came to know some good facts about c and c++  and wrote some interesting codes.

1. A C program which can print the file name it is kept in ;).



printf(“the source file name is %s\n”,__FILE__);


actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest.

2. Usage of assignment suppression operator in scanf  – Suppose you have some crap input (the input is provided to you but that is not of any use to your program) then what normally we do is take a dummy variable and scan the input in that variable.  Example -

int dummy;


but there is one more method which can save memory and time

what you do is


In this method, we provide a character * to the scanf function and by doing that; scanf will scan the input from standard input but it wont assign it to any variable. This is scanned in a buffer of sufficient size and you dont have to worry about that.

3.  My source code for problem KAMIL on spoj -


If anybody has a better idea on this problem then please do comment on the post.

4. Printing something with variable width  -

Suppose you are to print some formatted output in some program and for that sometime you require to print a variable in a fix width ..

for example if you have to pring an integar in width of 5 or more then you do

printf(“%5d”, var);

now if var is 10 it will append 2 leading spaces to complete the atleast 5 width rule.

now suppose you dont want leading spaces but leading zeroes, then here is another method to write the same -


now if var is 10 it will print 00010 and so on.

Another condition comes if the width in which we want to print the variable is not known at the compile time – suppose you want to print variable with a width which is contained in another variable, then we can use -


here x is the width length variable. Whenever we do this * operator .. then it looks for the next integar argument provided in the arguments and then  assumes that it is the width in which  the variable is to be printed.

Now if x = 2 then it will b the same as

printf(“%2d”,var); ..

this way we can work with this width thing pretty well.

If any of you guys know some nice facts about c or cpp language, then please post them as comments. Any gud suggestions related to things I posted are also welcome. Have Fun.

Posted in Programming | 35 Comments »


Get every new post delivered to your Inbox.