Just another tech blog.

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

Archive for the ‘vim’ Category

Tips and Tricks on vim editor.

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.

Advertisements

Posted in vim | 16 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 »

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 | 13 Comments »

Installing color schemes for Vim editor

Posted by ajay on June 24, 2006

If you are busy programming all the day and getting bored with the default color scheme of vim editor. You can install some color schemes for vim editor as you want and then you can switch between them.

for installing color schemes first you need to create two directories in your home directories [if these directories dont already exist]

[user@localhost.localadmin]mkdir ~/.vim [enter]

[user@localhost.localadmin]mkdir ~/.vim/colors [enter]

Thats it. Your pc configuration part is done.  Now you will need the theme configuration files. You can download the latest color schemes and many old ones from here (official website of vim editor).  There are many available themes to suit your mind and mood.

Now for each theme or I better call it a color scheme you will get a .vim file or an zipped archive which after extracting will produce lots of .vim files

now you have to copy all the files to the directory .vim/colors/ in your home directory.

For example, if you have downloaded a theme called matrix.vim, then –

[user@localhost.localadmin]mv matrix.vim ~/.vim/colors [enter]

Bingo, the colorscheme is installed, now whenever you are editing a file, you can change the colorscheme using

:colorscheme matrix.vim

You can install as many themes as you like by copying files into that directory.

Now if you want to make a theme your default theme, then you should add this command to your .vimrc –

colorscheme matrix

Here matrix is the name of the colorscheme.

You can also design a theme for vim editor yourself if you are interested. You can take a look at any existing colorscheme file and change the colors in the file to create a new scheme for yourself.

Any better ideas are always welcome. Happy Vimming.

Posted in vim | 33 Comments »

Vim – applying separate settings for different file types

Posted by ajay on June 2, 2006

If you are using vim quite often, you might have configured your vimrc very well. But these settings grow continuously; so it is always not feasable to apply same settings for each and every filetype .. you might want to apply some x settings if you are writing a cpp programme and some y settings when you are editing an html code and so on.  Your vimrc might be growing too large so you might want to separate it into different files; I mean different configuration for different file types.  Here’s what you can do –

[user@localhost.localadmin]vim ~/.vimrc

The syntex for specific filetype settings is

autocmd   FileType cpp source ~/.vim/cpp.vim

autocmd   FileType py source ~/.vim/py.vim

and so on…

now the third word in these lines specifies the extention and the fifth word specifies the file code to be executed .. that means from now onwards if a person will open a .cpp file ( cpp programme ) the vim settings writtin in ~/.vim/cpp.vim .. will be applied ..

you can specify settings for as many filetypes as you want .. and b sure that if you specify a source file in .vimrc of yours then even if you dont want to apply any settings as of now .. plzz create an appropriate file at an appropriate place which you have specified in the .vimrc otherwise whenever you will try to edit that filetype it will give errors .. thats it so now you can apply different settings for different codes.

I will put my .vimrc and some separate settings for different filetypes may be in a day or two .. any comments if you have any better ideas ..

Posted in vim | 55 Comments »

Taking backup of files using Vim

Posted by ajay on June 1, 2006

Many people have the habit of saving the file after every around 5 minutes .. there might be cases when you want to change something in your programme and see the output .. and if the output is not worth .. you might want to get the original programme back .. or by mistake you delete some lines frm a file and save the file In both cases you need the original file back .. To escape from such situation .. you might move the file to another file each time you want to edit the file but that is very tedious work .. so here is another method .. set the backup option in vim and it will do the rest for you ..

wat you have to do is ..

[x@localhost.localadmin]vim ~/.vimrc

now append these lines ..

set bk

set bdir=~/backup

set bex=bkp

now save the file .. in these lines .. first lines specifies that the user wants to take backup .. ( enables the backup option .. ) . The second line specifies the directory in which the backup will be stored .. the line specifies the string to be appended to the file name to get the name of the backup file .. like you save a file named a.cpp then a file named a.cppbkp will be created in ~/backup directory .. you can skip the third line .. in that case the default string which will be backup files contain is ~ .. ( the example file be stored as a.cpp~ ) ..

From now onwards whenever you save any file its backup will be saved in ~/backup directory .. ( you might choose a different one based on your choice .. ) .. and remember before opening any file CREATE a DIRECTORY NAMED BACKUP in your HOME DIRECTORY .. and the backup system is ready .. and whenever you save a file second time .. the original backup file is rewritten .. I still dont the know the methods for multilevel backup .. if you know .. please let me know by leaving a comment ..

Posted in vim | 2 Comments »

Calculater in Vim

Posted by ajay on June 1, 2006

Sometimes you write complicated mathematical expression in a vim and you need the result immediately .. and you dont want to open any additional application .. here is a method to configure a calculater for vim ..

[x@localhost.localadmin]vim ~/.vimrc

now after opening the vimrc .. append the following line to it ..

inoremap <C-B> <C-O>yiW<End>=<C-R>=<C-R>0<CR>

What this line exactly doing is setting ctrl+b as a shortcut for a complicated method which u have to do to otherwise to calculate the expression .. This calculater is configured for insert mode in vim ..

after this whenever you open a file .. and you write expression like ..

2+2

now press ctrl+b …. and it will replace it with ..

2+2=4

Isnt it funny .. this small calculater takes care of

+ => addition

– => subtraction

* => multiplication

/ => division

% => remainder operator ..

and it also takes care of operater precedence ..

If you have any better ideas .. you might post them as a comment ..

Posted in vim | 4 Comments »