Just another tech blog.

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

Archive for October, 2009

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 | 16 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 | 36 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 | 11 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 | 13 Comments »