Just another tech blog.

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

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.

Advertisements

13 Responses to “Setting up new keywords for programmes in vim”

  1. Turbo said

    This one is a really enlightening post. And congrats for qualifying in codejam and best of luck for TCCC 🙂

  2. awing piece of Ruumage seems to be a good one for those who wanna have a bit better reconnoiter while mugging over the VIM,therby making the editor more allured or splendid… Gr8 job dude .. Going Great Guns.. keep posting …

    Harshit Here

  3. mythalez said

    best of luck with topcoder/codejam … 🙂

  4. Atul said

    Good one …:) Code jam bond ….

    You told realy amazing tool in Vim ,
    if u put something about other editor also that will be fine
    keep it up…lage raho somani bhai ..:)
    best of luck for next round..:)

  5. Tavva Rajesh said

    Great job yaar.

    Really awesome. U r helping us a lot. Hope u continue enhancing ur knowledge and help others simultaneously.

    Keep up the good work.

    Waiting for more tips from u.

    Though I am not a geek like u, I would like to leave a few tips about vi on the basis of my knowledge. They are:

    (i) U can compile C progs and execute shell or perl scripts without leaving the vi editor. The commands are
    :!cc %[Enter] to compile the present file
    :!%[Enter] to run the present file(if it is a shell or perl script)

    (ii) I think most of the people know that we can use :map command to execute some commands with a single keystroke. But I don’t know whether u know or not that we can use this map command to execute some commands directly in input mode also. For example if we want to
    save the file directly when we r in input mode, then we can type F3 if we map
    like this:
    :map! #3 ^[:w^M

    Here #3 denotes the key F3(function key). ^M is the symbol for [Enter]. This u have to type by pressing [Ctrl-v] first and then [Ctrl-m]. Similarly ^[ is the symbol for esc. This u have to type by pressing [Ctrl-v] first and then [Ctrl-[]. Note the “!” mark after map. This is to specify that we want to save the file while we r in input mode using the key F3.

    So whenever u type F3(in input mode), ur file will automatically be saved.

    (iii)In vi there is a facility of automatic text completion. For example, if u already have some words starting with “p” in the file, then, whenever u type p and then [Ctrl-p], it gives the possible word starting with “p” from the words which r already in ur file. Repetitive use of [Ctrl-p] gives all the possibilities. [Ctrl-n] also does the same job but in reverse direction. This, I think, is useful in C progs to use for printf.

    I would like to put some questions here so that i can get an answer since many people read ur blog. The question is:

    (i)In vi editor, if our cursor is on some particular word, say printf, is there any keyboard shortcut to go to its man page directly.

    (ii) How to invert the case of the letters in the current line i.e., uppercase to lowercase and vice-versa, using some commands in vim.

    (iii) If we type :set nu, the no’s r seen for the current session, but they r not saved along with the file. Is there a way to save them along with the file.

    If some one knows the answer, please write it as a comment.

  6. ajay said

    @Tavva Rajesh

    for (ii) for changing the case of letters in a particular text in a file
    what you do is
    select that text in visual mode and then press ‘~’ ( shift + ‘`’) .. that will reverse the case of all the things which you have selected .. ( but remember; select them in visual mode .. )

    I will post the answers of other questions as soon as I know ..

    and 1 suggestion ..

    in your this command
    :map! #3 ^[:w^M

    the characters you are using are unix specific .. because ^m etc . are terminal characters when you use your file in unix it will work fine but in windows they wont work fine ..
    so a better idea is ..

    map <f3> <esc>:w<cr>

    here <f3> means f3 key ..
    <esc> – escape key ..
    <cr> – enter ..
    similarly you have other f keys .. and some other keys are
    <c-a> – this means ctrl + a in place of a you can write different characters to have different control characters .. and similarly you can have
    <a-b> -> this means alt+b .. you can have other options as in case of control ..

    similarly you have <Home> <del> <end> and so on ..
    if you use the keystroke mappings I mentioned then you can easily port your .vimrc to windows also ..

    Regards, Ajay

  7. Aditya said

    amazing observation nd research on ur part…..i ve grown out of tuch wid linux….cant figure out how 2 display colors in vim on SUSE…. btw congrats 4 geting thru d 1st round of codejam…wish u success in the nxt round…….

  8. ajay said

    @Aditya ..

    I heard the similar problems from many of our batchmates ( displaying colors in vim in suse ) for that just write this line in .vimc at your computer ..

    syntax on

    this is required because suse doesnt turn on syntax by default and you have to manually do that .. !!

    bytheway thanks for your wishes ..

    Regards,
    Ajay

  9. Tavva Rajesh said

    I came to know the answer to my first question. So I wanted to post it. It is [shift-k]. If u press this button when u r on any word, it will take u to the man page directly.

    • Armena said

      Weretwlit-ln content, could I’ve ever read through a little something to your! Truly, exactly why every people will not be able to write can be? Site conserved to favorite songs.

  10. umm… I am no

  11. Trudy said

    There are no words to describe how boduoicas this is.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: